Skip to content

Commit 2d646d7

Browse files
[v14]: refactor: fake timers tweaks (#1849)
1 parent 4341ad3 commit 2d646d7

File tree

3 files changed

+15
-10
lines changed

3 files changed

+15
-10
lines changed

src/__tests__/act.test.tsx

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,6 @@ test('fireEvent should trigger useState', async () => {
4040
expect(counter.props.children).toEqual('Total count: 1');
4141
});
4242

43-
test('should be able to not await act', () => {
44-
const result = act(() => {});
45-
expect(result).toHaveProperty('then');
46-
});
47-
4843
test('should be able to await act', async () => {
4944
const result = await act(async () => {});
5045
expect(result).toBe(undefined);

src/helpers/timers.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ const { clearTimeoutFn, setImmediateFn, setTimeoutFn } = runWithRealTimers(
8888

8989
export {
9090
clearTimeoutFn as clearTimeout,
91+
getJestFakeTimersType,
9192
jestFakeTimersAreEnabled,
9293
runWithRealTimers,
9394
setImmediateFn as setImmediate,

src/wait-for.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@ import { act } from './act';
33
import { getConfig } from './config';
44
import { flushMicroTasks } from './flush-micro-tasks';
55
import { copyStackTrace, ErrorWithStack } from './helpers/errors';
6-
import { clearTimeout, jestFakeTimersAreEnabled, setTimeout } from './helpers/timers';
6+
import {
7+
clearTimeout,
8+
getJestFakeTimersType,
9+
jestFakeTimersAreEnabled,
10+
setTimeout,
11+
} from './helpers/timers';
712
import { wrapAsync } from './helpers/wrap-async';
813

914
const DEFAULT_INTERVAL = 50;
@@ -36,9 +41,9 @@ function waitForInternal<T>(
3641

3742
let overallTimeoutTimer: NodeJS.Timeout | null = null;
3843

39-
const usingFakeTimers = jestFakeTimersAreEnabled();
44+
const fakeTimersType = getJestFakeTimersType();
4045

41-
if (usingFakeTimers) {
46+
if (fakeTimersType) {
4247
checkExpectation();
4348
// this is a dangerous rule to disable because it could lead to an
4449
// infinite loop. However, eslint isn't smart enough to know that we're
@@ -70,7 +75,11 @@ function waitForInternal<T>(
7075
// third party code that's setting up recursive timers so rapidly that
7176
// the user's timer's don't get a chance to resolve. So we'll advance
7277
// by an interval instead. (We have a test for this case).
73-
await act(() => jest.advanceTimersByTime(interval));
78+
await act(() =>
79+
fakeTimersType === 'modern'
80+
? jest.advanceTimersByTimeAsync(interval)
81+
: jest.advanceTimersByTime(interval),
82+
);
7483

7584
// It's really important that checkExpectation is run *before* we flush
7685
// in-flight promises. To be honest, I'm not sure why, and I can't quite
@@ -96,7 +105,7 @@ function waitForInternal<T>(
96105
clearTimeout(overallTimeoutTimer);
97106
}
98107

99-
if (!usingFakeTimers) {
108+
if (!fakeTimersType) {
100109
clearInterval(intervalId);
101110
}
102111

0 commit comments

Comments
 (0)