Jest 报错:“SyntaxError: Unexpected token export"; [英] Jest gives an error: "SyntaxError: Unexpected token export"

查看:73
本文介绍了Jest 报错:“SyntaxError: Unexpected token export";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Jest 来测试我的 React 应用程序.

I'm using Jest to test my React app.

最近,我在我的应用中添加了 DeckGL.我的测试失败并出现此错误:

Recently, I added DeckGL to my app. My tests fail with this error:

Test suite failed to run

/my_project/node_modules/deck.gl/src/react/index.js:21
export {default as DeckGL} from './deckgl';
^^^^^^

SyntaxError: Unexpected token export

  at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/script_transformer.js:318:17)
  at Object.<anonymous> (node_modules/deck.gl/dist/react/deckgl.js:9:14)
  at Object.<anonymous> (node_modules/deck.gl/dist/react/index.js:7:15)

这看起来像是 Jest 在运行测试之前转换节点模块的问题.

This looks like an issue with Jest transforming a node module before running it's tests.

这是我的 .babelrc:

Here is my .babelrc:

{
  "presets": ["react", "es2015", "stage-1"]
}

这是我的笑话设置:

"jest": {
    "testURL": "http://localhost",
    "setupFiles": [
      "./test/jestsetup.js"
    ],
    "snapshotSerializers": [
      "<rootDir>/node_modules/enzyme-to-json/serializer"
    ],
    "moduleDirectories": [
      "node_modules",
      "/src"
    ],
    "moduleNameMapper": {
      "\.(css|scss)$": "<rootDir>/test/EmptyModule.js"
    }
  },

我似乎拥有转换export {default as DeckGL } 所需的正确东西.那么任何想法出了什么问题?

I seem to have the correct things necessary to transform export {default as DeckGL }. So any ideas whats going wrong?

推荐答案

这意味着,文件不会通过 TypeScript 编译器进行转换,例如因为它是带有TS语法的JS文件,或者作为未编译的源文件发布到npm.这是您可以执行的操作.

This means, that a file is not transformed through TypeScript compiler, e.g. because it is a JS file with TS syntax, or it is published to npm as uncompiled source files. Here's what you can do.

调整您的 transformIgnorePatterns 允许列表:

Adjust your transformIgnorePatterns allowed list:

{
  "jest": {
    "transformIgnorePatterns": [
      "node_modules/(?!@ngrx|(?!deck.gl)|ng-dynamic)"
    ]
  }
}

默认情况下,Jest 不会转换 node_modules,因为它们应该是有效的 JavaScript 文件.然而,图书馆作者碰巧假设您将编译他们的源代码.所以你必须明确地告诉 Jest.上面的代码段意味着@ngrx、deck 和 ng-dynamic 将被转换,即使它们是 node_modules.

By default Jest doesn't transform node_modules, because they should be valid JavaScript files. However, it happens that library authors assume that you'll compile their sources. So you have to tell this to Jest explicitly. Above snippet means that @ngrx, deck and ng-dynamic will be transforemed, even though they're node_modules.

这篇关于Jest 报错:“SyntaxError: Unexpected token export";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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