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

"cannot read property ref of null" error when module.exports === undefined #14

Closed
jeffmo opened this issue May 15, 2014 · 3 comments
Closed

Comments

@jeffmo
Copy link
Contributor

jeffmo commented May 15, 2014

It only happens every so often, but it's super confusing and hard to debug when it does:

If ever a module were to somehow clobber it's exports object with something that isn't an object, you get a nasty error like this:

TypeError: Cannot read property 'ref' of null

at mocks.js:285:11
at visit (mocks.js:275:5)
at removeUnusedRefs (mocks.js:284:3)
at Object.module.exports.getMetadata (mocks.js:364:5)
at mock-modules.js:153:16
at mock-modules.js:128:22
at _generateMock (mock-modules.js:162:20)
at _System._loadAutoMock (mock-modules.js:199:51)
at _System._moduleRequire (mock-modules.js:313:17)

Let's add an assertion earlier on to ensure that the error message is at least helpful.

A common scenario where people hit this is when they're trying to test a react component that look something like this:

var React = require('React');

module.exports = React.createClass({
  // ...
});

In this case, if you don't explicitly mark the React module as unmocked, module.exports is assigned the return value of React.createClass() -- which is a mock function, and thus returns undefined.

@jeffmo jeffmo changed the title Jest throws a really confusing error when module.exports === undefined "cannot read property ref of null" error when module.exports === undefined May 28, 2014
@ccheever
Copy link

I've also run into this problem with a different module (not React). It's really confusing and unclear how to fix it.

ccheever@charless-mbp:~/projects/crayon$npm test

> crayon@2.1.0 test /Users/ccheever/projects/crayon
> jest

Found 1 matching tests...
 FAIL  __tests__/crayon-test.coffee (0.022s)
● crayon › it Can be called like `crayon.red`
  - TypeError: /Users/ccheever/projects/crayon/crayon.js: Cannot read property 'ref' of null

@jeffmo
Copy link
Contributor Author

jeffmo commented Jun 1, 2014

I just committed 3e5ddee (and published v0.1.15) which should make the module mocker more accurately pass through undefined and null exports values to the mocked version of the exports.

I also considered making jest throw with an error that suggests that, when undefined is encountered as an exports object, you should refer to this issue as it's likely that you're doing something like:

var createClass = require('createClass');
module.exports = createClass({ /*...*/ });

But I suppose this could false positive in the extremely rare case where someone, for some reason, intentionally (or non-erroneously) set their exports object to undefined...in which case the error message could be even more misleading.

I'm not necessarily unwilling to do this -- but I do think errors of this type should surface a more debuggable stack trace now with error messages more along the lines of TypeError: undefined is not a function. I do wonder if this will lead to people finding the source of the problem in a more reasonable way.

If this continues to be a source of pain and we keep getting questions about this, I'm willing to re-consider short-circuiting intentional module.exports = undefined scenarios in favor of a detailed explanation of why you might need to unmock something.

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 14, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants