From 675139e3ba6938b65d64445c391173f04780b688 Mon Sep 17 00:00:00 2001 From: Bertie690 Date: Wed, 6 Aug 2025 22:02:06 -0400 Subject: [PATCH] Moved interval to a static property to (hopefully) fix things --- test/test-utils/helpers/prompt-handler.ts | 6 +-- test/test-utils/interval-helper.ts | 43 --------------------- test/test-utils/tests/timeout-reset.test.ts | 25 ------------ test/vitest.setup.ts | 8 ++-- 4 files changed, 7 insertions(+), 75 deletions(-) delete mode 100644 test/test-utils/interval-helper.ts delete mode 100644 test/test-utils/tests/timeout-reset.test.ts diff --git a/test/test-utils/helpers/prompt-handler.ts b/test/test-utils/helpers/prompt-handler.ts index 57ee0b68c14..48b30064724 100644 --- a/test/test-utils/helpers/prompt-handler.ts +++ b/test/test-utils/helpers/prompt-handler.ts @@ -2,7 +2,6 @@ import type { AwaitableUiHandler } from "#app/ui/awaitable-ui-handler"; import { UiMode } from "#enums/ui-mode"; import type { GameManager } from "#test/test-utils/game-manager"; import { GameManagerHelper } from "#test/test-utils/helpers/game-manager-helper"; -import { setTempInterval } from "#test/test-utils/interval-helper"; import type { PhaseString } from "#types/phase-types"; import chalk from "chalk"; import { type MockInstance, vi } from "vitest"; @@ -49,6 +48,8 @@ export class PromptHandler extends GameManagerHelper { /** The original `setModeInternal` function, stored for use in {@linkcode setMode}. */ private originalSetModeInternal: (typeof this.game.scene.ui)["setModeInternal"]; + public static runInterval?: NodeJS.Timeout; + constructor(game: GameManager) { super(game); this.originalSetModeInternal = this.game.scene.ui["setModeInternal"]; @@ -60,8 +61,7 @@ export class PromptHandler extends GameManagerHelper { ).mockImplementation((...args) => this.setMode(args)); // Set an interval to repeatedly check the current prompt. - // TODO: Ideally we would find a way NOT for this to use a prompt check... - setTempInterval(() => this.doPromptCheck()); + PromptHandler.runInterval = setInterval(() => this.doPromptCheck()); } /** diff --git a/test/test-utils/interval-helper.ts b/test/test-utils/interval-helper.ts deleted file mode 100644 index 0ba0c6e602a..00000000000 --- a/test/test-utils/interval-helper.ts +++ /dev/null @@ -1,43 +0,0 @@ -/** An array of pending timeouts and intervals to clear on test end. */ -const allTimeouts: NodeJS.Timeout[] = []; - -/** - * Set a temporary timeout that will be cleared upon test end. - * @param args - The arguments to the original function - * @returns The newly created timeout - */ -export function setTempTimeout( - ...args: Parameters> -): ReturnType>; -export function setTempTimeout(...args: Parameters): ReturnType; -export function setTempTimeout(...args: Parameters): ReturnType { - const timeout = global.setTimeout(...args); - allTimeouts.push(timeout); - return timeout; -} - -/** - * Set a temporary interval that will be cleared upon test end. - * @param args - The arguments to the original function - * @returns The newly created interval - */ -export function setTempInterval( - ...args: Parameters> -): ReturnType>; -export function setTempInterval(...args: Parameters): ReturnType; -export function setTempInterval(...args: Parameters): ReturnType { - const interval = global.setInterval(...args); - allTimeouts.push(interval); - return interval; -} - -/** Clear all lingering timeouts on test end. */ -export function clearAllTimeouts() { - // NB: The absolute WORST CASE SCENARIO for this is us clearing a timeout twice in a row - // (behavior which MDN web docs has certified to be a no-op) - for (const timeout of allTimeouts) { - // clearTimeout works on both intervals and timeouts - clearTimeout(timeout); - } - allTimeouts.splice(0); -} diff --git a/test/test-utils/tests/timeout-reset.test.ts b/test/test-utils/tests/timeout-reset.test.ts deleted file mode 100644 index 343c4e2fc36..00000000000 --- a/test/test-utils/tests/timeout-reset.test.ts +++ /dev/null @@ -1,25 +0,0 @@ -import { setTempInterval } from "#test/test-utils/interval-helper"; -import { setTimeout } from "timers/promises"; -import { describe, expect, it, vi } from "vitest"; - -describe("Timeout resets", () => { - let counter = 1; - - it.sequential("should not reset intervals during test", async () => { - vi.spyOn(global, "clearInterval"); - setTempInterval(() => { - console.log("interval called"); - counter++; - expect(counter).toBeLessThan(200); - }, 50); - await vi.waitUntil(() => counter > 2); - expect(clearInterval).not.toHaveBeenCalled(); - }); - - it.sequential("should reset intervals after test end", async () => { - const initCounter = counter; - await setTimeout(500); - // Were the interval active, the counter would have increased by now - expect(counter).toBe(initCounter); - }); -}); diff --git a/test/vitest.setup.ts b/test/vitest.setup.ts index 631c15499e1..f6d1755fcd2 100644 --- a/test/vitest.setup.ts +++ b/test/vitest.setup.ts @@ -1,5 +1,5 @@ import "vitest-canvas-mock"; -import { clearAllTimeouts } from "#test/test-utils/interval-helper"; +import { PromptHandler } from "#test/test-utils/helpers/prompt-handler"; import { initTests } from "#test/test-utils/test-file-initialization"; import { afterAll, afterEach, beforeAll, vi } from "vitest"; @@ -59,15 +59,15 @@ afterAll(() => { }); afterEach(() => { - clearAllTimeouts(); + clearInterval(PromptHandler.runInterval); }); process.on("uncaughtException", err => { - clearAllTimeouts(); + clearInterval(PromptHandler.runInterval); throw err; }); process.on("unhandledRejection", err => { - clearAllTimeouts(); + clearInterval(PromptHandler.runInterval); throw err; });