Skip to content

Commit

Permalink
fix: Adjusted url join to avoid double slashes
Browse files Browse the repository at this point in the history
  • Loading branch information
itpropro committed Oct 2, 2024
1 parent 2348417 commit a3b0f22
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 5 deletions.
3 changes: 0 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ function _resolve(id: string | URL, options: ResolveOptions = {}): string {
for (const prefix of ["", "/index"]) {
for (const extension of options.extensions || DEFAULT_EXTENSIONS) {
resolved = _tryModuleResolve(
id + prefix + extension,
joinURL(id, prefix) + extension,
url,
conditionsSet,
);
Expand Down
41 changes: 40 additions & 1 deletion test/resolve.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { existsSync } from "node:fs";
import { describe, it, expect } from "vitest";
import { describe, it, expect, vi, afterEach } from "vitest";
import { resolveSync, resolvePathSync, fileURLToPath } from "../src";
import { parseFilename } from "ufo";

const tests = [
// Resolve to path
Expand All @@ -14,6 +15,44 @@ const tests = [
{ input: "/non/existent", action: "throws" },
] as const;

afterEach(() => {
vi.restoreAllMocks();
});

const { mockedResolve } = await vi.hoisted(async () => {
const importMetaResolve = await vi.importActual<
Record<string, (...args: unknown[]) => unknown>
>("import-meta-resolve");
return {
mockedResolve: vi.fn((id, url, conditions) => {
return importMetaResolve.moduleResolve(id, url, conditions);
}),
};
});

vi.mock("import-meta-resolve", () => {
return {
moduleResolve: mockedResolve,
};
});

describe("tryModuleResolve", () => {
it("should create correct url", () => {
expect(() =>
resolvePathSync("tslib/", {
url: import.meta.url.replace(
parseFilename(import.meta.url, { strict: false }) || "",
"",
),
}),
).toThrow();
expect(mockedResolve).toHaveBeenCalled();
expect(
mockedResolve.mock.calls.some((call) => call[0].includes("//")),
).toBe(false);
});
});

describe("resolveSync", () => {
for (const test of tests) {
it(`${test.input} should ${test.action}`, () => {
Expand Down

0 comments on commit a3b0f22

Please sign in to comment.