Skip to content

Commit

Permalink
refacot: move test to the end
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Oct 6, 2024
1 parent 6870354 commit 7cc8645
Showing 1 changed file with 38 additions and 38 deletions.
76 changes: 38 additions & 38 deletions test/resolve.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,44 +15,6 @@ 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 Expand Up @@ -120,3 +82,41 @@ describe("resolvePathSync", () => {
});
}
});

// https://github.com/unjs/mlly/pull/278
describe("tryModuleResolve", async () => {
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);
}),
};
});

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

it("should create correct url", () => {
vi.mock("import-meta-resolve", () => {
return {
moduleResolve: mockedResolve,
};
});
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);
});
});

0 comments on commit 7cc8645

Please sign in to comment.