【问题标题】:ES6 import doesnt work in jest even after babel config, throws SyntaxError: Unexpected token export即使在 babel 配置之后,ES6 导入也无法正常工作,抛出 SyntaxError: Unexpected token export
【发布时间】:2019-01-14 13:00:48
【问题描述】:

添加依赖

package.json

"devDependencies": {
"babel-core": "^7.0.0-0",
"babel-jest": "^23.4.2",
"babel-preset-env": "^1.7.0",
"jest": "^23.4.2",
"jest-cli": "^23.4.2",
"regenerator-runtime": "^0.12.1"
}

.babelrc

 {
 "presets": ["env"]
 }

仍然,在运行测试 SyntaxError: Unexpected token export is generated

【问题讨论】:

  • 请在堆栈跟踪中包含完整的错误消息。消息本身信息不足。
  • 这里是堆栈跟踪,Jest 遇到了一个意外的令牌 这通常意味着您正在尝试导入 Jest 无法解析的文件,例如它不是纯 JavaScript。出口{dom}; ^^^^^^ 默认情况下,如果 Jest 看到 Babel 配置,它将使用它来转换您的文件,忽略“node_modules”。
  • 请将其包含在问题中,而不是作为评论。此外,这仍然不包括堆栈跟踪,这只是错误消息。通常跟踪是消息之后的所有行,引用行号和文件名。

标签: ecmascript-6 jestjs babeljs


【解决方案1】:

具有export { dom }; 的文件的位置是什么?

env 预设应该转换 export 语法,除非该文件位于您的 node_modules 中,就像错误消息所暗示的那样。

如果它在 node_modules 内,那么您需要相应地调整您的 jest.config 文件:

{ "jest": { "transformIgnorePatterns": [ "node_modules/(?!(name-of-module-that-is-exporting-dom)/)" ] } }

更多信息可以在这里找到:https://jestjs.io/docs/en/configuration.html#transformignorepatterns-array-string

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-08-05
    • 2017-12-08
    • 2017-12-30
    • 1970-01-01
    • 2020-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多