test: reduce import wrapper reload churn

This commit is contained in:
Peter Steinberger
2026-04-03 07:14:31 +01:00
parent 9bba2ec0ad
commit b66197f932
9 changed files with 57 additions and 31 deletions

View File

@@ -1,4 +1,5 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
import { importFreshModule } from "../../test/helpers/import-fresh.ts";
const moduleLoads = vi.hoisted(() => ({
whatsapp: vi.fn(),
@@ -56,8 +57,13 @@ vi.mock("./send-runtime/imessage.js", () => {
});
describe("createDefaultDeps", () => {
async function loadCreateDefaultDeps() {
return (await import("./deps.js")).createDefaultDeps;
async function loadCreateDefaultDeps(scope: string) {
return (
await importFreshModule<typeof import("./deps.js")>(
import.meta.url,
`./deps.js?scope=${scope}`,
)
).createDefaultDeps;
}
function expectUnusedModulesNotLoaded(exclude: keyof typeof moduleLoads): void {
@@ -72,11 +78,10 @@ describe("createDefaultDeps", () => {
beforeEach(() => {
vi.clearAllMocks();
vi.resetModules();
});
it("does not load provider modules until a dependency is used", async () => {
const createDefaultDeps = await loadCreateDefaultDeps();
const createDefaultDeps = await loadCreateDefaultDeps("lazy-load");
const deps = createDefaultDeps();
expect(moduleLoads.whatsapp).not.toHaveBeenCalled();
@@ -95,7 +100,7 @@ describe("createDefaultDeps", () => {
});
it("reuses module cache after first dynamic import", async () => {
const createDefaultDeps = await loadCreateDefaultDeps();
const createDefaultDeps = await loadCreateDefaultDeps("module-cache");
const deps = createDefaultDeps();
const sendDiscord = deps["discord"] as (...args: unknown[]) => Promise<unknown>;
@@ -107,7 +112,10 @@ describe("createDefaultDeps", () => {
});
it("does not import the whatsapp runtime boundary on deps module load", async () => {
await import("./deps.js");
await importFreshModule<typeof import("./deps.js")>(
import.meta.url,
"./deps.js?scope=no-whatsapp-runtime-on-import",
);
expect(whatsappBoundaryLoads).not.toHaveBeenCalled();
});