diff --git a/src/__tests__/act.test.tsx b/src/__tests__/act.test.tsx index c54e35c38..08944e994 100644 --- a/src/__tests__/act.test.tsx +++ b/src/__tests__/act.test.tsx @@ -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); diff --git a/src/helpers/timers.ts b/src/helpers/timers.ts index 5c8d7e143..1284f9305 100644 --- a/src/helpers/timers.ts +++ b/src/helpers/timers.ts @@ -88,6 +88,7 @@ const { clearTimeoutFn, setImmediateFn, setTimeoutFn } = runWithRealTimers( export { clearTimeoutFn as clearTimeout, + getJestFakeTimersType, jestFakeTimersAreEnabled, runWithRealTimers, setImmediateFn as setImmediate, diff --git a/src/wait-for.ts b/src/wait-for.ts index 74174e3f6..409623a8c 100644 --- a/src/wait-for.ts +++ b/src/wait-for.ts @@ -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; @@ -36,9 +41,9 @@ function waitForInternal( 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 @@ -70,7 +75,11 @@ function waitForInternal( // 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 @@ -96,7 +105,7 @@ function waitForInternal( clearTimeout(overallTimeoutTimer); } - if (!usingFakeTimers) { + if (!fakeTimersType) { clearInterval(intervalId); }