Mocha无法识别JSX

我正在尝试通过使用摩卡咖啡和酶来更新单元测试。我正在测试的代码是在ES6中,使用JSX和React。

测试脚本:

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本身,从而成功运行了该测试。我的问题只是试图正确解决大口魔术。

回答:

对于Babel 6的用户来说,这是一个非常普遍的问题,Babel 6本身(babel-core)什么也没做。它要求在转换过程中将转换/插件提供给它。

Babel提供了许多常用插件作为预设。常见的阵营项目babel-preset-2015babel-preset-reactbabel-

preset-stage-0。在npm安装它们之后,添加一个.babelrc看起来像这样的配置文件:

{

"presets": ["react", "es2015", "stage-0"]

}

对于带有gulp签出的mocha,此堆栈gulp-mocha如何通过编译器标志?。

并在此处阅读有关一般设置Babel 6的信息https://babeljs.io/blog/2015/10/31/setting-up-

babel-6

以上是 Mocha无法识别JSX 的全部内容, 来源链接: utcz.com/qa/397821.html

回到顶部