[Bug]: Unexpected token 'export' when Jest test imports d3 (7.1.1)
See original GitHub issueVersion
27.3.1
Steps to reproduce
We use Typescript, if our test file (*.spec.ts) contains the following import
import { scaleLinear } from 'd3';
Expected behavior
Test runs without problems
Actual behavior
Running the test will give the following error:
.../node_modules/d3/src/index.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){export * from "d3-array";
^^^^^^
SyntaxError: Unexpected token 'export'
Additional context
The issue started when updating Jest from 26.6.3
to 27.3.1
.
I finally found a workaround for this. I first found this Jest issue https://github.com/facebook/jest/issues/2550 it mentioned setting up transformIgnorePatterns
and adding "allowJs": true
to our tsconfig.json
. This did not work, what did solve it in the end was adding the following to our jest.config.js
:
moduleNameMapper: {
'd3': '<rootDir>/node_modules/d3/dist/d3.min.js',
},
Which makes it import from that minified file instead of node_modules/d3/src/index.js
. Not sure if it is reasonable and possible for Jest to import from a module’s dist folder when it cannot import from the index file? Also not sure if this is an issue with d3 or a mismatch with all the different ways in which you can express JavaScript (ESM/CommonJS/…)?
Environment
System:
OS: Linux 5.11 Ubuntu 21.04 (Hirsute Hippo)
CPU: (16) x64 Intel(R) Core(TM) i9-10885H CPU @ 2.40GHz
Binaries:
Node: 14.16.1 - /tmp/fnm_multishells/13239_1636009783566/bin/node
npm: 6.14.12 - /tmp/fnm_multishells/13239_1636009783566/bin/npm
npmPackages:
jest: 27.3.1 => 27.3.1
Issue Analytics
- State:
- Created 2 years ago
- Reactions:4
- Comments:9
Top GitHub Comments
@istvandesign did the workaround I ended up with work for you?
so adding this to your jest config:
I had to use the
transformIgnorePatterns
in one of my projects…I had to do this with multiple modules.