I have some react project that uses d3.js v6.7.0 and I am able to test it with jest 27.2.0 on windows 10.
Part of my settings in jest.config.js:
testEnvironment: "jsdom",
testRunner: "jest-jasmine2",
transform: {
'^.+\\.jsx?$': 'babel-jest',
},
transformIgnorePatterns: [
"\\\\node_modules\\\\"
],
Part of my settings in webpack.config.js:
module: {
rules: [
{
test: /\.(jsx|js)$/,
exclude: /node_modules/,
loader: 'babel-loader',
},
If I upgrade d3.js to version 7.0.1, which uses ES6 modules, I get following error when running my tests:
Test suite failed to run
Jest encountered an unexpected token
Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.
Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.
By default "node_modules" folder is ignored by transformers.
Here's what you can do:
• If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it.
• To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
• If you need a custom transformation specify a "transform" option in your config.
• If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.
You'll find more details and examples of these config options in the docs:
https://jestjs.io/docs/configuration
For information about custom transformations, see:
https://jestjs.io/docs/code-transformation
Details:
C:\python_env\workspace\visualization\frontEnd\node_modules\d3\src\index.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){export * from "d3-array";
=> How should I adapt my configurations to get my tests running again?
a) I tried to enable experimental node features with adapted webpack test script and cross-env:
cross-env NODE_OPTIONS=--experimental-vm-modules jest
b) I tried to adapt the jest transformIgnorePatterns
transformIgnorePatterns: [
"/node_modules/(?!(d3.*))"
],
c) I tried to include node_modules/d3 for the webpack babel-loader
However, I did not find a working configuration, yet.
Related:
https://github.com/nrwl/nx/issues/812
https://github.com/webpack/webpack/issues/2031
I had the exact same problem and was able to fix (or rather work around) this by configuring a moduleNameMapper
for d3
. I didn't have anything sensible to map it to, so I just created a file called test/d3.js
that only contained a comment explaining what it was used for.
The configuration ended up looking like this in jest.config.js
:
moduleNameMapper: {
"^d3$": "<rootDir>/test/d3.js"
},