From 8b7fc4ce567661fb290856970608ee03df601d59 Mon Sep 17 00:00:00 2001 From: Wilson Page Date: Wed, 26 Jul 2023 12:02:47 +0100 Subject: [PATCH] Fix handle jest v27 fake timers (issue #30) The existing check for jest fake timers no longer works as jest v27 mutates the `setTimeout` global differently. This patch checks for both the old and new formats. --- src/helpers.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/helpers.ts b/src/helpers.ts index 4b4e8d3..cf55492 100644 --- a/src/helpers.ts +++ b/src/helpers.ts @@ -7,9 +7,12 @@ const globalObj = typeof window === "undefined" ? global : window; // Currently this fn only supports jest timers, but it could support other test runners in the future. function runWithRealTimers(callback: () => any) { + // legacy jest timers mutate `global.setTimeout` differently to + // jest v27 fake timers; this checks hanldes both const usingJestFakeTimers = // eslint-disable-next-line no-underscore-dangle - (globalObj.setTimeout as any)._isMockFunction && + ((globalObj.setTimeout as any)._isMockFunction || + (globalObj.setTimeout as any).clock) && typeof jest !== "undefined"; if (usingJestFakeTimers) {