Does Jest support ES6 import/export?

我只是一个虾纸丫 提交于 2019-11-28 22:54:41

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;

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!