Code splitting `import` breaks Jest tests

2019-02-27 02:25发布

问题:

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

回答1:

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"]
    }
  }
}