0

当我使用 ES6 功能import并运行 jest 时,我收到此错误:

SyntaxError: Unexpected token import

它建议

 Here's what you can do:
     • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
     • If you need a custom transformation specify a "transform" option in your config.
     • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.

当我使用 Webpack 时,我不需要转换。但看起来我需要它来使用 Jest。我该怎么做?如何transform在配置中使用选项?

我在 package.json 中与 Jest 相关的所有内容是

"jest": {
        "verbose": true
    },
"scripts": {
        "test": "jest"
    },

4

2 回答 2

0

查看文档Using with webpack 2。您需要配置 babel 以使其工作。

于 2019-03-01T02:31:29.263 回答
0

这得到了解决:

  • 安装babel-jestbabel-preset-es2015

  • 添加.babelrc具有以下预设的文件

{
    "presets": ["es2015"]
}
  • 在 jest 配置中添加以下转换选项
    "jest": {
        "verbose": true,
        "transform": {
            "^.+\\.js$": "babel-jest"
        }
    },
于 2019-03-02T00:29:56.797 回答