Jest是否支持ES6导入/导出? [英] Does Jest support ES6 import/export?

查看:765
本文介绍了Jest是否支持ES6导入/导出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我使用ES6中的import/export,那么我所有的笑话测试都会失败并显示错误:

If I use import/export from ES6 then all my jest tests fail with error:

意外的保留字

Unexpected reserved word

我将要测试的对象转换为使用旧式IIFY语法,突然我的测试通过了.或者,采用更简单的测试用例:

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

相同错误.显然这里的导入/导出有问题.为了使测试框架满意,使用ES5语法重写代码对我来说是不切实际的.

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.

我有通天的笑话.我从github问题中尝试了各种建议.到目前为止没有.

I have babel-jest. I tried various suggestions from github issues. No go so far.

 "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"]
}

是否有此解决方法?

推荐答案

来自我的答案关于另一个问题,这可能会更简单:

From my answer on another question, this can be simpler:

唯一的要求是将您的test环境配置为Babel,并添加es2015转换插件:

The only requirement is to config your test environment to Babel, and add the es2015 transform plugin:

第1步:

test环境添加到项目根目录中的.babelrc:

Add your test environment to .babelrc in the root of your project:

{
  "env": {
    "test": {
      "plugins": ["transform-es2015-modules-commonjs"]
    }
  }
}

第2步:

安装es2015转换插件:

Install the es2015 transform plugin:

npm install --save-dev babel-plugin-transform-es2015-modules-commonjs


就是这样. Jest将自动启用从ES模块到CommonJS的编译,而不必在package.json中的jest属性中通知其他选项.


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.

这篇关于Jest是否支持ES6导入/导出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆