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 27 errors with ReferenceError: setImmediate is not defined
#8558
Comments
Version
|
Hi @janpio Sorry, 27.0.3 of jest 14.17.4 I didn't realize it was built into node, I'll have to investigate why jest 27 is breaking that |
For anyone else getting here from google or whatever, there are lots of changes regarding setImmediate in the jest changelog for 27 https://github.com/facebook/jest/blob/master/CHANGELOG.md I'm still investigating, but I suspect jestjs/jest#11222 might have to do with it. obviously make sure your test env is set to node. (ours is, and we still get the problem 🤷♂️ ). I'll update this issue when I figure it out. EDIT: Just an update as to my investigation on this. I never figured it out, I think updating to an even newer jest version than 27.0.3 may have fixed it, but I don't have access to the codebase where this was a problem anymore. So I won't be able to investigate this. |
ReferenceError: setImmediate is not defined
@ericwooley @janpio I've seen this error as well. Downgrading from Jest The error that was being thrown was:
And in the generated runtime client, the code that calls destroy() {
clearTimeout(this.timeout);
this.timeout = null;
this.unconsume();
setImmediate((self) => self.close(), this);
} Downgrading Jest hid this error, likely due to Jest overriding Using This is likely a Jest issue, but not sure if there's any advice on how to avoid triggering this. |
Same problem here, using {
"@types/jest": "26.0.14",
"jest": "26.6.3",
"ts-jest": "26.5.6"
} |
Just an update as to my investigation on this. I never figured it out, I think updating to an even newer jest version than 27.0.3 may have fixed it, but I don't have access to the codebase where this was a problem anymore. So I won't be able to investigate this. |
Some problem, using Playwright |
By the way, if using Jest, make sure your env is set to |
@TomSssM: that's a very strange response. Jest 26 worked fine (as you noticed), but Jest 27 fails, even if you set the test environment to jsdom, It's curious to me that Jest 26 works (with the default jsdom environment), I wonder what has changed in the Jest 27 jsdom implementation? |
@davidmoshal my theory was that env |
@TomSssM , you're missing the point. |
@davidmoshal I already understood your point and I know that switching from Unfortunately I don't know how to help you out, I would roll jest back to 26 and hope somebody does a fix in response to this issue. Yup, it is often difficult to migrate to a new major of some library, we have all been there |
Thanks |
When I put /*
* @jest-environment node
*/ above my import statements it fixed it for me, since it was a dependency that was using setImmediate. It didn't work when I put it below the statements. |
I had the same issue after updating to
module.exports = {
...
setupFilesAfterEnv: [
'<rootDir>/jest.env.js',
],
...
}
global.setImmediate = jest.useRealTimers; |
@davidmoshal what kind of code are you testing that needs both DOM and Node environment to exist? Maybe you can work around the problem somehow, so if you post the code someone might be able to help. The way I see it, the fact that Jest 26 supported |
@silvenon is probably right here. I chose to replace |
If you are actually using /**
* @jest-environment jsdom
*/
import {setImmediate} from 'timers' |
I am afraid I don't see how this would have worked for anyone
TLDR: What worked for me is
For those interested https://nodejs.dev/learn/understanding-setimmediate |
@jaimeagudo You are just replacing a missing setImmediate with a call to setTimeout. Did you try just importing the setImmediate timer? |
The import statement does get rid of the error ' ReferenceError: setImmediate is not defined'
how do you force setImmediate to process with Jest v27 ? The runAllImmediates() works in v26 |
@nabeelfarid My comment was just actually referring to fixing the issue about setImmediate not being defined in the test, not in any implementation code. Importing it will just bring it into the scope of the test and no implementation code will know about it and it will not be added to the global scope. Jest only stubs the bits in the global scope, so if you want Jest to do anything about Still, I do not think your example makes much sense anyhow: you only test the effects of what is happening inside your test module. This does not prove anything about code imported from outside your test module.
P.S. I do not even know what Prisma is and just happened to see this from some other thread, so unsubscribing. |
If you're using TypeScript, the following worked for me: global.setImmediate = jest.useRealTimers as unknown as typeof setImmediate; |
For me setting testEnvironment to |
Following @tobilg's solution, setting |
Such a banger workaround |
note: this worked for me, and the bug is in
|
Some updates on this issue:
What is the most recent version of Prisma and Node.js where you people have detected this issue? Moreover, I see several proposed solutions above that seem to have been appreciated by the community (in particular, setting the |
This is still happening. |
@robbporto Can you provide some more information on the versions and exactly what you are doing so we can reproduce this, please? |
@jkomyno have you tried it with the |
This is the only correct solution for me. the other one ( |
This is what worked for me (react typescript app), add it inside the test file, socketio was causing the error for me //@ts-ignore
window.setImmediate = window.setTimeout |
I didn't use setimmediate in my code but in the error, it came from node_modules. install in jest.setup.ts |
Problem
While upgrading to 27.0.3 of jest, I have failing integration tests because
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.
The text was updated successfully, but these errors were encountered: