我有一个开玩笑的测试文件 hello.test.js 。这将从另一个文件 robot.js 导入类Robot
。
hello.test.js
import { Robot } from './robot'
.
.
robot.js 从另一个文件导入另一个类x
。
robot.js
import { x } from 'file'
.
.
Jest无法导入x
。我收到以下错误。
● Test suite failed to run
Jest encountered an unexpected token
This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.
By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".
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.
import {
^
SyntaxError: Unexpected token {
我在package.json
中拥有
"@babel/core": "^7.4.5",
"@babel/preset-env": "^7.4.5",
"babel-jest": "^24.8.0",
"jest": "^24.8.0",
"regenerator-runtime": "^0.13.2",
"@babel/plugin-proposal-class-properties": "^7.4.4"
我的jest.config.js
有:
transform: {
'^.+\\.js$': 'babel-jest'
},
这是我的babel.config.js
const presets = [
"@babel/preset-env"
]
const plugins = [
"@babel/plugin-proposal-class-properties"
]
module.exports = {
presets,
plugins
}
import
可以在我使用相同的babel配置运行webpack时找到工作,但是如何使其在玩笑测试中起作用。
感谢您的帮助!