question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Bug]: Unexpected token 'export' when Jest test imports d3 (7.1.1)

See original GitHub issue

Version

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:closed
  • Created 2 years ago
  • Reactions:4
  • Comments:9

github_iconTop GitHub Comments

20reactions
maistrotoadcommented, Nov 25, 2021

@istvandesign did the workaround I ended up with work for you?

so adding this to your jest config:

moduleNameMapper: {
    'd3': '<rootDir>/node_modules/d3/dist/d3.min.js',
},
8reactions
tschleusscommented, Nov 29, 2021

I had to use the transformIgnorePatterns in one of my projects…

"transformIgnorePatterns": [
    "/node_modules/(?!d3|d3-array|internmap|delaunator|robust-predicates)"
],

I had to do this with multiple modules.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Jest Unexpected token 'export' when using d3 - Stack Overflow
A quick fix is to use the minified d3 build, which is already transpiled. Either import the minified build directly: import * as...
Read more >
nx jest unexpected token 'export' - You.com | The AI Search ...
Problem. When I run jest, on some test it will throw an error about failure to import .js files with export in them....
Read more >
Jest SyntaxError: Unexpected token 'export'
Probably it was caused by hurrying up writing a component and autocompleting an import with an IDE help. For Webpack it is fine...
Read more >
TypeScript Jest: Unexpected Token Export - Reddit
Ok so now your test project is using common JS, but node sees your dependency as an ES Module, so it throws an...
Read more >
Jest With Amcharts4 In Reactjs - ADocLib
Geeting error : Unexpected token export while running tests in Jest with components having ... Unexpected token 'export' when Jest test imports d3...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found