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.

jest (babel) fails if using ES6 imports

See original GitHub issue

Environment

devDependencies:

"@haul-bundler/babel-preset-react-native": "^0.13.2",
"@haul-bundler/cli": "^0.13.1",
"@haul-bundler/preset-0.60": "^0.13.0",
"jest": "^24.9.0",

Node v12.4.0

Description

After changing babel.config.js preset @haul-bundler/babel-preset-react-native jest tests don’t support import syntax anymore. If switching back to metro-react-native-babel-preset jest works again.

Trying to figure out what setting is triggering this, but not expert in babel configuration.

Getting errors such as

    import fetch, { Headers } from 'node-fetch';
           ^^^^^

    SyntaxError: Unexpected identifier

      at ScriptTransformer._transformAndBuildScript (node_modules/@jest/transform/build/ScriptTransformer.js:537:17)
      at ScriptTransformer.transform (node_modules/@jest/transform/build/ScriptTransformer.js:579:25)

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
zamotanycommented, Oct 11, 2019

@jounii Looks a bit hacky, you can alternatively try this:

env: {
  test: {
    plugins: [['@babel/plugin-transform-modules-commonjs', { allowTopLevelThis: true }]]
  }
}

It should work for every file if it’s being run by Jest, which sets process.env.NODE_ENV to test.

0reactions
zamotanycommented, Oct 11, 2019

In next release of @haul-bundler/babel-preset-react-native CommonJS transform will be added automatically in when NODE_ENV === 'test', like above.

Read more comments on GitHub >

github_iconTop Results From Across the Web

babel-jest doesn't handle ES6 within modules - Stack Overflow
Jest doesn't seem to have a problem using ES6 imports however as soon as it hits on an import statement within one of...
Read more >
Configuring Jest
For example, with the following configuration jest will fail if there is less than 80% branch, line, and function coverage, or if there...
Read more >
cannot use import statement outside a module jest - You.com
Jest doesn't support ES6 module and hence throwing this error when you directly run the test with Jest. if you want to run...
Read more >
Setup and debug Jest to use ES6 imports in Node v12.x.x
Setup Babel to transpile our ES6 .js source code; Configure Babel so that Jest can use it; Setup VSCode to enable easy debugging....
Read more >
Anyone gotten unit tests working with ES6 modules?
But if you really need Babel there are solutions in the forum that have Jest+Babel set ups but you'd likely need to use...
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