mirror of
https://github.com/snapotter-hq/SnapOtter.git
synced 2026-08-03 07:46:42 +02:00
fix: use fake timers in lazy-with-retry tests to prevent CI timeout
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
// @vitest-environment jsdom
|
// @vitest-environment jsdom
|
||||||
import { describe, expect, it, vi } from "vitest";
|
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||||
|
|
||||||
vi.mock("@/stores/connection-store", () => ({
|
vi.mock("@/stores/connection-store", () => ({
|
||||||
useConnectionStore: { getState: () => ({ setDisconnected: vi.fn() }) },
|
useConnectionStore: { getState: () => ({ setDisconnected: vi.fn() }) },
|
||||||
@@ -8,6 +8,14 @@ vi.mock("@/stores/connection-store", () => ({
|
|||||||
import { retryDynamicImport } from "@/lib/lazy-with-retry";
|
import { retryDynamicImport } from "@/lib/lazy-with-retry";
|
||||||
|
|
||||||
describe("retryDynamicImport", () => {
|
describe("retryDynamicImport", () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
vi.useFakeTimers();
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
vi.useRealTimers();
|
||||||
|
});
|
||||||
|
|
||||||
it("resolves on first success", async () => {
|
it("resolves on first success", async () => {
|
||||||
const mod = { default: () => null };
|
const mod = { default: () => null };
|
||||||
const importFn = vi.fn().mockResolvedValue(mod);
|
const importFn = vi.fn().mockResolvedValue(mod);
|
||||||
@@ -23,7 +31,9 @@ describe("retryDynamicImport", () => {
|
|||||||
.mockRejectedValueOnce(new TypeError("Failed to fetch dynamically imported module"))
|
.mockRejectedValueOnce(new TypeError("Failed to fetch dynamically imported module"))
|
||||||
.mockRejectedValueOnce(new TypeError("Failed to fetch dynamically imported module"))
|
.mockRejectedValueOnce(new TypeError("Failed to fetch dynamically imported module"))
|
||||||
.mockResolvedValue(mod);
|
.mockResolvedValue(mod);
|
||||||
const result = await retryDynamicImport(importFn, 3, 0);
|
const promise = retryDynamicImport(importFn, 3, 100);
|
||||||
|
await vi.runAllTimersAsync();
|
||||||
|
const result = await promise;
|
||||||
expect(result).toBe(mod);
|
expect(result).toBe(mod);
|
||||||
expect(importFn).toHaveBeenCalledTimes(3);
|
expect(importFn).toHaveBeenCalledTimes(3);
|
||||||
});
|
});
|
||||||
@@ -31,9 +41,10 @@ describe("retryDynamicImport", () => {
|
|||||||
it("rejects after all retries exhausted", async () => {
|
it("rejects after all retries exhausted", async () => {
|
||||||
const err = new TypeError("Failed to fetch dynamically imported module");
|
const err = new TypeError("Failed to fetch dynamically imported module");
|
||||||
const importFn = vi.fn().mockRejectedValue(err);
|
const importFn = vi.fn().mockRejectedValue(err);
|
||||||
await expect(retryDynamicImport(importFn, 3, 0)).rejects.toThrow(
|
const promise = retryDynamicImport(importFn, 3, 100);
|
||||||
"Failed to fetch dynamically imported module",
|
promise.catch(() => {});
|
||||||
);
|
await vi.runAllTimersAsync();
|
||||||
|
await expect(promise).rejects.toThrow("Failed to fetch dynamically imported module");
|
||||||
expect(importFn).toHaveBeenCalledTimes(3);
|
expect(importFn).toHaveBeenCalledTimes(3);
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -45,7 +56,9 @@ describe("retryDynamicImport", () => {
|
|||||||
new TypeError("Unable to preload CSS for /assets/tool-page-DDbXBANV.css"),
|
new TypeError("Unable to preload CSS for /assets/tool-page-DDbXBANV.css"),
|
||||||
)
|
)
|
||||||
.mockResolvedValue(mod);
|
.mockResolvedValue(mod);
|
||||||
const result = await retryDynamicImport(importFn, 3, 0);
|
const promise = retryDynamicImport(importFn, 3, 100);
|
||||||
|
await vi.runAllTimersAsync();
|
||||||
|
const result = await promise;
|
||||||
expect(result).toBe(mod);
|
expect(result).toBe(mod);
|
||||||
expect(importFn).toHaveBeenCalledTimes(2);
|
expect(importFn).toHaveBeenCalledTimes(2);
|
||||||
});
|
});
|
||||||
@@ -53,7 +66,7 @@ describe("retryDynamicImport", () => {
|
|||||||
it("only retries chunk-related errors, not other errors", async () => {
|
it("only retries chunk-related errors, not other errors", async () => {
|
||||||
const err = new Error("Some other error");
|
const err = new Error("Some other error");
|
||||||
const importFn = vi.fn().mockRejectedValue(err);
|
const importFn = vi.fn().mockRejectedValue(err);
|
||||||
await expect(retryDynamicImport(importFn, 3, 0)).rejects.toThrow("Some other error");
|
await expect(retryDynamicImport(importFn, 3, 100)).rejects.toThrow("Some other error");
|
||||||
expect(importFn).toHaveBeenCalledTimes(1);
|
expect(importFn).toHaveBeenCalledTimes(1);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user