Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

chunkReloadGuard.test.ts

103 lines3.4 KB

1import { describe, expect, it, vi } from "vite-plus/test";
2​
3import {
4 isRoutePreloadPending,
5 isSpeculativeLoadFailure,
6 isSpeculativeLoadPending,
7 preloadRouteSpeculatively,
8 preloadSpeculatively,
9 reloadOnceForChunkLoadError,
10} from "./chunkReloadGuard";
11​
12function createStorageStub(): Storage {
13 const store = new Map<string, string>();
14 return {
15 getItem: (key) => store.get(key) ?? null,
16 setItem: (key, value) => {
17 store.set(key, value);
18 },
19 removeItem: (key) => {
20 store.delete(key);
21 },
22 clear: () => store.clear(),
23 key: (index) => [...store.keys()][index] ?? null,
24 get length() {
25 return store.size;
26 },
27 };
29​
30describe("reloadOnceForChunkLoadError", () => {
31 it("reloads on the first failure and lets the second one surface", () => {
32 const storage = createStorageStub();
33 const reload = vi.fn();
34​
35 expect(reloadOnceForChunkLoadError("a", "chunk", () => storage, reload)).toBe(true);
36 expect(reload).toHaveBeenCalledTimes(1);
37​
38 expect(reloadOnceForChunkLoadError("a", "chunk", () => storage, reload)).toBe(false);
39 expect(reload).toHaveBeenCalledTimes(1);
40 });
41​
42 it("gives each build its own reload", () => {
43 const storage = createStorageStub();
44 const reload = vi.fn();
45​
46 reloadOnceForChunkLoadError("a", "chunk", () => storage, reload);
47​
48 expect(reloadOnceForChunkLoadError("b", "chunk", () => storage, reload)).toBe(true);
49 expect(reload).toHaveBeenCalledTimes(2);
50 });
51​
52 it("bounds failures per build and resets the limit for the next build", () => {
53 const storage = createStorageStub();
54 const reload = vi.fn();
55 for (const build of ["a", "b"]) {
56 for (let index = 0; index < 20; index += 1) {
57 expect(reloadOnceForChunkLoadError(build, String(index), () => storage, reload)).toBe(true);
58 }
59 expect(reloadOnceForChunkLoadError(build, "20", () => storage, reload)).toBe(false);
60 }
61 expect(reload).toHaveBeenCalledTimes(40);
62 });
63​
64 it("never reloads when storage is blocked, so a persistent failure cannot loop", () => {
65 const reload = vi.fn();
66 const blocked = () => {
67 throw new DOMException("blocked", "SecurityError");
68 };
69​
70 expect(reloadOnceForChunkLoadError("a", "chunk", blocked, reload)).toBe(false);
71 expect(reload).not.toHaveBeenCalled();
72 });
73});
74​
75it.each([preloadSpeculatively, preloadRouteSpeculatively])(
76 "attributes speculative failures and resets pending state: %s",
77 async (load) => {
78 let reject!: (error: unknown) => void;
79 const pending = new Promise<never>((_, fail) => {
80 reject = fail;
81 });
82 const preload = load(() => pending);
83 expect(
84 load === preloadSpeculatively ? isSpeculativeLoadPending() : isRoutePreloadPending(),
85 ).toBe(true);
86 const failure = new TypeError(
87 "Failed to fetch dynamically imported module: https://app.t3.codes/assets/chunk.js",
88 );
89 reject(failure);
90 if (load === preloadSpeculatively) await expect(preload).resolves.toBeUndefined();
91 else await expect(preload).rejects.toBe(failure);
92 expect(isSpeculativeLoadPending() || isRoutePreloadPending()).toBe(false);
93 expect(isSpeculativeLoadFailure(failure)).toBe(load === preloadSpeculatively);
94 expect(
95 isSpeculativeLoadFailure(
96 new TypeError(
97 "Failed to fetch dynamically imported module: https://app.t3.codes/assets/chunk.js",
98 ),
99 ),
100 ).toBe(false);
101 await expect(load(async () => "module")).resolves.toBe("module");
102 },
103);