Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code splitting `import` breaks Jest tests

I'm using the code splitting feature of webpack, but it seems that jest doesn't recognize the import() function:

        import('myModule').then(function (myModule) {
        ^^^^^^
SyntaxError: Unexpected token import

I don't have any special setup. My npm test script is simply run jest "test": "jest"

How can I make it work?

I'm using the latest version of jest 20.0.4 and babel-jest 20.0.3

like image 992
CodinCat Avatar asked Jun 20 '17 03:06

CodinCat


1 Answers

Oh I just found the answer.

Simply install this plugin: https://github.com/airbnb/babel-plugin-dynamic-import-node and add it to the .babelrc file:

{
  ...
  "env": {
    "test": {
      "plugins": ["dynamic-import-node"]
    }
  }
}
like image 124
CodinCat Avatar answered Oct 06 '22 19:10

CodinCat