-
Notifications
You must be signed in to change notification settings - Fork 273
Version 2.x not compatible with jest.useFakeTimers('modern'); #391
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
Comments
Thanks, this is interesting. After a brief investigation, I've identified that this code is to "blame", specifically the react-native-testing-library/src/index.js Lines 12 to 15 in 401d3d3
react-native-testing-library/src/flushMicroTasks.js Lines 13 to 15 in 401d3d3
Interesting thing is that it works fine with legacy timers. The Verified that this works with both legacy and modern timers. @ccfz As a workaround until this is resolved you'll need to skip auto cleanup by setting cc @SimenB ideas on why this scenario regressed? You have more context on the topic. Here's a diff with repro and fix that makes it work (likely will send a PR later): diff --git a/src/__tests__/waitFor.test.js b/src/__tests__/waitFor.test.js
index e38f05c..874c5be 100644
--- a/src/__tests__/waitFor.test.js
+++ b/src/__tests__/waitFor.test.js
@@ -93,3 +93,10 @@ test('works with fake timers', async () => {
jest.useRealTimers();
});
+
+it('should render hello world!', () => {
+ jest.useFakeTimers('modern');
+ const utils = render(<BananaContainer />);
+
+ expect(utils.getByText('Change freshness!')).not.toBeNull();
+});
diff --git a/src/flushMicroTasks.js b/src/flushMicroTasks.js
index c4bb679..37816b7 100644
--- a/src/flushMicroTasks.js
+++ b/src/flushMicroTasks.js
@@ -10,6 +10,22 @@ export default function flushMicrotasksQueue(): Promise<any> {
return flushMicroTasks();
}
+let enqueueTask;
+try {
+ // assuming we're in node, let's try to get node's
+ // version of setImmediate, bypassing fake timers if any.
+ enqueueTask = require('timers').setImmediate;
+} catch (_err) {
+ // we're in a browser, do nothing
+ enqueueTask = (resolve) => {
+ resolve();
+ };
+}
+
export function flushMicroTasks(): Promise<any> {
- return new Promise((resolve) => setImmediate(resolve));
+ return {
+ then(resolve) {
+ enqueueTask(resolve);
+ },
+ };
} |
@thymikee thanks for the very quick response! Workaround works as expected. |
That's because they actually run immediates (and ticks) on real timers even when time is faked: https://github.com/facebook/jest/blob/4471bbbb2d009a5d71c7beb702cb3cadb46d0a94/packages/jest-fake-timers/src/legacyFakeTimers.ts#L443-L451. So even when you say Might make sense to add some sort of |
@SimenB the issue turned out to be using |
Only idea is jestjs/jest#10221 (comment) |
This suggestion seem to work with |
Hi @thymikee I updated to the latest version and removed the workaround, and I am still getting the same
Did my example from above pass for you with the fix implemented?
|
@ccfz @thymikee I'll chime in to say that I am also still struggling with this issue with If it's helpful, then I can try to find time to create a reproduction case for what I am seeing. |
Creating a new issue and a repro would be appreciated! |
Describe the bug
Using
jest.useFakeTimers('modern');
in any spec file, causes the tests to fail due to timeout error:Downgrading to 1.14 fixes this, therefore I believe this is related to 2.x.
I am using
jest.useFakeTimers('modern');
in order to calljest.setSystemTime
in my test just FYI.Expected behavior
Tests should pass
Steps to Reproduce
Simple component:
spec file:
Versions
The text was updated successfully, but these errors were encountered: