UNPKG

one

Version:

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

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