UNPKG

one

Version:

One is a new React Framework that makes Vite serve both native and web.

87 lines 4.71 kB
var import_vitest = require("vitest"); var import_dynamicImport = require("./dynamicImport.cjs"); const instantDelay = () => Promise.resolve(); const chunkError = () => new Error("Failed to fetch dynamically imported module: /assets/EditorPane.js"); const drain = () => new Promise(r => setTimeout(r, 0)); (0, import_vitest.describe)("isChunkLoadError", () => { (0, import_vitest.it)("matches the chrome / firefox / safari chunk-load messages, nothing else", () => { (0, import_vitest.expect)((0, import_dynamicImport.isChunkLoadError)(new Error("Failed to fetch dynamically imported module: /a"))).toBe(true); (0, import_vitest.expect)((0, import_dynamicImport.isChunkLoadError)(new Error("error loading dynamically imported module: /a"))).toBe(true); (0, import_vitest.expect)((0, import_dynamicImport.isChunkLoadError)(new Error("Importing a module script failed."))).toBe(true); (0, import_vitest.expect)((0, import_dynamicImport.isChunkLoadError)("Failed to fetch dynamically imported module")).toBe(true); (0, import_vitest.expect)((0, import_dynamicImport.isChunkLoadError)(new Error("TypeError: x is not a function"))).toBe(false); }); }); (0, import_vitest.describe)("loadWithRetry", () => { (0, import_vitest.it)("resolves on first success without retrying or recovering", async () => { const loader = import_vitest.vi.fn().mockResolvedValue({ default: "ok" }); const delay = import_vitest.vi.fn(instantDelay); const onChunkErrorExhausted = import_vitest.vi.fn(() => false); await (0, import_vitest.expect)((0, import_dynamicImport.loadWithRetry)(loader, { delay, onChunkErrorExhausted })).resolves.toEqual({ default: "ok" }); (0, import_vitest.expect)(loader).toHaveBeenCalledTimes(1); (0, import_vitest.expect)(delay).not.toHaveBeenCalled(); (0, import_vitest.expect)(onChunkErrorExhausted).not.toHaveBeenCalled(); }); (0, import_vitest.it)("retries a transient rejection then resolves, re-invoking the loader each attempt", async () => { const loader = import_vitest.vi.fn().mockRejectedValueOnce(chunkError()).mockRejectedValueOnce(chunkError()).mockResolvedValue({ default: "ok" }); const delay = import_vitest.vi.fn(instantDelay); const onChunkErrorExhausted = import_vitest.vi.fn(() => false); await (0, import_vitest.expect)((0, import_dynamicImport.loadWithRetry)(loader, { attempts: 3, delay, onChunkErrorExhausted })).resolves.toEqual({ default: "ok" }); (0, import_vitest.expect)(loader).toHaveBeenCalledTimes(3); (0, import_vitest.expect)(delay).toHaveBeenCalledTimes(2); (0, import_vitest.expect)(onChunkErrorExhausted).not.toHaveBeenCalled(); }); (0, import_vitest.it)("exhausts retries on a persistent chunk error, recovers once, and stays pending", async () => { const loader = import_vitest.vi.fn().mockRejectedValue(chunkError()); const delay = import_vitest.vi.fn(instantDelay); const onChunkErrorExhausted = import_vitest.vi.fn(() => true); const settled = import_vitest.vi.fn(); void (0, import_dynamicImport.loadWithRetry)(loader, { attempts: 2, delay, onChunkErrorExhausted }).then(settled, settled); await drain(); (0, import_vitest.expect)(loader).toHaveBeenCalledTimes(3); (0, import_vitest.expect)(onChunkErrorExhausted).toHaveBeenCalledTimes(1); (0, import_vitest.expect)(settled).not.toHaveBeenCalled(); }); (0, import_vitest.it)("rethrows a non-chunk error without recovering", async () => { const loader = import_vitest.vi.fn().mockRejectedValue(new Error("boom: a real bug")); const delay = import_vitest.vi.fn(instantDelay); const onChunkErrorExhausted = import_vitest.vi.fn(() => true); await (0, import_vitest.expect)((0, import_dynamicImport.loadWithRetry)(loader, { attempts: 1, delay, onChunkErrorExhausted })).rejects.toThrow("boom: a real bug"); (0, import_vitest.expect)(loader).toHaveBeenCalledTimes(2); (0, import_vitest.expect)(onChunkErrorExhausted).not.toHaveBeenCalled(); }); (0, import_vitest.it)("rethrows a chunk error when the reload was debounced away", async () => { const loader = import_vitest.vi.fn().mockRejectedValue(chunkError()); const delay = import_vitest.vi.fn(instantDelay); const onChunkErrorExhausted = import_vitest.vi.fn(() => false); await (0, import_vitest.expect)((0, import_dynamicImport.loadWithRetry)(loader, { attempts: 1, delay, onChunkErrorExhausted })).rejects.toThrow("Failed to fetch dynamically imported module"); (0, import_vitest.expect)(onChunkErrorExhausted).toHaveBeenCalledTimes(1); }); });