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

Add options object with ignore property to modern useFakeTimers function #11661

Closed
wants to merge 3 commits into from

Conversation

jschoubben
Copy link

Summary

Since version 27, the "modern" useFakeTimers implementation became the default implementation. Unfortunately, this way doesn't support the async/await pattern. This is caused because the process.nextTick function is mocked which causes the async tests to timeout.
Issue can be found here: #10221

Test plan

@codecov-commenter
Copy link

Codecov Report

Merging #11661 (569028a) into master (fdc74af) will increase coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master   #11661   +/-   ##
=======================================
  Coverage   69.01%   69.01%           
=======================================
  Files         312      312           
  Lines       16335    16336    +1     
  Branches     4734     4734           
=======================================
+ Hits        11273    11274    +1     
  Misses       5034     5034           
  Partials       28       28           
Impacted Files Coverage Δ
packages/jest-fake-timers/src/modernFakeTimers.ts 86.20% <100.00%> (+0.24%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update fdc74af...569028a. Read the comment docs.

@danny-does-stuff
Copy link

Any chance of this one getting merged? Seems pretty straightforward, and non-breaking. One suggestion would be to add documentation about the new options arg

Copy link
Member

@SimenB SimenB left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR, and sorry about the wait!


This needs an integration test showing it works from jest.useFakeTimers, not just a unit test within @jest/fake-timers.

At that point you'll see this doesn't work as jest.useFakeTimers takes a string (modern or legacy) as its first arg - you'll need to makes some changes here: https://github.com/facebook/jest/blob/7dd17d541bcdb4d17d96b53586949fb195294040/packages/jest-runtime/src/index.ts#L1904-L1912

This also needs to work when config is passed, not just configured at runtime (although runtime should override config)

It also needs docs 🙂

Comment on lines +103 to +104
// This is a simple attempt to fix the issue when using fakeTimers in combination with promises
// Developers can now add the nextTick function to the list timers not to mock
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// This is a simple attempt to fix the issue when using fakeTimers in combination with promises
// Developers can now add the nextTick function to the list timers not to mock

this text is for a PR description, not the code 🙂

const toFake = Object.keys(this._fakeTimers.timers).filter(
// This is a simple attempt to fix the issue when using fakeTimers in combination with promises
// Developers can now add the nextTick function to the list timers not to mock
timerFunc => !(options?.ignore || []).includes(timerFunc),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is sorta hard to read, what about

diff --git i/packages/jest-fake-timers/src/modernFakeTimers.ts w/packages/jest-fake-timers/src/modernFakeTimers.ts
index 59c4c49321..049982792b 100644
--- i/packages/jest-fake-timers/src/modernFakeTimers.ts
+++ w/packages/jest-fake-timers/src/modernFakeTimers.ts
@@ -12,6 +12,12 @@ import {
 } from '@sinonjs/fake-timers';
 import {StackTraceConfig, formatStackTrace} from 'jest-message-util';
 
+type ValidTimers = keyof FakeTimerWithContext['timers'];
+
+interface ModernFakeTimersOptions {
+  ignore?: Array<ValidTimers>;
+}
+
 export default class FakeTimers {
   private _clock!: InstalledClock;
   private _config: StackTraceConfig;
@@ -93,16 +99,20 @@ export default class FakeTimers {
     }
   }
 
-  useFakeTimers(): void {
+  useFakeTimers(options?: ModernFakeTimersOptions): void {
     if (!this._fakingTime) {
-      const toFake = Object.keys(this._fakeTimers.timers) as Array<
-        keyof FakeTimerWithContext['timers']
-      >;
+      const toFake = new Set(
+        Object.keys(this._fakeTimers.timers) as Array<ValidTimers>,
+      );
+
+      options?.ignore?.forEach(timer => {
+        toFake.delete(timer);
+      });
 
       this._clock = this._fakeTimers.install({
         loopLimit: this._maxLoops,
         now: Date.now(),
-        toFake,
+        toFake: Array.from(toFake),
       });
 
       this._fakingTime = true;

@SimenB
Copy link
Member

SimenB commented Feb 24, 2022

@jschoubben ping 🙂

@StanleySathler
Copy link

It would be awesome to get this merged. What if someone from the core team handle it since it's partly done? 😃

@SimenB SimenB closed this in #12572 Apr 5, 2022
@SimenB
Copy link
Member

SimenB commented Apr 5, 2022

@github-actions
Copy link

github-actions bot commented May 6, 2022

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 6, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants