Trying to run the example of Jest testing React code (from https://github.com/facebook/jest/tree/master/examples/react ), I get the following error:
> @ 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.
The tests fails at line 15 of file CheckboxWithLabel-test.js:
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');
});
});
apparently the preprocessor to handle .jsx is not working. The package.json is as follows:
{
"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"
]
}
}
My node is v4.2.2 and npm is 3.3.12.
Any ideas??
I was running into the same problem. It looks like this is an issue caused by the recent release of Babel 6. It's being tracked here, and hopefully a fix is added soon!
In the meantime, you can go back to an earlier version of babel-jest
in your package.json
. For example, try ^5.3.0
.
If you are using Babel 6 there are no default transformations so you'll need to add some presets. Not sure if you can do this in 'package.json' but I added the '.babelrc' file with the 'react' and 'es2015' presets and that sorted it. See http://babeljs.io/docs/plugins/preset-react/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With