Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions src/__tests__/act.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,6 @@ test('fireEvent should trigger useState', async () => {
expect(counter.props.children).toEqual('Total count: 1');
});

test('should be able to not await act', () => {
const result = act(() => {});
expect(result).toHaveProperty('then');
});

test('should be able to await act', async () => {
const result = await act(async () => {});
expect(result).toBe(undefined);
Expand Down
1 change: 1 addition & 0 deletions src/helpers/timers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ const { clearTimeoutFn, setImmediateFn, setTimeoutFn } = runWithRealTimers(

export {
clearTimeoutFn as clearTimeout,
getJestFakeTimersType,
jestFakeTimersAreEnabled,
runWithRealTimers,
setImmediateFn as setImmediate,
Expand Down
19 changes: 14 additions & 5 deletions src/wait-for.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ import { act } from './act';
import { getConfig } from './config';
import { flushMicroTasks } from './flush-micro-tasks';
import { copyStackTrace, ErrorWithStack } from './helpers/errors';
import { clearTimeout, jestFakeTimersAreEnabled, setTimeout } from './helpers/timers';
import {
clearTimeout,
getJestFakeTimersType,
jestFakeTimersAreEnabled,
setTimeout,
} from './helpers/timers';
import { wrapAsync } from './helpers/wrap-async';

const DEFAULT_INTERVAL = 50;
Expand Down Expand Up @@ -36,9 +41,9 @@ function waitForInternal<T>(

let overallTimeoutTimer: NodeJS.Timeout | null = null;

const usingFakeTimers = jestFakeTimersAreEnabled();
const fakeTimersType = getJestFakeTimersType();

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

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

if (!usingFakeTimers) {
if (!fakeTimersType) {
clearInterval(intervalId);
}

Expand Down