问题描述
我正在使用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?
推荐答案
这是因为Node.js无法处理ES6模块.
This is because Node.js cannot handle ES6 modules.
因此,您应该将模块转换为CommonJS.
You should transform your modules to CommonJS therefore.
如果您使用Babel 7 =>
If you use Babel 7 =>
安装npm install --save-dev @babel/plugin-transform-modules-commonjs
要仅用于测试用例,请将其添加到.babelrc中,笑话会自动给出NODE_ENV = test全局变量.
And to use only for test cases add to .babelrc,Jest automatically gives NODE_ENV=test global variable.
"env": {
"test": {
"plugins": ["@babel/plugin-transform-modules-commonjs"]
}
}
或者如果您使用Babel 6 =>
or if you use Babel 6 =>
npm install --save-dev babel-plugin-transform-es2015-modules-commonjs
到.babelrc
"env": {
"test": {
"plugins": ["transform-es2015-modules-commonjs"]
}
}
这篇关于笑话给出错误:"SyntaxError:意外的令牌导出";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!