diff --git a/tests/setup.ts b/tests/setup.ts index e2621932..d9a6d8e8 100644 --- a/tests/setup.ts +++ b/tests/setup.ts @@ -1,7 +1,10 @@ import "@testing-library/jest-dom/vitest"; import { cleanup } from "@testing-library/react"; -import { afterEach } from "vitest"; +import { afterEach, vi } from "vitest"; afterEach(() => { cleanup(); + // A test that fails before its own vi.useRealTimers() must not leave fake + // timers to the next test in the file. + vi.useRealTimers(); }); diff --git a/tests/unit/test-setup.test.ts b/tests/unit/test-setup.test.ts new file mode 100644 index 00000000..0e921b3b --- /dev/null +++ b/tests/unit/test-setup.test.ts @@ -0,0 +1,14 @@ +import { describe, expect, it, vi } from "vitest"; + +// tests/setup.ts restores real timers after every test, so a test that fails +// before its own vi.useRealTimers() does not hand fake timers to the next one. +describe("global test setup", () => { + it("may end with fake timers still installed", () => { + vi.useFakeTimers(); + expect(vi.isFakeTimers()).toBe(true); + }); + + it("starts the next test on real timers", () => { + expect(vi.isFakeTimers()).toBe(false); + }); +});