From 1858a5d2444667980044c5fdad68e2b2740544c1 Mon Sep 17 00:00:00 2001 From: Hugos68 Date: Fri, 24 Jan 2025 10:42:24 +0100 Subject: [PATCH] Moved to `bun:test`. --- bun.lock | 7 ++ package.json | 4 +- test/pagefind-build.test.ts | 6 +- test/pagefind-develop.test.ts | 215 ++++++++++++++++------------------ test/pagefind.test.ts | 2 +- 5 files changed, 117 insertions(+), 117 deletions(-) diff --git a/bun.lock b/bun.lock index f0d721d..2793550 100644 --- a/bun.lock +++ b/bun.lock @@ -4,6 +4,7 @@ "": { "name": "vite-plugin-pagefind", "dependencies": { + "@types/bun": "^1.2.0", "colorette": "^2.0.20", "package-manager-detector": "^0.2.8", }, @@ -231,12 +232,16 @@ "@trysound/sax": ["@trysound/sax@0.2.0", "", {}, "sha512-L7z9BgrNEcYyUYtF+HaEfiS5ebkh9jXqbszz7pC0hRBPaatV0XjSD3+eHrpqFemQfgwiFF0QPIarnIihIDn7OA=="], + "@types/bun": ["@types/bun@1.2.0", "", { "dependencies": { "bun-types": "1.2.0" } }, "sha512-5N1JqdahfpBlAv4wy6svEYcd/YfO2GNrbL95JOmFx8nkE6dbK4R0oSE5SpBA4vBRqgrOUAXF8Dpiz+gi7r80SA=="], + "@types/estree": ["@types/estree@1.0.6", "", {}, "sha512-AYnb1nQyY49te+VRAVgmzfcgjYS91mY5P0TKUDCLEM+gNnA+3T6rWITXRLYCpahpqSQbN5cE+gHpnPyXjHWxcw=="], "@types/node": ["@types/node@22.10.9", "", { "dependencies": { "undici-types": "~6.20.0" } }, "sha512-Ir6hwgsKyNESl/gLOcEz3krR4CBGgliDqBQ2ma4wIhEx0w+xnoeTq3tdrNw15kU3SxogDjOgv9sqdtLW8mIHaw=="], "@types/resolve": ["@types/resolve@1.20.2", "", {}, "sha512-60BCwRFOZCQhDncwQdxxeOEEkbc5dIMccYLwbxsS4TUNeVECQ/pBJ0j09mrHOl/JJvpRPGwO9SvE4nR2Nb/a4Q=="], + "@types/ws": ["@types/ws@8.5.14", "", { "dependencies": { "@types/node": "*" } }, "sha512-bd/YFLW+URhBzMXurx7lWByOu+xzU9+kb3RboOteXYDfW+tr+JZa99OyNmPINEGB/ahzKrEuc8rcv4gnpJmxTw=="], + "@vitest/expect": ["@vitest/expect@3.0.4", "", { "dependencies": { "@vitest/spy": "3.0.4", "@vitest/utils": "3.0.4", "chai": "^5.1.2", "tinyrainbow": "^2.0.0" } }, "sha512-Nm5kJmYw6P2BxhJPkO3eKKhGYKRsnqJqf+r0yOGRKpEP+bSCBDsjXgiu1/5QFrnPMEgzfC38ZEjvCFgaNBC0Eg=="], "@vitest/mocker": ["@vitest/mocker@3.0.4", "", { "dependencies": { "@vitest/spy": "3.0.4", "estree-walker": "^3.0.3", "magic-string": "^0.30.17" }, "peerDependencies": { "msw": "^2.4.9", "vite": "^5.0.0 || ^6.0.0" }, "optionalPeers": ["msw", "vite"] }, "sha512-gEef35vKafJlfQbnyOXZ0Gcr9IBUsMTyTLXsEQwuyYAerpHqvXhzdBnDFuHLpFqth3F7b6BaFr4qV/Cs1ULx5A=="], @@ -273,6 +278,8 @@ "browserslist": ["browserslist@4.24.4", "", { "dependencies": { "caniuse-lite": "^1.0.30001688", "electron-to-chromium": "^1.5.73", "node-releases": "^2.0.19", "update-browserslist-db": "^1.1.1" }, "bin": { "browserslist": "cli.js" } }, "sha512-KDi1Ny1gSePi1vm0q4oxSF8b4DR44GF4BbmS2YdhPLOEqd8pDviZOGH/GsmRwoWJ2+5Lr085X7naowMwKHDG1A=="], + "bun-types": ["bun-types@1.2.0", "", { "dependencies": { "@types/node": "*", "@types/ws": "~8.5.10" } }, "sha512-KEaJxyZfbV/c4eyG0vyehDpYmBGreNiQbZIqvVHJwZ4BmeuWlNZ7EAzMN2Zcd7ailmS/tGVW0BgYbGf+lGEpWw=="], + "cac": ["cac@6.7.14", "", {}, "sha512-b6Ilus+c3RrdDk+JhLKUAQfzzgLEPy6wcXqS7f/xe1EETvsDP6GORG7SFuOs6cID5YkqchW/LXZbX5bc8j7ZcQ=="], "caniuse-api": ["caniuse-api@3.0.0", "", { "dependencies": { "browserslist": "^4.0.0", "caniuse-lite": "^1.0.0", "lodash.memoize": "^4.1.2", "lodash.uniq": "^4.5.0" } }, "sha512-bsTwuIg/BZZK/vreVTYYbSWoe2F+71P7K5QGEX+pT250DZbfU1MQ5prOKpPR+LL6uWKK3KMwMCAS74QB3Um1uw=="], diff --git a/package.json b/package.json index da01d0e..8dacb32 100644 --- a/package.json +++ b/package.json @@ -22,8 +22,8 @@ "scripts": { "build": "unbuild", "build:watch": "unbuild --watch", - "test": "vitest run --silent", - "test:watch": "vitest run --silent --watch", + "test": "bun test", + "test:watch": "bun test --watch", "check": "biome check", "check:fix": "biome check --write", "prepack": "unbuild" diff --git a/test/pagefind-build.test.ts b/test/pagefind-build.test.ts index a662cfd..f0f3a40 100644 --- a/test/pagefind-build.test.ts +++ b/test/pagefind-build.test.ts @@ -1,5 +1,5 @@ -import { describe, expect, it } from "vitest"; -import { pagefindBuild, pagefindDevelop } from "../src"; +import { describe, expect, it } from "bun:test"; +import { pagefindBuild } from "../src"; describe("pagefindBuild", () => { it("has `pagefind-build` as name", () => { @@ -26,7 +26,7 @@ describe("pagefindBuild", () => { }); }); it("externalizes the pagefind bundle with custom bundle directory", () => { - const plugin = pagefindDevelop({ bundleDirectory: "custom" }); + const plugin = pagefindBuild({ bundleDirectory: "custom" }); const config = plugin.config(); expect(config).toMatchObject({ build: { diff --git a/test/pagefind-develop.test.ts b/test/pagefind-develop.test.ts index 305b4ee..0ea23e6 100644 --- a/test/pagefind-develop.test.ts +++ b/test/pagefind-develop.test.ts @@ -1,30 +1,31 @@ -import { execSync } from "node:child_process"; -import { cpSync } from "node:fs"; +import { afterEach, describe, expect, it, mock, spyOn } from "bun:test"; +import * as child_process from "node:child_process"; +import * as fs from "node:fs"; import { resolve } from "node:path"; -import { beforeEach, describe, expect, it, vi } from "vitest"; import { pagefindDevelop } from "../src"; -const mocks = vi.hoisted(() => { - return { - fs: { - existsSync: vi.fn(), - cpSync: vi.fn(), - }, - child_process: { - execSync: vi.fn(), - }, - "package-manager-detector": { - detectSync: vi.fn(), - resolveCommand: vi.fn(), - }, - }; -}); +mock.module("node:fs", () => ({ + existsSync: mock(() => false), + cpSync: mock(() => {}), +})); + +mock.module("node:child_process", () => ({ + execSync: mock(() => {}), +})); -vi.mock("node:fs", () => mocks.fs); -vi.mock("node:child_process", () => mocks.child_process); -vi.mock("package-manager-detector", () => mocks["package-manager-detector"]); +mock.module("package-manager-detector", () => ({ + detectSync: mock(() => ({ agent: "npm" })), + resolveCommand: mock(() => ({ command: "npm run", args: ["build"] })), +})); -beforeEach(vi.restoreAllMocks); +const cpSyncSpy = spyOn(fs, "cpSync"); +const execSyncSpy = spyOn(child_process, "execSync"); + +afterEach(() => { + mock.restore(); + cpSyncSpy.mockClear(); + execSyncSpy.mockClear(); +}); describe("pagefindDevelop", () => { it("has `pagefind-develop` as name", () => { @@ -60,120 +61,112 @@ describe("pagefindDevelop", () => { }, }); }); - describe("lazy strategy", () => { - it("builds and copies the bundle", () => { - mocks.fs.existsSync.mockReturnValue(false); - mocks["package-manager-detector"].detectSync.mockReturnValue({ - agent: "npm", - }); - mocks["package-manager-detector"].resolveCommand.mockReturnValue({ - command: "npm run", - args: ["build"], - }); + describe('"lazy" strategy', () => { + it("builds and copies the bundle when output and bundle are not present", () => { + mock.module("node:fs", () => ({ + existsSync: mock(() => false), + })); const plugin = pagefindDevelop({ developStrategy: "lazy" }); - // @ts-expect-error - We're not mocking a full Vite config + // @ts-expect-error - The plugin only requires this bit of the Vite plugin plugin.configResolved({ root: process.cwd(), }); - expect(cpSync).toHaveBeenCalledWith( - resolve(process.cwd(), "build", "pagefind"), - resolve(process.cwd(), "public", "pagefind"), - { recursive: true }, - ); - expect(execSync).toHaveBeenCalled(); + expect(cpSyncSpy).toHaveBeenCalledTimes(1); + expect(cpSyncSpy.mock.calls).toEqual([ + [ + resolve(process.cwd(), "build", "pagefind"), + resolve(process.cwd(), "public", "pagefind"), + { recursive: true }, + ], + ]); + expect(execSyncSpy).toHaveBeenCalledTimes(1); + expect(execSyncSpy.mock.calls).toEqual([ + ["npm run build", { cwd: process.cwd() }], + ]); }); - it("skips build and copies the bundle if the bundle is only present in output", () => { - mocks.fs.existsSync.mockImplementation((path) => { - if (path === resolve(process.cwd(), "public", "pagefind")) { + it("does not build and copies the bundle when output is present and bundle is not", () => { + mock.module("node:fs", () => ({ + existsSync: mock((path) => { + if (path === resolve(process.cwd(), "public", "pagefind")) { + return false; + } + if (path === resolve(process.cwd(), "build", "pagefind")) { + return true; + } return false; - } - if (path === resolve(process.cwd(), "build", "pagefind")) { - return true; - } - return false; - }); + }), + })); + const plugin = pagefindDevelop({ developStrategy: "lazy" }); - // @ts-expect-error - We're not mocking a full Vite config + // @ts-expect-error - The plugin only requires this bit of the Vite plugin plugin.configResolved({ root: process.cwd(), }); - expect(cpSync).toHaveBeenCalledWith( - resolve(process.cwd(), "build", "pagefind"), - resolve(process.cwd(), "public", "pagefind"), - { recursive: true }, - ); - expect(execSync).not.toHaveBeenCalled(); + expect(cpSyncSpy).toHaveBeenCalledTimes(1); + expect(cpSyncSpy.mock.calls).toEqual([ + [ + resolve(process.cwd(), "build", "pagefind"), + resolve(process.cwd(), "public", "pagefind"), + { recursive: true }, + ], + ]); + expect(execSyncSpy).toHaveBeenCalledTimes(0); }); - it("skips build and copy if bundle already present in assets", () => { - mocks.fs.existsSync.mockImplementation((path) => { - if (path === resolve(process.cwd(), "public", "pagefind")) { - return true; - } - if (path === resolve(process.cwd(), "build", "pagefind")) { - return false; - } - return false; - }); + it("does not build nor copies the bundle when output and bundle are present", () => { + mock.module("node:fs", () => ({ + existsSync: mock(() => true), + })); const plugin = pagefindDevelop({ developStrategy: "lazy" }); - // @ts-expect-error - We're not mocking a full Vite config + // @ts-expect-error - The plugin only requires this bit of the Vite plugin plugin.configResolved({ root: process.cwd(), }); - expect(execSync).not.toHaveBeenCalled(); - expect(cpSync).not.toHaveBeenCalled(); + expect(cpSyncSpy).toHaveBeenCalledTimes(0); + expect(execSyncSpy).toHaveBeenCalledTimes(0); }); }); - describe("eager strategy", () => { - it("builds and copies the bundle", () => { - mocks["package-manager-detector"].detectSync.mockReturnValue({ - agent: "npm", - }); - mocks["package-manager-detector"].resolveCommand.mockReturnValue({ - command: "npm run", - args: ["build"], - }); + + describe('"eager" strategy', () => { + it("builds and copies the bundle when output and bundle are not present", () => { const plugin = pagefindDevelop({ developStrategy: "eager" }); - // @ts-expect-error - We're not mocking a full Vite config + // @ts-expect-error - The plugin only requires this bit of the Vite plugin plugin.configResolved({ root: process.cwd(), }); - expect(cpSync).toHaveBeenCalledWith( - resolve(process.cwd(), "build", "pagefind"), - resolve(process.cwd(), "public", "pagefind"), - { recursive: true }, - ); - expect(execSync).toHaveBeenCalledWith("npm run build", { - cwd: process.cwd(), - }); + expect(cpSyncSpy).toHaveBeenCalledTimes(1); + expect(cpSyncSpy.mock.calls).toEqual([ + [ + resolve(process.cwd(), "build", "pagefind"), + resolve(process.cwd(), "public", "pagefind"), + { recursive: true }, + ], + ]); + expect(execSyncSpy).toHaveBeenCalledTimes(1); + expect(execSyncSpy.mock.calls).toEqual([ + ["npm run build", { cwd: process.cwd() }], + ]); }); - it("builds and copies the bundle with custom command", () => { - mocks["package-manager-detector"].detectSync.mockReturnValue({ - agent: "npm", - }); - mocks["package-manager-detector"].resolveCommand.mockImplementationOnce( - (agent: string, command: string, args: string[]) => { - return { - command: `${agent} ${command}`, - args: args, - }; - }, - ); - const plugin = pagefindDevelop({ - developStrategy: "eager", - buildScript: "build:custom", - }); - // @ts-expect-error - We're not mocking a full Vite config + it("builds and copies bundle when output and bundle are present", () => { + mock.module("node:fs", () => ({ + existsSync: mock(() => true), + })); + const plugin = pagefindDevelop({ developStrategy: "eager" }); + // @ts-expect-error - The plugin only requires this bit of the Vite plugin plugin.configResolved({ root: process.cwd(), }); - expect(cpSync).toHaveBeenCalledWith( - resolve(process.cwd(), "build", "pagefind"), - resolve(process.cwd(), "public", "pagefind"), - { recursive: true }, - ); - expect(execSync).toHaveBeenCalledWith("npm run build:custom", { - cwd: process.cwd(), - }); + expect(cpSyncSpy).toHaveBeenCalledTimes(1); + expect(cpSyncSpy.mock.calls).toEqual([ + [ + resolve(process.cwd(), "build", "pagefind"), + resolve(process.cwd(), "public", "pagefind"), + { recursive: true }, + ], + ]); + expect(execSyncSpy).toHaveBeenCalledTimes(1); + expect(execSyncSpy.mock.calls).toEqual([ + ["npm run build", { cwd: process.cwd() }], + ]); }); }); }); diff --git a/test/pagefind.test.ts b/test/pagefind.test.ts index 12e7435..fca18a7 100644 --- a/test/pagefind.test.ts +++ b/test/pagefind.test.ts @@ -1,4 +1,4 @@ -import { describe, expect, it } from "vitest"; +import { describe, expect, it } from "bun:test"; import { pagefind } from "../src"; describe("pagefind", () => {