Jest: setting testEnvironment to node causes jest functions to be undefined

Created on 8 Feb 2020  ·  13Comments  ·  Source: facebook/jest

🐛 Bug Report

When setting testEnvironment to node and running a test I get ReferenceError: test is not defined. If I remove testEnvironment: 'node' from the config it runs fine. The same applies to describe, beforeAll...etc.

Note: it works fine on node 10.16.0

 FAIL  test/1.test.js
  ● Test suite failed to run

    ReferenceError: test is not defined

    > 1 | test('test1', () => {
        | ^
      2 |   expect(1).toBe(1);
      3 | });

      at Object.<anonymous> (test/1.test.js:1:1)

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        0.333s
Ran all test suites.
npm ERR! Test failed.  See above for more details.

To Reproduce

Steps to reproduce the behavior:

jest.config.js

module.exports = {
  testEnvironment: 'node'
}

test/1.test.js

test('test1', () => {
  expect(1).toBe(1);
});

Expected behavior


Tests should run without errors when testEnvironment is set to node.

Link to repl or repo (highly encouraged)

envinfo

  System:
    OS: macOS 10.15.3
    CPU: (12) x64 Intel(R) Core(TM) i7-8850H CPU @ 2.60GHz
  Binaries:
    Node: 10.14.0 - /usr/local/bin/node
    npm: 6.13.7 - /usr/local/bin/npm
  npmPackages:
    jest: ^25.1.0 => 25.1.0 
Upstream Bug

Most helpful comment

Right, that's a bug in node 10.14.0 and 10.14.1 (possibly earlier as well, didn't check) - 10.14.2 (and newer) works correctly. Fix is https://github.com/nodejs/node/pull/23206.

(duplicate of #9453)

All 13 comments

Jest itself sets it to node in our own tests, so your reproduction is not enough. Can you put together a repository or a repl/codesandbox

Here's a simple test https://github.com/alyhegazy/jest-tests-bug.git
I get the error in the description on node 10.14.0

Right, that's a bug in node 10.14.0 and 10.14.1 (possibly earlier as well, didn't check) - 10.14.2 (and newer) works correctly. Fix is https://github.com/nodejs/node/pull/23206.

(duplicate of #9453)

Damn, I wasted one whole day :|
10.14.2 worked! Thanks a lot!

Tell me about it!

@alyhegazy What you want to know?

Wasted few hours on this. :( Thanks for the fix.

I've published 25.2.2 which has a workaround for this issue on the buggy versions of node

Thanks for this -- had issues using 10.13.0. Seems like jest 26 no longer works with node 10.13, but downgrading to 25.2.2 does. Any way to get 26 to support the buggy versions of node?

@enuchi Jest 26 requires Node >=10.14.2

Precisely because of this issue

Cool. Thanks!

This was such a stupid big and I spent nearly 1 hour exploring it.

Was this page helpful?
0 / 5 - 0 ratings