Does Jest support ES6 import/export?
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 IIFE 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. It is no go so far.
File package.json
"scripts": {
"start": "webpack-dev-server",
"test": "jest"
},
"jest": {
"testPathDirs": [
"__tests__"
],
"testPathIgnorePatterns": [
"/node_modules/"
],
"testFileExtensions": ["es6", "js"],
"moduleFileExtensions": ["js", "json", "es6"]
},
File babelrc
{
"presets": ["es2015", "react"],
"plugins": ["transform-decorators-legacy"]
}
Is there a fix for this?
Solution 1:
From my answer to another question, this can be simpler:
The only requirement is to configure your test
environment to Babel, and add the ECMAScript 6 transform plugin:
Step 1:
Add your test
environment to .babelrc
in the root of your project:
{
"env": {
"test": {
"plugins": ["@babel/plugin-transform-modules-commonjs"]
}
}
}
Step 2:
Install the ECMAScript 6 transform plugin:
npm install --save-dev @babel/plugin-transform-modules-commonjs
And that's it. Jest will enable compilation from ECMAScript modules to CommonJS automatically, without having to inform additional options to your jest
property inside package.json
.
Solution 2:
UPDATE 2020 - native support of ECMAScript modules (ESM)
According to this issue, there is native support of ESM from [email protected]
. So you won't have to use babel anymore. At the time of writing this answer (05/2020), to activate that you need to do three simple things:
- Make sure you don't transform away
import
statements by settingtransform: {}
in config file - Run
node@^12.16.0 || >=13.2.0
with--experimental-vm-modules
flag - Run your test with
jest-environment-node
orjest-environment-jsdom-sixteen
.
So your Jest configuration file should contain at least this:
export default {
testEnvironment: 'jest-environment-node',
transform: {}
...
};
And to set --experimental-vm-modules
flag, you will have to run Jest as follows:
node --experimental-vm-modules node_modules/jest/bin/jest.js
Also note in the Github issue that this approach does not yet support the jest
object. So you may need to import it manually:
import {jest} from '@jest/globals'
(I hope this will change in the future)
Solution 3:
For an updated configuration, I'm using https://babeljs.io/setup#installation
Select JEST and be happy:
As a reference, the current configuration:
npm install --save-dev babel-jest
In your package.json file, make the following changes:
{
"scripts": {
"test": "jest"
},
"jest": {
"transform": {
"^.+\\.jsx?$": "babel-jest"
}
}
}
Install babel preset:
npm install @babel/preset-env --save-dev
Create a .babelrc
file:
{
"presets": ["@babel/preset-env"]
}
Run your tests:
npm run test
Solution 4:
In package.json
, kindly set like this one: "test": "node --experimental-vm-modules node_modules/.bin/jest"
Should be good!
Solution 5:
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"]
}