Files
bun.sh/test/js/bun/resolve/import-query.test.ts

45 lines
1.8 KiB
TypeScript

import { beforeEach, expect, test } from "bun:test";
globalThis.importQueryFixtureOrder = [];
const resolvedPath = require.resolve("./import-query-fixture.ts");
const resolvedURL = Bun.pathToFileURL(resolvedPath).href;
beforeEach(() => {
globalThis.importQueryFixtureOrder = [];
Loader.registry.delete(resolvedPath);
Loader.registry.delete(resolvedPath + "?query");
Loader.registry.delete(resolvedPath + "?query2");
});
test("[query, no query]", async () => {
const second = await import("./import-query-fixture.ts?query");
const first = await import("./import-query-fixture.ts");
expect(second.url).toBe(first.url + "?query");
expect(globalThis.importQueryFixtureOrder).toEqual([resolvedURL + "?query", resolvedURL]);
});
test("[no query, query]", async () => {
const first = await import("./import-query-fixture.ts");
const second = await import("./import-query-fixture.ts?query");
expect(second.url).toBe(first.url + "?query");
expect(globalThis.importQueryFixtureOrder).toEqual([resolvedURL, resolvedURL + "?query"]);
});
for (let order of [
[resolvedPath, resolvedPath + "?query", resolvedPath + "?query2"],
[resolvedPath + "?query", resolvedPath + "?query2", resolvedPath],
[resolvedPath + "?query", resolvedPath, resolvedPath + "?query2"],
[resolvedPath, resolvedPath + "?query2", resolvedPath + "?query"],
[resolvedPath + "?query2", resolvedPath, resolvedPath + "?query"],
[resolvedPath + "?query2", resolvedPath + "?query", resolvedPath],
]) {
test(`[${order.map(url => url.replaceAll(import.meta.dir, "")).join(", ")}]`, async () => {
for (const url of order) {
await import(url);
}
expect(globalThis.importQueryFixtureOrder).toEqual(
order.map(url => resolvedURL + (url.includes("?") ? "?" + url.split("?")[1] : "")),
);
});
}