Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: Cannot read property 'prototype' of undefined when run in jest #1898

Open
junbujianwpl opened this issue Jun 28, 2020 · 7 comments

Comments

@junbujianwpl
Copy link

junbujianwpl commented Jun 28, 2020

yarn jest --testPathPattern path/to/file
yarn run v1.22.4
$ D:\workspace\js\myproject\node_modules\.bin\jest --testPathPattern path/to/file
 FAIL  path/to/xxx.spec.ts
  ● Test suite failed to run

      ● Test suite failed to run

        TypeError: Cannot read property 'prototype' of undefined

      at Object.<anonymous>.createComplexClass.isClass (node_modules/mathjs/lib/type/complex/Complex.js:28:23)
      at assertAndCreate (node_modules/mathjs/lib/utils/factory.js:49:12)
      at Object.<anonymous> (node_modules/mathjs/lib/entry/pureFunctionsAny.generated.js:24:38)
      at Object.<anonymous> (node_modules/mathjs/lib/entry/mainAny.js:53:25)

It can be produced with the latest at least three versions. It works fine in my project when run the project by running npm run start. Why this is not working with jest.

@josdejong
Copy link
Owner

Thanks for reporting. How can I reproduce this issue?

@Git-on-my-level
Copy link

Git-on-my-level commented Sep 15, 2020

I was able to reproduce this, it involves configuring jest module resolution using the moduleDirectories key in jest.config.js
I created a minimal reproducible repo here:
https://github.com/Git-on-my-level/mathjs-jest-issue-1898

@Git-on-my-level
Copy link

Git-on-my-level commented Sep 15, 2020

My fix was rather than including . as a target in moduleDirectories I had to explicitly list all my directories or use moduleNameMapper instead

Example

Instead of

module.exports = {
  moduleDirectories: ["node_modules", "."],
};

Replace with

module.exports = {
  moduleDirectories: ["node_modules"],
  moduleNameMapper: {<map module resolutions here instead>},
};

I'm not familiar enough with jest to root cause this, but hope this helps

@josdejong
Copy link
Owner

Thanks a lot for working out this example David!

We're working on improved support for ES modules and Commonjs in the library (see #1941, #1962), it will be interesting to see if we can solve this issue with Jest (or maybe it's even already fixed in this PR).

@josdejong
Copy link
Owner

mathjs v8 was released a few days ago. This improved ES modules support which also gave trouble with Jest.

Can anyone see if this issue still occurring in v8?

@Git-on-my-level
Copy link

Using my above repo but updating mathjs to 8.0.1 still results in the same error

@josdejong
Copy link
Owner

Thanks for checking 👍

We'll keep this issue open and have to do more digging then. Anyone able to help?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants