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.

Babel plugin-syntax-dynamic-import not in effect when running with Jest

See original GitHub issue

My project builds and runs fine with webpack normally. When I run it under Jest, it doesn’t like my dynamic import:

        const ExtensionReducer = await import('../../../reducers/extension');
                                       ^^^^^^
    
    SyntaxError: Unexpected token import

      46 | 
      47 | class LinksContainer extends React.Component {
    > 48 |   constructor (props) {
      49 |     super(props)
      50 | 
      51 |     this.state = {
      
      at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/script_transformer.js:316:17)
      at Object.<anonymous> (src/components/pixel/links/linksContainer.jsx:48:46)
      at Object.<anonymous> (src/components/pixel/firstPixelCreator/firstPixelCreator.jsx:22:46)

.babelrc:

{
  "presets": [[
    "@babel/preset-env", {
      "targets": {
        "chrome": "60"
      }
  }], "@babel/react"],
  "plugins": [
    "@babel/plugin-syntax-dynamic-import",
    "@babel/plugin-proposal-class-properties",
    "@babel/plugin-proposal-decorators",
    "@babel/plugin-proposal-object-rest-spread"]
}

I tried asking #need-help on discord and they didn’t know what was up.

I share the same webpack config with Jest that I use normally to build.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:19

github_iconTop GitHub Comments

78reactions
revmischacommented, Apr 4, 2018

Also I don’t know how people normally figure this insane shit out

72reactions
artzhycommented, Dec 4, 2018

it’s working with babel 7, react, jest for react use

@babel/plugin-syntax-dynamic-import

for jest use

dynamic-import-node

plugins: [
  ...
    '@babel/plugin-syntax-dynamic-import',
 ...
],
  env: {
    test: {
      plugins: ['dynamic-import-node']
    }
  }
Read more comments on GitHub >

github_iconTop Results From Across the Web

Can't test a function with a dynamic import with Jest and babel ...
You use dynamic-import-node plugin twice. I did the same with "presets": [ "react-app", ],.
Read more >
Using with webpack - Jest
Non-Babel JavaScript transformations can be handled with Jest's transform config ... you need to enable the dynamic-import-node plugin.
Read more >
babel-plugin-syntax-dynamic-import does not transpile-Reactjs
Coding example for the question babel-plugin-syntax-dynamic-import does not transpile-Reactjs.
Read more >
Learn Handling Dynamic Imports – Testing React Applications ...
Kent installs a babel plugin to transform dynamic imports to something that can be run in Node that Jest understands.
Read more >
jest encountered an unexpected token import - You.com | The ...
The way I got around it was to install two class babel plugins: ... ,global,jest){import { configure } from "enzyme"; ^ SyntaxError: Unexpected...
Read more >

github_iconTop Related Medium Post

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