If I use import/export
from ES6 then all my jest tests fail with error:
Unexpected reserved word
I convert my object under test to use old school IIFY syntax and suddenly my tests pass. Or, take an even simpler test case:
var Validation = require('../src/components/validation/validation');//PASS
//import * as Validation from '../src/components/validation/validation'//FAIL
Same error. Obviously there's a problem with import/export here. It's not practical for me to rewrite my code using ES5 syntax just to make my test framework happy.
I have babel-jest. I tried various suggestions from github issues. No go so far.
package.json
"scripts": {
"start": "webpack-dev-server",
"test": "jest"
},
"jest": {
"testPathDirs": [
"__tests__"
],
"testPathIgnorePatterns": [
"/node_modules/"
],
"testFileExtensions": ["es6", "js"],
"moduleFileExtensions": ["js", "json", "es6"]
},
babelrc
{
"presets": ["es2015", "react"],
"plugins": ["transform-decorators-legacy"]
}
Is there a fix for this?
From my answer on another question, this can be simpler:
The only requirement is to config your test
environment to Babel, and add the es2015 transform plugin:
Step 1:
Add your test
environment to .babelrc
in the root of your project:
{
"env": {
"test": {
"plugins": ["transform-es2015-modules-commonjs"]
}
}
}
Step 2:
Install the es2015 transform plugin:
npm install --save-dev babel-plugin-transform-es2015-modules-commonjs
And that's it. Jest will enable compilation from ES modules to CommonJS automatically, without having to inform additional options to your jest
property inside package.json
.
It's a matter of adding stage-0 to your .babelrc file. here is an example:
{
"presets": ["es2015", "react", "stage-0"],
"plugins": ["transform-decorators-legacy"]
}
In addition to installing babel-jest
(which comes with jest by default now) be sure to install regenerator-runtime
.
I solved it with .default
.
Try
var Validation = require('../src/components/validation/validation').default;
来源:https://stackoverflow.com/questions/35756479/does-jest-support-es6-import-export