我正在尝试使用 mocha 和酶来更新我的单元测试。我正在测试的代码在 ES6 中,使用 JSX 和 React。
在我的测试脚本中,我无法让 mocha 在 JSX 上不出错。
测试脚本:
import React from 'react';
import assert from 'assert';
import { shallow } from 'enzyme';
import SamplePageMain from '../SamplePageMain';
describe('<SamplePageMain />', () => {
var samplePage = shallow(<SamplePageMain />);
it('should render', function () {
assert.notEqual(samplePage, null);
});
});
gulpfile.js:
require('babel-core/register');
...
gulp.task('test', function() {
return gulp.src('scripts/**/test/*.js', {read: false})
.pipe(mocha());
});
输出是:
gulp test
[16:19:06] Using gulpfile ~/dev/bikini/gulpfile.js
[16:19:06] Starting 'test'...
[16:19:06] 'test' errored after 62 ms
[16:19:06] SyntaxError in plugin 'gulp-mocha'
Message:
/Users/me/dev/bikini/scripts/components/test/samplePageMain.js: Unexpected token (9:26)
Details:
pos: 206
loc: [object Object]
_babel: true
codeFrame: 7 |
8 |
> 9 | var samplePage = shallow(<SamplePageMain />);
| ^
10 |
11 | it('should render', function () {
12 | assert.notEqual(samplePage, null);
Stack:
SyntaxError: /Users/me/dev/bikini/scripts/components/test/samplePageMain.js: Unexpected token (9:26)
7 |
8 |
> 9 | var samplePage = shallow(<SamplePageMain />);
| ^
10 |
11 | it('should render', function () {
12 | assert.notEqual(samplePage, null);
at Parser.pp.raise (/Users/me/dev/bikini/node_modules/babel- register/node_modules/babel-core/node_modules/babylon/index.js:1425:13)
at Parser.pp.unexpected (/Users/me/dev/bikini/node_modules/babel- register/node_modules/babel-core/node_modules/babylon/index.js:2907:8)
at Parser.pp.parseExprAtom (/Users/me/dev/bikini/node_modules/babel-register/node_modules/babel- core/node_modules/babylon/index.js:754:12)
at Parser.pp.parseExprSubscripts (/Users/me/dev/bikini/node_modules/babel-register/node_modules/babel- core/node_modules/babylon/index.js:509:19)
at Parser.pp.parseMaybeUnary (/Users/me/dev/bikini/node_modules/babel-register/node_modules/babel- core/node_modules/babylon/index.js:489:19)
at Parser.pp.parseExprOps (/Users/me/dev/bikini/node_modules/babel-register/node_modules/babel- core/node_modules/babylon/index.js:420:19)
at Parser.pp.parseMaybeConditional (/Users/me/dev/bikini/node_modules/babel-register/node_modules/babel- core/node_modules/babylon/index.js:402:19)
at Parser.pp.parseMaybeAssign (/Users/me/dev/bikini/node_modules/babel-register/node_modules/babel- core/node_modules/babylon/index.js:365:19)
at Parser.pp.parseExprListItem (/Users/me/dev/bikini/node_modules/babel-register/node_modules/babel- core/node_modules/babylon/index.js:1232:16)
at Parser.pp.parseCallExpressionArguments (/Users/me/dev/bikini/node_modules/babel-register/node_modules/babel- core/node_modules/babylon/index.js:585:20)
我通过 browserify 运行源代码并将其放入测试目录中以证明它不是 mocha/enzyme 本身,从而成功运行了测试。我的问题只是试图让 gulp 魔法正确。