jest: Test suite failed to run, SyntaxError: Unexpected token import
jest: Test suite failed to run, SyntaxError: Unexpected token import
这是 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这就是我需要的一切,让 babel 发挥它的魔力。
无论如何,本次测试:
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');
});
});
returns:
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)
我做错了什么?
Jest 将 env 变量设置为测试,因此我必须将我的预设添加到 .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"]
}
}
}
Each yearly preset only compiles what was ratified in that year. babel-preset-env replaces es2015
, es2016
, es2017
, latest
基于此,根据最新配置,您必须 use/replace es2015
的 Plugins/Preset 和任何 esX
到新配置:env
。
- 您必须安装
babel-preset-env
和 npm install
。
- 在您的
.babelrc
中,您应该进行相应更新:
{
"presets": [
"env",
"stage-0",
"react-native"
],
"plugins": ...
}
有关 Babel plugins official Documentation 的更多信息。
☝️ 记住数组中 plugins/preset 的顺序很重要。
就我而言,我有以下 .babelrc
配置:
{
"presets": [
["env", { "modules": false }],
"react",
"stage-2"
],
"plugins": [
"transform-runtime",
"transform-class-properties",
"react-hot-loader/babel"
]
}
即使指定了 babel-env
,我仍然遇到错误。要修复它,我必须删除 "modules": false 标志。
您需要安装babel-jest。我遇到了同样的问题。
进入你的应用目录,yarn add babel-jest
祝你好运!
以下 .babelrc 对我有用(没有添加):
{
"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"
]
}
"devDependencies" package.json 部分如下所示:
...
"devDependencies": {
"babel-cli": "latest",
"babel-core": "^6.26.3",
"babel-eslint": "^8.2.3",
"babel-jest": "^22.4.4",
"babel-loader": "latest",
"babel-plugin-dynamic-import-node": "^1.2.0",
"babel-plugin-lodash": "latest",
"babel-plugin-syntax-dynamic-import": "^6.18.0",
"babel-plugin-transform-class-properties": "^6.24.1",
"babel-plugin-transform-decorators-legacy": "latest",
"babel-plugin-transform-dynamic-import": "^2.0.0",
"babel-plugin-transform-flow-strip-types": "^6.22.0",
"babel-plugin-transform-object-rest-spread": "latest",
"babel-polyfill": "^6.26.0",
"babel-preset-env": "^1.7.0",
"babel-preset-flow": "^6.23.0",
"babel-preset-react": "^6.24.1",
"babel-preset-react-app-babel-7": "^4.0.1",
"babel-preset-stage-0": "^6.24.1",
...
Jest 不处理导入,因此它需要一个转换插件,这就是我必须添加该插件的原因:
babel-plugin-dynamic-import-node
并更新我的 babel 设置以告诉 jest 使用此插件正确转换代码:
"env": {
"test": {
"plugins" : ["dynamic-import-node"]
}
}
这是 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这就是我需要的一切,让 babel 发挥它的魔力。
无论如何,本次测试:
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');
});
});
returns:
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)
我做错了什么?
Jest 将 env 变量设置为测试,因此我必须将我的预设添加到 .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"]
}
}
}
Each yearly preset only compiles what was ratified in that year. babel-preset-env replaces
es2015
,es2016
,es2017
,latest
基于此,根据最新配置,您必须 use/replace es2015
的 Plugins/Preset 和任何 esX
到新配置:env
。
- 您必须安装
babel-preset-env
和npm install
。 - 在您的
.babelrc
中,您应该进行相应更新:
{
"presets": [
"env",
"stage-0",
"react-native"
],
"plugins": ...
}
有关 Babel plugins official Documentation 的更多信息。
☝️ 记住数组中 plugins/preset 的顺序很重要。
就我而言,我有以下 .babelrc
配置:
{
"presets": [
["env", { "modules": false }],
"react",
"stage-2"
],
"plugins": [
"transform-runtime",
"transform-class-properties",
"react-hot-loader/babel"
]
}
即使指定了 babel-env
,我仍然遇到错误。要修复它,我必须删除 "modules": false 标志。
您需要安装babel-jest。我遇到了同样的问题。
进入你的应用目录,yarn add babel-jest
祝你好运!
以下 .babelrc 对我有用(没有添加):
{
"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"
]
}
"devDependencies" package.json 部分如下所示:
...
"devDependencies": {
"babel-cli": "latest",
"babel-core": "^6.26.3",
"babel-eslint": "^8.2.3",
"babel-jest": "^22.4.4",
"babel-loader": "latest",
"babel-plugin-dynamic-import-node": "^1.2.0",
"babel-plugin-lodash": "latest",
"babel-plugin-syntax-dynamic-import": "^6.18.0",
"babel-plugin-transform-class-properties": "^6.24.1",
"babel-plugin-transform-decorators-legacy": "latest",
"babel-plugin-transform-dynamic-import": "^2.0.0",
"babel-plugin-transform-flow-strip-types": "^6.22.0",
"babel-plugin-transform-object-rest-spread": "latest",
"babel-polyfill": "^6.26.0",
"babel-preset-env": "^1.7.0",
"babel-preset-flow": "^6.23.0",
"babel-preset-react": "^6.24.1",
"babel-preset-react-app-babel-7": "^4.0.1",
"babel-preset-stage-0": "^6.24.1",
...
Jest 不处理导入,因此它需要一个转换插件,这就是我必须添加该插件的原因:
babel-plugin-dynamic-import-node
并更新我的 babel 设置以告诉 jest 使用此插件正确转换代码:
"env": {
"test": {
"plugins" : ["dynamic-import-node"]
}
}