Moved interval to a static property to (hopefully) fix things

This commit is contained in:
Bertie690 2025-08-06 22:02:06 -04:00
parent d10b1b0a13
commit 675139e3ba
4 changed files with 7 additions and 75 deletions

View File

@ -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());
}
/**

View File

@ -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<TArgs extends any[]>(
...args: Parameters<typeof setTimeout<TArgs>>
): ReturnType<typeof setTimeout<TArgs>>;
export function setTempTimeout(...args: Parameters<typeof setTimeout>): ReturnType<typeof setTimeout>;
export function setTempTimeout(...args: Parameters<typeof setTimeout>): ReturnType<typeof setTimeout> {
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<TArgs extends any[]>(
...args: Parameters<typeof setInterval<TArgs>>
): ReturnType<typeof setInterval<TArgs>>;
export function setTempInterval(...args: Parameters<typeof setInterval>): ReturnType<typeof setInterval>;
export function setTempInterval(...args: Parameters<typeof setInterval>): ReturnType<typeof setInterval> {
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);
}

View File

@ -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);
});
});

View File

@ -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;
});