23

I am following the Jest tutorial to test a react component and am running into preprocessing issues with my jsx. I assume the error is due to preprocessing, the error message is not very helpful. Googling shows similar errors with older versions of react/jest that were fixed by including the /** @jsx React.DOM */ docblock which as far as I can tell was fixed.

When I run my test:

Using Jest CLI v0.8.0, jasmine1
 FAIL  spec/MyComponent_spec.js
Runtime Error
SyntaxError: /Users/asdf/react/stuff-react/spec/MyComponent_spec.js: Unexpected token (13:6)
npm ERR! Test failed.  See above for more details.

The line in question is the one that should be rendering my component:

jest.dontMock('../src/MyComponent');

let React = require('react');
let ReactDOM = require('react-dom');
let TestUtils = require('react-addons-test-utils');

const MyComponent = require('../src/MyComponent');

describe('MyComponent', function(){
  it('render', function(){

    var myComponent = TestUtils.renderIntoDocument(
      // This is the line referenced in the test error
      <MyComponent />
    )
    var myComponentNode = ReactDOM.findDOMNode(myComponent);

    expect(myComponentNode.textContent).toEqual('hi');
  });
});

I thought my package.json was responsible for telling jest to preprocess that file?

 "scripts": {
    "test": "jest"
  },
  "jest": {
    "testDirectoryName": "spec",
    "scriptPreprocessor": "<rootDir>/node_modules/babel-jest",
    "unmockedModulePathPatterns": [
      "<rootDir>/node_modules/react",
      "<rootDir>/node_modules/react-dom",
      "<rootDir>/node_modules/react-addons-test-utils",
      "<rootDir>/node_modules/fbjs"
    ]
  },

My component:

import React from 'react';

class MyComponent extends React.Component({
  render () {
    return (
      <div>
        hi
      </div>
    )
  }
});

export default MyComponent;
Cihan Keser
  • 3,190
  • 4
  • 30
  • 43
user2936314
  • 1,734
  • 4
  • 18
  • 32

5 Answers5

12

Using a .bablerc file in the project root directory fixed it for me.

I was not using a .babelrc file while developing because I defined my presets in the webpack configuration file. But it turns out that when you run the unit test with jest, then jest is not aware of this presets as it does not know about webpack. So simply adding a .babelrc file with the presets should solve the issue for you too.

Contents of .babelrc:

{ "presets": ["es2015", "react"] }

2

I had a similar problem, and the solution was adding this to jest config file:

"transform": {
      "^.+\\.js$": "babel-jest",
      "^.+\\.jsx$": "babel-jest"  // This line was missing
}

The reason it was needed in our project, is because we overridden the default "transform" value in jest config file.

Ofer Segev
  • 5,094
  • 2
  • 22
  • 22
1

I think you may just need to add the testFileExtensions and testFileExtensions to the jest section of your package.json.

See the README.md of babel-jest:

https://github.com/babel/babel-jest

Matt Holland
  • 2,190
  • 3
  • 22
  • 26
  • thanks, i do have `testFileExtensions` and `moduleFileExtensions`with es6 in there, but it still throws with the same error – user2936314 Nov 28 '15 at 04:31
  • 1
    I found a similar question here: http://stackoverflow.com/questions/28870296/how-to-use-jest-with-webpack - does that help at all? – Matt Holland Nov 30 '15 at 19:55
0

Changing my .babelrc config file to babel.config.js or babel.config.json worked for me because Jest ignores .babelrc.

protoEvangelion
  • 4,355
  • 2
  • 29
  • 38
0

Add ["@babel/preset-react", { runtime: "automatic" }] to the presets in babel.config.js.

Your babel.config.js file should look something like:

module.exports = {
  presets: [
    ["@babel/preset-env", { targets: { node: "current" } }],
    "@babel/preset-typescript",
    ["@babel/preset-react", { runtime: "automatic" }],
  ],
};

And you need to set your jest environment to jsdom by adding this to the top of your test (make sure you've installed the jest-environment-jsdom node package):

/**
 * @jest-environment jsdom
 */
Dan Sterrett
  • 1,160
  • 11
  • 12