Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
voinik opened this issue Nov 5, 2021 · 4 comments

Comments

@voinik
Copy link

voinik commented Nov 5, 2021

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

@jeppester
Copy link

I had this same problem. After much troubleshooting it turned out that I used an old version of jest.

Jest started supporting AbortController with jestjs/jest#11182 which is included from version 27.0.0

@stale
Copy link

stale bot commented Jan 9, 2022

This issue has been automatically marked as stale because it has not had any recent activity. It will be closed in 5 days if no further activity occurs.

@stale stale bot added the stale label Jan 9, 2022
@jaw187
Copy link

jaw187 commented Mar 31, 2022

thank you @jeppester

@benjie
Copy link

benjie commented Oct 30, 2023

If, for reasons I won't go into, you happen to be stuck on Jest 26, a workaround for this is to create your own Jest environment:

// __tests__/nodeEnvironment.js
const NodeEnvironment = require("jest-environment-node");

class CustomEnvironment extends NodeEnvironment {
  constructor(config, context) {
    super(config, context);
    this.global.AbortController = global.AbortController;
  }
}

module.exports = CustomEnvironment;
// jest.config.js
module.exports = {
  /* ... */
  testEnvironment: "./__tests__/nodeEnvironment.js",
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants