cannot run tests that require d3.js
I am not able to run any test for a component that depends on d3.js. I created a simple test to illustrate.
d3include-test.js
jest.dontMock('../d3include');
var d3include = require('../d3include');
describe('test', function() {
it('test', function() {
});
});
d3include.js
var d3 = require('d3');
module.exports = function() {
d3.scale();
}
When I run the above test I get:
TypeError:
test/unit/core/cursor-test.js: src/d3include.js: node_modules/d3/index.js: node_modules/d3/node_modules/jsdom/lib/jsdom.js: Object [object Object] has no method 'readFileSync'
at node_modules/d3/node_modules/jsdom/lib/jsdom.js:4:30
at Object.runContentWithLocalBindings (node_modules/jest-cli/src/lib/utils.js:317:17)
at Loader._execModule (node_modules/jest-cli/src/HasteModuleLoader/HasteModuleLoader.js:243:9)
at Loader.requireModule (node_modules/jest-cli/src/HasteModuleLoader/HasteModuleLoader.js:887:10)
at Loader.requireModuleOrMock (node_modules/jest-cli/src/HasteModuleLoader/HasteModuleLoader.js:907:17)
at node_modules/d3/index.js:1:83
My guess is there is a conflict between the jsdom version that d3.js includes as a dependency and the jest.js jsdom version. d3.js includes version 0.5.7 of jsdom.
Note:
_I can make my test work if I require('d3/d3') because that is linking only the client side d3.js code, but I need to require('d3') because I have my react code setup to run on server or client._
I'm not able to repro this exact error message, but I do get a slightly different one:
TypeError: d3_issue/__tests__/d3include-test.js: d3_issue/d3include: d3_issue/node_modules/d3/index.js: d3_issue/node_module
s/d3/node_modules/jsdom/lib/jsdom.js: d3_issue/node_modules/d3/node_modules/jsdom/lib/jsdom/level3/index.js: d3_issue/node_modules/d3/node_modules/jsdom/lib/jsdom/l
evel3/core.js: d3_issue/node_modules/d3/node_modules/jsdom/lib/jsdom/level2/core.js: Cannot read property 'length' of undefined
at Object.firstChild ( d3_issue/node_modules/d3/node_modules/jsdom/lib/jsdom/level1/core.js:435:28)
at _getMetadata (jest-cli/src/lib/moduleMocker.js:279:49)
at _getMetadata (jest-cli/src/lib/moduleMocker.js:286:23)
at _getMetadata (jest-cli/src/lib/moduleMocker.js:279:27)
at _getMetadata (jest-cli/src/lib/moduleMocker.js:279:27)
at _getMetadata (jest-cli/src/lib/moduleMocker.js:279:27)
at _getMetadata (jest-cli/src/lib/moduleMocker.js:279:27)
at Object.module.exports.getMetadata (jest-cli/src/lib/moduleMocker.js:388:20)
at Loader._generateMock (jest-cli/src/HasteModuleLoader/HasteModuleLoader.js:280:56)
at Loader.requireMock (jest-cli/src/HasteModuleLoader/HasteModuleLoader.js:790:43)
at Loader.requireModuleOrMock (jest-cli/src/HasteModuleLoader/HasteModuleLoader.js:905:17)
at d3_issue/node_modules/d3/node_modules/jsdom/lib/jsdom/level2/core.js:1:99
[...]
In my case, the stack trace suggests that jest is attempting to generate an automock for node_modules/d3/node_modules/jsdom/lib/jsdom/level1/core.js (when it's require()'d on line 1 in level2/core.js) -- which is definitely the wrong behavior considering that that file's path is inside <rootDir>/node_modules/ -- which is the default entry for config.unmockedModulePathPatterns.
(Note that you can tell that jest has decided to mock because the stack goes from Loader.requireModuleOrMock to Loader.requireMock instead of to Loader.requireModule)
I should mention that I've been super swamped with other work lately and haven't had a ton of time to devote to jest issues -- so apologies in advance for any delays here. It might be a bit before I can dig into this too much, but if you'd want to try and debug a bit before I can get a chance, you might start with the logic in the Loader.requireModuleOrMock method (inside src/HasteModuleLoader/HasteModuleLoader.js) and see if you can figure out why jest thinks it should mock that module.
More specifically, for your particular repro, it seems the fs object being returned by require('fs') inside jsdom.js doesn't have a readFileSync property -- which is obviously not supposed to happen. You might start by digging in to see what is being returned by require('fs') in that file?
I get the same error you described above @jeffmo . Adding d3 to unmocked modules then yields the readFileSync error. I logged fs and got this:
{"parse":"[Function: ]","resolve":"[Function: ]","resolveObject":"[Function: ]","format":"[Function: ]","Url":"[Function: ]"}
That's for URL so seems streams are getting crossed somewhere.
This should be fixed once https://github.com/facebook/jest/pull/122 is merged
I'd like to draw extra attention to @smussell's last comment: if you don't need to run d3 server-side, you can work around this bug by changing require('d3') to require('d3/d3').
@danvk Indeed, I missed it but a colleague of min caught it. Maybe @smussell can put it in bold just for future reference.
Just want to comment that, with respect to d3 specifically, this is no longer an issue. As of v3.5.4 it relies on jsdom only as a dev dependency. Does not resolve issues related to other modules, obviously.
Thank you for reporting this issue and appreciate your patience. We've notified the core team for an update on this issue. We're looking for a response within the next 30 days or the issue may be closed.
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.