尝试运行Jest测试React代码的示例(来自PRAW documentation),我收到以下错误:
> @ test /home/aizquier/jest/examples/react
> jest
Using Jest CLI v0.7.1
FAIL __tests__/CheckboxWithLabel-test.js
● Runtime Error
SyntaxError: /home/aizquier/jest/examples/react/__tests__/CheckboxWithLabel-test.js: Unexpected token (15:6)
npm ERR! Test failed. See above for more details.
测试在文件CheckboxWithLabel-test.js的第15行失败:
jest.dontMock('../CheckboxWithLabel');
import React from 'react';
import ReactDOM from 'react-dom';
import TestUtils from 'react-addons-test-utils';
const CheckboxWithLabel = require('../CheckboxWithLabel');
describe('CheckboxWithLabel', function() {
it('changes the text after click', function() {
// Render a checkbox with label in the document
var checkbox = TestUtils.renderIntoDocument(
<CheckboxWithLabel labelOn="On" labelOff="Off" />
);
var checkboxNode = ReactDOM.findDOMNode(checkbox);
// Verify that it's Off by default
expect(checkboxNode.textContent).toEqual('Off');
// Simulate a click and verify that it is now On
TestUtils.Simulate.change(TestUtils.findRenderedDOMComponentWithTag(checkbox, 'input'));
expect(checkboxNode.textContent).toEqual('On');
});
});
显然处理.jsx的预处理器无效。 package.json如下:
{
"dependencies": {
"react": "~0.14.0",
"react-dom": "~0.14.0"
},
"devDependencies": {
"react-addons-test-utils": "~0.14.0",
"babel-jest": "*",
"jest-cli": "*"
},
"scripts": {
"test": "jest"
},
"jest": {
"scriptPreprocessor": "./node_modules/babel-jest",
"unmockedModulePathPatterns": [
"./node_modules/react",
"./node_modules/react-dom",
"./node_modules/react-addons-test-utils",
"./node_modules/fbjs"
]
}
}
我的节点是v4.2.2,npm是3.3.12。
任何想法??
答案 0 :(得分:6)
我遇到了同样的问题。看起来这是由最近发布的Babel 6引起的问题。它是tracked here,希望很快就能添加一个修复程序!
与此同时,您可以在babel-jest
中返回早期版本的package.json
。例如,尝试^5.3.0
。
答案 1 :(得分:2)
如果您使用的是Babel 6,则没有默认转换,因此您需要添加一些预设。不确定你是否可以在&#39; package.json&#39;但我添加了&#39; .babelrc&#39;文件与&#39;反应&#39;和&#39; es2015&#39;预设并对其进行排序。见http://babeljs.io/docs/plugins/preset-react/