one
Version:
One is a new React Framework that makes Vite serve both native and web.
109 lines (107 loc) • 5.02 kB
JavaScript
;
var import_vitest = require("vitest");
var import_dynamicImport = require("./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);
});
};
(0, import_vitest.describe)("isChunkLoadError", function () {
(0, import_vitest.it)("matches the chrome / firefox / safari chunk-load messages, nothing else", function () {
(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", function () {
(0, import_vitest.it)("resolves on first success without retrying or recovering", async function () {
var loader = import_vitest.vi.fn().mockResolvedValue({
default: "ok"
});
var delay = import_vitest.vi.fn(instantDelay);
var onChunkErrorExhausted = import_vitest.vi.fn(function () {
return 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 function () {
var loader = import_vitest.vi.fn().mockRejectedValueOnce(chunkError()).mockRejectedValueOnce(chunkError()).mockResolvedValue({
default: "ok"
});
var delay = import_vitest.vi.fn(instantDelay);
var onChunkErrorExhausted = import_vitest.vi.fn(function () {
return 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 function () {
var loader = import_vitest.vi.fn().mockRejectedValue(chunkError());
var delay = import_vitest.vi.fn(instantDelay);
var onChunkErrorExhausted = import_vitest.vi.fn(function () {
return true;
});
var 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 function () {
var loader = import_vitest.vi.fn().mockRejectedValue(new Error("boom: a real bug"));
var delay = import_vitest.vi.fn(instantDelay);
var onChunkErrorExhausted = import_vitest.vi.fn(function () {
return 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 function () {
var loader = import_vitest.vi.fn().mockRejectedValue(chunkError());
var delay = import_vitest.vi.fn(instantDelay);
var onChunkErrorExhausted = import_vitest.vi.fn(function () {
return 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);
});
});
//# sourceMappingURL=dynamicImport.test.native.js.map