这是我在package.json文件中的jest配置:
"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" ] } } }
根据在开玩笑的入门页面上找到的文档,这是我需要让babel工作的所有东西.
无论如何,这个测试:
import React from 'react'; import {shallow} from 'enzyme'; import Landing from '../components/Landing.component'; describe('', () => { it('should render a header to the page', () => { const landing = shallow( ); 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.":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变量设置为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"] } } }
每个年度预设仅编译当年批准的内容。巴别预置-ENV内容替换
es2015
,es2016
,es2017
,latest
在此基础上,对最新的配置,就必须使用/更换你的插件/预置es2015
任何esX
新的一个:env
。
您必须安装babel-preset-env
使用npm install
。
在您中,.babelrc
您应该相应地更新:
{
"presets": [
"env",
"stage-0",
"react-native"
],
"plugins": ...
}
有关Babel插件官方文档的更多信息。
?? 请记住,数组中插件/预设的顺序很重要。