-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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]: Calling useFakeTimers does not reset state #12362
Comments
@stephenh Thanks for the report. In the implementation, But $ git --no-pager diff
diff --git a/src/faker-timers.test.ts b/src/faker-timers.test.ts
index 3da723c..c6ebd3d 100644
--- a/src/faker-timers.test.ts
+++ b/src/faker-timers.test.ts
@@ -1,6 +1,7 @@
beforeEach(() => {
console.log("Calling useFakeTimers");
+ jest.useRealTimers()
jest.useFakeTimers("modern")
});
$ npm test src/faker-timers.test.ts
> [email protected] test
> jest "src/faker-timers.test.ts"
console.log
Calling useFakeTimers
at Object.<anonymous> (src/faker-timers.test.ts:3:11)
console.log
Calling useFakeTimers
at Object.<anonymous> (src/faker-timers.test.ts:3:11)
PASS src/faker-timers.test.ts
fake-timers
✓ test 1 (31 ms)
✓ test 2 (2 ms)
Test Suites: 1 passed, 1 total
Tests: 2 passed, 2 total
Snapshots: 0 total
Time: 0.3 s, estimated 1 s I am not sure whether is document is wrong or the code implementation, @SimenB could you help? |
The thought was that "if already faking, do nothing when called". I guess we could throw instead of silently doing nothing, and if what you wanted was a reset, you'd do |
It will break every suite which calls
I've seen many cases in my company's repo which I think it's very reasonable and will not be bitten by timer mock bec the global operation attribute. Maybe we could just fix our document? from:
to
|
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
27.4.7
Steps to reproduce
Expected behavior
The
jest.getTimerCount()
intest 2
should be 0, instead it is 1.Actual behavior
A timer leaked from test 1 to test 2, despite calling
useFakeTimers
, which the docs:https://jestjs.io/docs/timer-mocks
"Additionally, you need to call jest.useFakeTimers() to reset internal counters before each test."
Explicitly suggest that
useFakeTimers
resets internal counters, but it does not.Additional context
No response
Environment
The text was updated successfully, but these errors were encountered: