|
| 1 | +import { mkdir } from "node:fs/promises"; |
| 2 | +import path from "node:path"; |
| 3 | +import type { Compressor, Settings } from "@node-minify/types"; |
| 4 | +import { afterEach, describe, expect, test, vi } from "vitest"; |
| 5 | + |
| 6 | +vi.mock("node:fs/promises", () => ({ |
| 7 | + mkdir: vi.fn().mockResolvedValue(undefined), |
| 8 | +})); |
| 9 | + |
| 10 | +vi.mock("@node-minify/utils", async (importOriginal) => { |
| 11 | + const actual = await importOriginal<typeof import("@node-minify/utils")>(); |
| 12 | + return { |
| 13 | + ...actual, |
| 14 | + compressSingleFile: vi.fn().mockResolvedValue("ok"), |
| 15 | + }; |
| 16 | +}); |
| 17 | + |
| 18 | +import { compressSingleFile } from "@node-minify/utils"; |
| 19 | +import { compress } from "../src/compress.ts"; |
| 20 | + |
| 21 | +describe("compress path handling", () => { |
| 22 | + afterEach(() => { |
| 23 | + vi.clearAllMocks(); |
| 24 | + vi.restoreAllMocks(); |
| 25 | + }); |
| 26 | + |
| 27 | + test("should create directory for output paths using backslashes", async () => { |
| 28 | + const compressor: Compressor = async () => ({ code: "ok" }); |
| 29 | + const settings: Settings = { |
| 30 | + compressor, |
| 31 | + input: "input.js", |
| 32 | + output: "nested\\dir\\file.min.js", |
| 33 | + }; |
| 34 | + |
| 35 | + await compress(settings); |
| 36 | + |
| 37 | + expect(vi.mocked(mkdir)).toHaveBeenCalledWith("nested/dir", { |
| 38 | + recursive: true, |
| 39 | + }); |
| 40 | + expect(vi.mocked(compressSingleFile)).toHaveBeenCalledTimes(1); |
| 41 | + }); |
| 42 | + |
| 43 | + test("should normalize dirname output before mkdir", async () => { |
| 44 | + vi.spyOn(path, "dirname").mockReturnValue("nested\\dir"); |
| 45 | + |
| 46 | + const compressor: Compressor = async () => ({ code: "ok" }); |
| 47 | + const settings: Settings = { |
| 48 | + compressor, |
| 49 | + input: "input.js", |
| 50 | + output: "nested\\dir\\file.min.js", |
| 51 | + }; |
| 52 | + |
| 53 | + await compress(settings); |
| 54 | + |
| 55 | + expect(vi.mocked(mkdir)).toHaveBeenCalledWith("nested/dir", { |
| 56 | + recursive: true, |
| 57 | + }); |
| 58 | + expect(vi.mocked(compressSingleFile)).toHaveBeenCalledTimes(1); |
| 59 | + }); |
| 60 | +}); |
0 commit comments