Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Jest example doesn't work

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??

like image 799
aizquier Avatar asked Feb 08 '23 14:02

aizquier


2 Answers

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.

like image 149
Kevin Cooper Avatar answered Feb 11 '23 20:02

Kevin Cooper


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/

like image 32
Luke Venn Avatar answered Feb 11 '23 21:02

Luke Venn