Jest 27 errors with `ReferenceError: setImmediate is not defined`

See original GitHub issue

Problem

While upgrading to 27.0.3 of jest, I have failing integration tests because

 ● Test suite failed to run

    ReferenceError: setImmediate is not defined

      at Parser.destroy (../../../node_modules/@prisma/client/runtime/index.js:25498:7)
      at detachSocket (../../../node_modules/@prisma/client/runtime/index.js:25536:21)
      at Socket.onSocketClose (../../../node_modules/@prisma/client/runtime/index.js:25545:5)

Suggested solution

Because setImmediate is a non-standard API in the first place, and only officially supported by ie10, prisma should remove references to it.

Sorry if this is the wrong report time, I was split between a feature request and a bug.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:23 (1 by maintainers)

github_iconTop GitHub Comments

34reactions
fatso83commented, Feb 15, 2022

If you are actually using setImmediate in your tests, but your test environment is actually jsdom, the cleanest approach is to simply import the dependency. Just add this at the top:

/**
 * @jest-environment jsdom
 */

import {setImmediate} from 'timers'
25reactions
dtostes-melicommented, Jan 5, 2022

I had the same issue after updating to jest v27. For those who could not solve with any of the previous responses (like me). After some research I’ve solved with:

jest.config.js

module.exports = {
  ...
  setupFilesAfterEnv: [
    '<rootDir>/jest.env.js',
  ],
  ...
}

jest.env.js

global.setImmediate = jest.useRealTimers;
Read more comments on GitHub >

github_iconTop Results From Across the Web

Jest Test Error: browserType.launch: setImmediate is not ...
I just added the polyfill.js in my test class : import 'core-js'; after that setImmediate was defined again and works now as intended....
Read more >
spyon is not defined jest | The AI Search Engine You Control
ReferenceError : spyOn is not defined. So changed it to jest.spyOn but now I am getting the error: Property 'and' does not exist...
Read more >
Jest 27: New Defaults for Jest, 2021 edition
We introduced a few more small breaking changes to help you avoid mistakes by disallowing some things that can easily happen unintentionally:.
Read more >
How to resolve the Jest test error 'browsertype.launch - Quora
launch: setimmediate is not defined" while using Playwright (JavaScript, Jest.js, Playwright, development)?.
Read more >
jest-environment-jsdom | Yarn - Package Manager
Jest will set process.env.NODE_ENV to 'test' if it's not set to something else. You can use that in your configuration to conditionally setup...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found