Jest: Test fails

Created on 13 May 2019  ·  5Comments  ·  Source: facebook/jest

🐛 Bug Report

All of my tests fails all of a sudden.

The error is quite vague and low-level. I am not sure exactly what should I do and what it means...

I have tried: - installed jest globally - uninstalling jest globally and running it locally (jest 24.8.0) - changed my node version lower and upper (currently using 10, tried 8, and it was working previously with 10)

describe('Test', () => {

    test('That simple', () => {
        expect(true).toEqual(true);
    });
});


I have a file that sets up the environment for my tests like this:

/**
 * Defines the React 16 Adapter for Enzyme. 
 *
 * @link http://airbnb.io/enzyme/docs/installation/#working-with-react-16
 * @copyright 2017 Airbnb, Inc.
 */
// const enzyme = require("enzyme");
// const Adapter = require("enzyme-adapter-react-16");

// enzyme.configure({ adapter: new Adapter() });

// setup file
import { configure } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';

configure({ adapter: new Adapter() });

Below is the error that I get...

  ● Test suite failed to run

    SyntaxError: Unexpected token m in JSON at position 0
        at JSON.parse (<anonymous>)

      3 |  *
      4 |  * @link http://airbnb.io/enzyme/docs/installation/#working-with-react-16
    > 5 |  * @copyright 2017 Airbnb, Inc.
        |               ^
      6 |  */
      7 | // const enzyme = require("enzyme");
      8 | // const Adapter = require("enzyme-adapter-react-16");

      at Runtime._loadModule (node_modules/jest-runtime/build/index.js:568:59)
      at Object.<anonymous> (node_modules/entities/lib/decode_codepoint.js:1:106)
      at Object.<anonymous> (node_modules/htmlparser2/lib/Tokenizer.js:3:23)
      at Object.<anonymous> (node_modules/htmlparser2/lib/Parser.js:1:106)
      at Object.<anonymous> (node_modules/htmlparser2/lib/index.js:1:103)
      at Object.<anonymous> (node_modules/cheerio/lib/parse.js:4:18)
      at Object.<anonymous> (node_modules/cheerio/lib/cheerio.js:5:13)
      at Object.<anonymous> (node_modules/cheerio/index.js:5:28)
      at Object.<anonymous> (node_modules/enzyme/build/ReactWrapper.js:15:16)
      at Object.<anonymous> (node_modules/enzyme/build/index.js:3:21)
      at Object.<anonymous> (src/test-setup.js:5:15)

To Reproduce

I am not sure? but my environment is:


npx: installed 1 in 1.505s

  System:
    OS: macOS High Sierra 10.13.6
    CPU: (8) x64 Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz
  Binaries:
    Node: 8.11.3 - ~/.nvm/versions/node/v8.11.3/bin/node
    Yarn: 1.12.1 - /usr/local/bin/yarn
    npm: 5.10.0 - ~/Documents/workspace/ISR/mylotto-web-mobile/node_modules/.bin/npm
  npmPackages:
    jest: 24.7.1 => 24.7.1

Expected behavior

Test to run normally and pass..

Bug Report Needs Repro Needs Triage

Most helpful comment

I faced the same issue but after hours of googling around in vain, the following change fixed it.

"transformIgnorePatterns": [
  "node_modules/(?!(babel-jest)/)"
]

So, it seems babel-jest was ignored all the time. But, I'm still surprised that this was never required because everything was working and tests were passing with "jest": "^24.7.1" and "babel-jest": "^24.7.1". Find my full configurations here.

Hope this helps!

All 5 comments

Looks like something is trying to parse JavaScript as JSON. Update to the latest Jest, it's hard to support you on an older version. Give us the stack trace on the latest Jest and your configuration file, especially any transformers you have in the config.

Actually I found where is the root cause to this problem....

As I mentioned in the description, I have a file for setting up the test environment like this :


//-------> jest.config.js
{
    "setupFiles": [
        "<rootDir>/src/test-setup.js"
    ],
}

//------>  test-setup.js

/**
 * Defines the React 16 Adapter for Enzyme. 
 *
 * @link http://airbnb.io/enzyme/docs/installation/#working-with-react-16
 * @copyright 2017 Airbnb, Inc.
 */
// const enzyme = require("enzyme");
// const Adapter = require("enzyme-adapter-react-16");

// enzyme.configure({ adapter: new Adapter() });

// setup file
import { configure } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';

configure({ adapter: new Adapter() });

This file is causing that problem.

When I comment out everything in the set-up file as below:

/**
 * Defines the React 16 Adapter for Enzyme. 
 *
 * @link http://airbnb.io/enzyme/docs/installation/#working-with-react-16
 * @copyright 2017 Airbnb, Inc.
 */
// const enzyme = require("enzyme");
// const Adapter = require("enzyme-adapter-react-16");

// enzyme.configure({ adapter: new Adapter() });

// setup file
// import { configure } from 'enzyme';
// import Adapter from 'enzyme-adapter-react-16';

// configure({ adapter: new Adapter() });

it works 😭, but it doesn't solve the problem.

More update on this:

I am still facing this issue and my jest.config.js is as follow (only imported things that I think are releavant):

I don't know why I get error or better say: syntax error like SyntaxError: Unexpected token m in JSON at position 0 at JSON.parse (<anonymous>)

I thought it's because jest does not transpile the test-setup.js file but even when I use ES5, or when I import the simple test-setup.js script into my own test to get transpiled, it is still not working

    "setupFiles": [
        "<rootDir>/src/test-setup.js"
    ],
    "transform": {
        ".+\\.(js|jsx|ts|tsx)$": "<rootDir>/node_modules/babel-jest",
        ".+\\.css$": "<rootDir>/config/jest/cssTransform.js",
        "(?!.*\\.(js|jsx|ts|tsx|css|json)$)": "<rootDir>/config/jest/fileTransform.js"
    },

I faced the same issue but after hours of googling around in vain, the following change fixed it.

"transformIgnorePatterns": [
  "node_modules/(?!(babel-jest)/)"
]

So, it seems babel-jest was ignored all the time. But, I'm still surprised that this was never required because everything was working and tests were passing with "jest": "^24.7.1" and "babel-jest": "^24.7.1". Find my full configurations here.

Hope this helps!

@nunisa This is exactly why i'm here, we also upgraded jest and all of a sudden we need to use
transformIgnorePatterns

Was this page helpful?
0 / 5 - 0 ratings