-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
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
[Bug]: Jest fails wrong test case #14068
Comments
Did you try upgrading Jest? v26 is rather old. It can be that it simply does not support dynamic I can’t reproduce the issue after upgrading By the way, reproduction repo has few "unmet peer dependency" errors including:
|
@mrazauskas I've just updated Jest to v29.5.0 (by create new project with nestjs v9.4.0): commit |
I took a second look. First time I was running tests with Seems like with Might be I miss something, but it sounds like this is hardly possible to be improved. |
@mrazauskas thank you for the response. |
For now, I've resolved this by store all the timers. and clear it after each test case // timers.ts
const _setInterval = global.setInterval;
const _setTimeout = global.setTimeout;
const _setImmediate = global.setImmediate;
const INTERVALS: (NodeJS.Timeout & number)[] = [];
const TIMEOUTS: (NodeJS.Timeout & number)[] = [];
const IMMEDIATES: (NodeJS.Timeout & number)[] = [];
global.setInterval = function (): NodeJS.Timeout {
const interval = _setInterval(...arguments);
INTERVALS.push(interval);
return interval;
};
/**
* didn't work with promisify(setTimeout)
* @returns
*/
global.setTimeout = function (): NodeJS.Timeout {
const timeout = _setTimeout(...arguments);
TIMEOUTS.push(timeout);
return timeout;
};
global.setImmediate = function (): NodeJS.Immediate {
const immediate = _setImmediate(...arguments);
IMMEDIATES.push(immediate);
return immediate;
};
export const clearAllTimers = () => {
INTERVALS.forEach((i) => clearInterval(i));
TIMEOUTS.forEach((i) => clearTimeout(i));
IMMEDIATES.forEach((i) => clearImmediate(i));
}; setupFilesAfterEnv file import { clearAllTimers } from './timers';
afterAll(() => {
clearAllTimers();
}); |
This could work. I though your problem was related with dynamic import. |
It was related to dynamic import. But the root cause is throwing an error on a callback of Asynchronous resource (specifically here is Timer of setTimeout) after a test file has been torn down while another test file is running. |
By the way, jest.useFakeTimers({ advanceTimers: true });
afterAll(() => {
jest.clearAllTimers();
}); The |
Work like a charm. Thank you |
This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days. |
This issue was closed because it has been stalled for 30 days with no activity. Please open a new issue if the issue is still relevant, linking to this one. |
This issue was closed because it has been stalled for 30 days with no activity. Please open a new issue if the issue is still relevant, linking to this one. |
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Version
26.0.1
Steps to reproduce
Example steps:
Or you can check the result here https://github.com/linhx/jest-import-after-torn-down/actions/runs/4686813490/jobs/8305329437
Expected behavior
no test cases failed
Actual behavior
Test case of
src/test/bnothertest.spec.ts
was failedAdditional context
I have a Nestjs project which used quite a lot of dependencies. Some of the dependencies import a module and try to use that module after the Jest environment has been torn down. An error occurs:
That causes one of the test case failed, even that test case should not be failed.
I reproduce the issue by create this. In the
src/test/app.controller.spec.ts
, I import a module which has an dynamic import and use a module with a timeout to make the test imports a module after Jest env torn down.I set the timeout so that it will run while src/test/bnothertest.spec.ts is running. When the error occurs, jest fails
src/test/bnothertest.spec.ts
.This error happens if an error throw from a timer of previous test, not about dynamic import. E.g. this causes the error too:
The result:
Environment
The text was updated successfully, but these errors were encountered: