使用摩卡进行酶测试。我收到了错误
(function(exports,require,module,__ filename,__ dirname){import React from ^^^^^^
SyntaxError:意外的保留字
我在测试文件中有以下给定的测试脚本
import React from 'react';
import { assert } from 'chai';
import { render } from 'enzyme';
import Book from '../src/Book';
describe("Book", () => {
it("render text", () => {
const wrapper = render(<Book author="Dan Abramov" title="Redux and ReactJS" />);
assert.equal(wrapper.text(), 'Redux and ReactJS by Dan Abramov');
});
});
我已尝试将导入替换为 require ,但无效。如果有人伸出援助之手来解决这个问题,那就太好了。
修改 我在下面包含了package.json文件,
{
"name": "test-client",
"version": "0.0.1",
"description": "test companies client.",
"repository": "https://github.com/Test/test-client",
"main": "js/app.js",
"directories": {
"test": "test"
},
"scripts": {
"test": "mocha"
},
"dependencies": {
"classnames": "2.2.*",
"clone": "^1.0.2",
"es5-shim": "4.1.*",
"es6-object-assign": "1.0.*",
"es6-promise": "3.0.*",
"flux": "^2.1.1",
"font-awesome": "^4.5.0",
"form-data": "^1.0.0-rc3",
"hashids": "^1.0.2",
"howler": "^1.1.29",
"isomorphic-fetch": "^2.2.1",
"js-cookie": "^2.1.0",
"json3": "3.3.*",
"keymirror": "0.1.*",
"normalize.css": "^4.0.0",
"react": "^15.0.1",
"react-addons-create-fragment": "^15.0.1",
"react-dnd": "^2.1.4",
"react-dnd-html5-backend": "^2.1.2",
"react-dom": "^15.0.1",
"react-infinite": "^0.9.2",
"react-paginate": "^1.0.4",
"react-tooltip": "^2.0.0",
"socket.io-client": "1.3.*",
"twemoji": "^2.0.5",
"unorm": "^1.4.1"
},
"devDependencies": {
"browserify": "^6.2.0",
"catw": "^1.0.1",
"dockerode": "^2.0.4",
"dotenv": "^2.0.0",
"envify": "^3.4.0",
"gulp-awspublish": "^3.0.2",
"less": "^2.6.1",
"reactify": "^0.15.2",
"watchify": "^3.4.0"
},
"browserify": {
"transform": [
"reactify",
"envify"
]
},
"scripts": {
"start-css": "catw -c 'lessc -' 'css/src/*.less' -o css/bundle.css -v",
"start-js": "watchify -o js/bundle.js -v -d js/app.js",
"start": "npm run start-css & npm run start-js"
},
"author": "Test"
}
答案 0 :(得分:6)
您的测试使用ES2015,您需要像Babel这样的转录程序。
要使用Mocha,首先需要安装一些软件包:
$ npm install --save-dev babel-core babel-preset-es2015 babel-preset-react
接下来,将以下内容添加到package.json
:
"babel": {
"presets": [ "es2015", "react" ]
}
或者,如果您没有package.json
,请在工作目录中创建一个名为.babelrc
的文件,其中包含以下内容:
{
"presets": [ "es2015", "react" ]
}
最后,告诉Mocha使用Babel转换器:
$ mocha --compilers js:babel-core/register test.js