7

Can't figure out how to resolve these errors yet, Jest is complaining about dynamic imports like these:

const importModules = Promise.all([
      import('containers/HomePage/reducer'),
      import('containers/HomePage/sagas'),
      import('containers/HomePage'),
    ]);

Error message:

F:\react-boilerplate\app\store.js:49
      import('./reducers').then(function (reducerModule) {
      ^^^^^^
SyntaxError: Unexpected token import

You can find all of the details here: https://github.com/mxstbr/react-boilerplate/pull/1358

4
  • what does your .babelrc looks like? Commented Dec 17, 2016 at 14:36
  • It looks like this: github.com/Dattaya/react-boilerplate/blob/… Commented Dec 17, 2016 at 14:40
  • 1
    Have you tried to use the same plugins for prod and test. Seems like babel cant compile the stuff with your babel env.test settings Commented Dec 17, 2016 at 18:26
  • Thank you @AndreasKöberle for your suggestion, I added babel-plugin-dynamic-import-node plugin to the list of plugins in test environment and the error was resolved for the most parts. I guess dynamic import()s can't be polyfilled so the only choice for node is to transform them to require inside a Promise. Commented Dec 17, 2016 at 20:52

2 Answers 2

7

Installing babel-plugin-dynamic-import-node solved our problem. In addition, make sure that babylon (babel dependency) is at least 6.12.0.

Also, we had a problem with Travis build because Travis cached node_modules and old babylon with it:

$ npm ls babylon
[email protected] /home/travis/build/mxstbr/react-boilerplate
├─┬ [email protected]
│ └── [email protected]
├─┬ [email protected]
│ └── [email protected] 
├─┬ [email protected]
│ └─┬ [email protected]
│   └── [email protected] 

, so we had to turn the cache off with: cache: false

Sign up to request clarification or add additional context in comments.

1 Comment

I had a similar issue where Jest tests would run fine locally (with the @babel/plugin-syntax-dynamic-import plugin) but they would fail during CI. Adding babel-plugin-dynamic-import-node worked (because the CI runs on node). See github.com/facebook/jest/issues/5920#issuecomment-444112504
0

It looks like jest cant compile your files with the babel settings for test in your .babelrc file. You need to have the same settings for prod and test

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.