SyntaxError:与酶一起运行摩卡时出现意外的保留字 [英] SyntaxError: Unexpected reserved word on running mocha with enzyme

查看:131
本文介绍了SyntaxError:与酶一起运行摩卡时出现意外的保留字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用摩卡咖啡进行酶测试时.我收到错误

On running the enzyme test by using mocha. I am getting the error

(function(exports,require,module,__filename,__dirname){导入来回 ^^^^^^

(function (exports, require, module, __filename, __dirname) { import React fro ^^^^^^

SyntaxError:意外的保留字

SyntaxError: Unexpected reserved word

我的测试文件中包含以下给出的测试脚本

I have the below-given test script in my test file

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 代替 import ,但是没有用.如果有人伸出援助之手找出问题,那就太好了.

I have tried to replace import with require, did not work. It would be wonderful if someone give a helping hand to figure out the issue.

编辑 我在下面包含了package.json文件,

EDIT I am including the package.json file below,

{
"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"
}

推荐答案

您的测试使用ES2015,为此您需要一个类似 Babel的编译器.

Your test uses ES2015, for which you require a transpiler like Babel.

要与Mocha一起使用,您首先需要安装一些软件包:

To get this working with Mocha, you first need to install a few packages:

$ npm install --save-dev babel-core babel-preset-es2015 babel-preset-react

接下来,将以下内容添加到您的package.json:

Next, add the following to your package.json:

"babel": {
  "presets": [ "es2015", "react" ]
}

或者,如果没有package.json,则在工作目录中创建一个名为.babelrc的文件,其中包含以下内容:

Or, if you don't have a package.json, create a file called .babelrc in the working directory, containing the following:

{
  "presets": [ "es2015", "react" ]
}

最后,告诉摩卡使用Babel转译器:

Lastly, tell Mocha to use the Babel transpiler:

$ mocha --compilers js:babel-core/register test.js

这篇关于SyntaxError:与酶一起运行摩卡时出现意外的保留字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆