File size: 1,542 Bytes
fc93158 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | import path from "node:path";
import { pathToFileURL } from "node:url";
import { describe, expect, it } from "vitest";
import { resolveFileModuleUrl, resolveFunctionModuleExport } from "./module-loader.js";
describe("hooks module loader helpers", () => {
it("builds a file URL without cache-busting by default", () => {
const modulePath = path.resolve("/tmp/hook-handler.js");
expect(resolveFileModuleUrl({ modulePath })).toBe(pathToFileURL(modulePath).href);
});
it("adds a cache-busting query when requested", () => {
const modulePath = path.resolve("/tmp/hook-handler.js");
expect(
resolveFileModuleUrl({
modulePath,
cacheBust: true,
nowMs: 123,
}),
).toBe(`${pathToFileURL(modulePath).href}?t=123`);
});
it("resolves explicit function exports", () => {
const fn = () => "ok";
const resolved = resolveFunctionModuleExport({
mod: { run: fn },
exportName: "run",
});
expect(resolved).toBe(fn);
});
it("falls back through named exports when no explicit export is provided", () => {
const fallback = () => "ok";
const resolved = resolveFunctionModuleExport({
mod: { transform: fallback },
fallbackExportNames: ["default", "transform"],
});
expect(resolved).toBe(fallback);
});
it("returns undefined when export exists but is not callable", () => {
const resolved = resolveFunctionModuleExport({
mod: { run: "nope" },
exportName: "run",
});
expect(resolved).toBeUndefined();
});
});
|