这是我在package.json文件中的jest configuration:
"jest": {
"automock": false,
"browser": true,
"moduleNameMapper": {
"\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "./app/tests/mocks/FileMock.js",
"\\.(css|less)$": "identity-obj-proxy"
},
"moduleFileExtensions": [
"js",
"jsx"
],
"moduleDirectories": [
"node_modules"
],
"transform": {
"^.+\\.jsx?$": "./node_modules/babel-jest",
"\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "./app/tests/mocks/FileTransformer.js"
},
"testEnvironment": "jsdom",
"testPathDirs": [
"./app/tests"
],
"testRegex": ".*.test.js",
"verbose": true
}
.babelrc文件位于我的根文件夹中:
{
"plugins": ["syntax-dynamic-import", "transform-runtime"],
"presets": [
[
"es2015",
{
"modules": false
}
],
"react",
"stage-0"
],
"env": {
"start": {
"presets": [
"react-hmre"
]
}
}
}
根据在开玩笑getting started page中找到的文件,这是我所需要的一切,让宝贝能够发挥它的魔力。
无论如何,这个测试:
import React from 'react';
import {shallow} from 'enzyme';
import Landing from '../components/Landing.component';
describe('<Landing/>', () => {
it('should render a header to the page', () => {
const landing = shallow(<Landing/>);
expect(landing.find('h1').text()).toBe('This is the Landing component');
});
});
返回:
FAIL app/tests/Landing.component.test.js
● Test suite failed to run
/Users/shooshte/PersonalProjects/surviveJS/app/tests/Landing.component.test.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import React from 'react';
^^^^^^
SyntaxError: Unexpected token import
at transformAndBuildScript (node_modules/jest-runtime/build/transform.js:320:12)
我做错了什么?
答案 0 :(得分:36)
Jest将env变量设置为test,所以我必须将我的预设添加到.babelrc中的env设置中:
{
"plugins": ["syntax-dynamic-import", "transform-runtime"],
"presets": [
[
"es2015",
{
"modules": false
}
],
"react",
"stage-0"
],
"env": {
"start": {
"presets": [
"react-hmre"
]
},
"test": {
"presets": ["es2015", "react", "stage-0"]
}
}
}
答案 1 :(得分:3)
每年的预设仅汇编当年批准的内容。 babel-preset-env取代
es2015
,es2016
,es2017
,latest
基于此,在最新配置中,您必须使用/替换es2015
和任何esX
的插件/预设到新的env
。
babel-preset-env
安装npm install
。.babelrc
中,您应该相应更新:{
"presets": [
"env",
"stage-0",
"react-native"
],
"plugins": ...
}
有关Babel plugins official Documentation的更多信息。
☝️请记住,数组中插件/预设的顺序很重要。
答案 2 :(得分:3)
你需要安装babel-jest。我遇到了同样的问题。
转到你的app目录,添加babel-jest
祝你好运!答案 3 :(得分:2)
在我的情况下,我有以下.babelrc
配置:
{
"presets": [
["env", { "modules": false }],
"react",
"stage-2"
],
"plugins": [
"transform-runtime",
"transform-class-properties",
"react-hot-loader/babel"
]
}
即使指定了babel-env
,我仍然遇到错误。为了解决这个问题,我必须删除&#34; modules&#34;:false 标记。
答案 4 :(得分:0)
以下.babelrc适合我(没有添加):
\0
package.json的{
"presets": [["env", {
"debug": false,
"modules": false
}], "es2015", "stage-0", "react"],
"plugins": [
"react-hot-loader/babel",
"syntax-dynamic-import",
"dynamic-import-node",
"transform-class-properties",
"transform-decorators-legacy"
]
}
答案 5 :(得分:0)
Jest无法处理导入,因此需要一个转换插件,这就是为什么我必须添加该插件的原因:
babel-plugin-dynamic-import-node
并更新我的babel设置,告诉玩笑者使用此插件正确转换代码:
"env": {
"test": {
"plugins" : ["dynamic-import-node"]
}
}