最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Dynamic imports in Jest produce: SyntaxError: Unexpected token import - Stack Overflow

programmeradmin2浏览0评论

Can't figure out how to resolve these errors yet, Jest is plaining 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:

Can't figure out how to resolve these errors yet, Jest is plaining 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./mxstbr/react-boilerplate/pull/1358

Share Improve this question edited Dec 17, 2016 at 9:45 Dattaya asked Dec 16, 2016 at 21:57 DattayaDattaya 93911 silver badges15 bronze badges 4
  • what does your .babelrc looks like? – Andreas Köberle Commented Dec 17, 2016 at 14:36
  • It looks like this: github./Dattaya/react-boilerplate/blob/… – Dattaya Commented Dec 17, 2016 at 14:40
  • 1 Have you tried to use the same plugins for prod and test. Seems like babel cant pile the stuff with your babel env.test settings – Andreas Köberle 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. – Dattaya Commented Dec 17, 2016 at 20:52
Add a ment  | 

2 Answers 2

Reset to default 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

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

发布评论

评论列表(0)

  1. 暂无评论