Skip to content

Jest can't find AbortController in node v16.13.0 when "testEnvironment": "node" is set in package.json #11626

@voinik

Description

@voinik

Description

If you have

"jest": {
    "testEnvironment": "node"
  },

set in your package.json, and you run an npm command with jest in it (e.g. "test": "jest"), you'll get:

ReferenceError: AbortController is not defined

This happens despite AbortController being available in Node v16.13.0.
Running code using AbortController without Jest works fine.

It seems CRA is changing Jest's node environment somehow?

I'm aware that CRA does not allow the "testEnvironment" setting to be set in the package.json when using react-scripts test. I simply did not expect setting the "testEnvironment" setting to its default ("node") to seemingly change the environment.

Environment

CRA v4.0.3
Node v16.13.0
npm v8.1.0

Steps to reproduce

  1. Setup a fresh CRA project
  2. Delete App.test.js
  3. Add 2 new files to src:
// src/abortControllerCode.js
function start() {
    const abortController = new AbortController();
    abortController.abort();
    return 'foo';
}

module.exports = { start }

and

// src/abortControllerCode.test.js
const { start } = require('./abortControllerCode');

describe('AbortController test', () => {
    it('Should not throw', () => {
        expect(start()).toBe('foo');
    })
});
  1. Replace the default test command with: "test": "jest", in your package.json
  2. Add
"jest": {
    "testEnvironment": "node"
  },

To your package.json

  1. Run npm test

I get the following output from that.
ReferenceError

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions