From 456489974d2500ba71a3e76c5af2071d97d392b7 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Mon, 20 Apr 2026 19:35:31 +0100 Subject: [PATCH] test: share jiti loader cache fixture --- src/plugins/jiti-loader-cache.test.ts | 76 +++++++++------------------ 1 file changed, 25 insertions(+), 51 deletions(-) diff --git a/src/plugins/jiti-loader-cache.test.ts b/src/plugins/jiti-loader-cache.test.ts index 6e429abef44..35fd8fd2e6b 100644 --- a/src/plugins/jiti-loader-cache.test.ts +++ b/src/plugins/jiti-loader-cache.test.ts @@ -6,20 +6,28 @@ afterEach(() => { vi.doUnmock("jiti"); }); +async function loadCachedPluginJitiLoader(scope: string) { + const createJiti = vi.fn((filename: string, options?: Record) => + Object.assign(vi.fn(), { + filename, + options, + }), + ); + vi.doMock("jiti", () => ({ + createJiti, + })); + + const { getCachedPluginJitiLoader } = await importFreshModule< + typeof import("./jiti-loader-cache.js") + >(import.meta.url, `./jiti-loader-cache.js?scope=${scope}`); + + return { createJiti, getCachedPluginJitiLoader }; +} + describe("getCachedPluginJitiLoader", () => { it("reuses cached loaders for the same module config and filename", async () => { - const createJiti = vi.fn((filename: string) => - Object.assign(vi.fn(), { - filename, - }), - ); - vi.doMock("jiti", () => ({ - createJiti, - })); - - const { getCachedPluginJitiLoader } = await importFreshModule< - typeof import("./jiti-loader-cache.js") - >(import.meta.url, "./jiti-loader-cache.js?scope=cached-loader"); + const { createJiti, getCachedPluginJitiLoader } = + await loadCachedPluginJitiLoader("cached-loader"); const cache = new Map(); const params = { @@ -39,19 +47,8 @@ describe("getCachedPluginJitiLoader", () => { }); it("keeps loader caches scoped by jiti filename and dist preference", async () => { - const createJiti = vi.fn((filename: string, options: Record) => - Object.assign(vi.fn(), { - filename, - options, - }), - ); - vi.doMock("jiti", () => ({ - createJiti, - })); - - const { getCachedPluginJitiLoader } = await importFreshModule< - typeof import("./jiti-loader-cache.js") - >(import.meta.url, "./jiti-loader-cache.js?scope=filename-scope"); + const { createJiti, getCachedPluginJitiLoader } = + await loadCachedPluginJitiLoader("filename-scope"); const cache = new Map(); const first = getCachedPluginJitiLoader({ @@ -94,19 +91,7 @@ describe("getCachedPluginJitiLoader", () => { }); it("lets callers override alias maps and tryNative while keeping cache keys stable", async () => { - const createJiti = vi.fn((filename: string, options: Record) => - Object.assign(vi.fn(), { - filename, - options, - }), - ); - vi.doMock("jiti", () => ({ - createJiti, - })); - - const { getCachedPluginJitiLoader } = await importFreshModule< - typeof import("./jiti-loader-cache.js") - >(import.meta.url, "./jiti-loader-cache.js?scope=overrides"); + const { createJiti, getCachedPluginJitiLoader } = await loadCachedPluginJitiLoader("overrides"); const cache = new Map(); const first = getCachedPluginJitiLoader({ @@ -147,19 +132,8 @@ describe("getCachedPluginJitiLoader", () => { }); it("lets callers intentionally share loaders behind a custom cache scope key", async () => { - const createJiti = vi.fn((filename: string, options: Record) => - Object.assign(vi.fn(), { - filename, - options, - }), - ); - vi.doMock("jiti", () => ({ - createJiti, - })); - - const { getCachedPluginJitiLoader } = await importFreshModule< - typeof import("./jiti-loader-cache.js") - >(import.meta.url, "./jiti-loader-cache.js?scope=cache-scope-key"); + const { createJiti, getCachedPluginJitiLoader } = + await loadCachedPluginJitiLoader("cache-scope-key"); const cache = new Map(); const first = getCachedPluginJitiLoader({