Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mocha doesn't recognize dynamic imports

I'm using Mocha to test a react/webpack/babel application that is written in es6 stage-0 syntax, which includes dynamic imports. Webpack compiles it without errors, but mocha throws the following error when reaching a dynamic import syntax:

import('path/to/file').then(...)  
^^^^^^  
SyntaxError: Unexpected token import  

The command that i'm running is:

$ mocha --compilers js:babel-register src/**/*.test.js

And in my .babelrc I have to following:

{
  "presets": ["es2015", "react", "stage-0"]
}
like image 851
Yoav Kadosh Avatar asked Sep 27 '17 10:09

Yoav Kadosh


1 Answers

As @louis has mentioned, this is a node issue. The solution is to add the dynamic-import-node plugin to babel:

{
  "presets": ["es2015", "react", "stage-0"],
  "plugins": ["dynamic-import-node"]
}

Which will add support for the import(...) syntax in node.

like image 70
Yoav Kadosh Avatar answered Oct 29 '22 21:10

Yoav Kadosh