Skip to content

Commit

Permalink
Moved to bun:test.
Browse files Browse the repository at this point in the history
  • Loading branch information
Hugos68 committed Jan 24, 2025
1 parent 3fd3d75 commit 1858a5d
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 117 deletions.
7 changes: 7 additions & 0 deletions bun.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"": {
"name": "vite-plugin-pagefind",
"dependencies": {
"@types/bun": "^1.2.0",
"colorette": "^2.0.20",
"package-manager-detector": "^0.2.8",
},
Expand Down Expand Up @@ -231,12 +232,16 @@

"@trysound/sax": ["@trysound/[email protected]", "", {}, "sha512-L7z9BgrNEcYyUYtF+HaEfiS5ebkh9jXqbszz7pC0hRBPaatV0XjSD3+eHrpqFemQfgwiFF0QPIarnIihIDn7OA=="],

"@types/bun": ["@types/[email protected]", "", { "dependencies": { "bun-types": "1.2.0" } }, "sha512-5N1JqdahfpBlAv4wy6svEYcd/YfO2GNrbL95JOmFx8nkE6dbK4R0oSE5SpBA4vBRqgrOUAXF8Dpiz+gi7r80SA=="],

"@types/estree": ["@types/[email protected]", "", {}, "sha512-AYnb1nQyY49te+VRAVgmzfcgjYS91mY5P0TKUDCLEM+gNnA+3T6rWITXRLYCpahpqSQbN5cE+gHpnPyXjHWxcw=="],

"@types/node": ["@types/[email protected]", "", { "dependencies": { "undici-types": "~6.20.0" } }, "sha512-Ir6hwgsKyNESl/gLOcEz3krR4CBGgliDqBQ2ma4wIhEx0w+xnoeTq3tdrNw15kU3SxogDjOgv9sqdtLW8mIHaw=="],

"@types/resolve": ["@types/[email protected]", "", {}, "sha512-60BCwRFOZCQhDncwQdxxeOEEkbc5dIMccYLwbxsS4TUNeVECQ/pBJ0j09mrHOl/JJvpRPGwO9SvE4nR2Nb/a4Q=="],

"@types/ws": ["@types/[email protected]", "", { "dependencies": { "@types/node": "*" } }, "sha512-bd/YFLW+URhBzMXurx7lWByOu+xzU9+kb3RboOteXYDfW+tr+JZa99OyNmPINEGB/ahzKrEuc8rcv4gnpJmxTw=="],

"@vitest/expect": ["@vitest/[email protected]", "", { "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/[email protected]", "", { "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=="],
Expand Down Expand Up @@ -273,6 +278,8 @@

"browserslist": ["[email protected]", "", { "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": ["[email protected]", "", { "dependencies": { "@types/node": "*", "@types/ws": "~8.5.10" } }, "sha512-KEaJxyZfbV/c4eyG0vyehDpYmBGreNiQbZIqvVHJwZ4BmeuWlNZ7EAzMN2Zcd7ailmS/tGVW0BgYbGf+lGEpWw=="],

"cac": ["[email protected]", "", {}, "sha512-b6Ilus+c3RrdDk+JhLKUAQfzzgLEPy6wcXqS7f/xe1EETvsDP6GORG7SFuOs6cID5YkqchW/LXZbX5bc8j7ZcQ=="],

"caniuse-api": ["[email protected]", "", { "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=="],
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
6 changes: 3 additions & 3 deletions test/pagefind-build.test.ts
Original file line number Diff line number Diff line change
@@ -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", () => {
Expand All @@ -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: {
Expand Down
215 changes: 104 additions & 111 deletions test/pagefind-develop.test.ts
Original file line number Diff line number Diff line change
@@ -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", () => {
Expand Down Expand Up @@ -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() }],
]);
});
});
});
2 changes: 1 addition & 1 deletion test/pagefind.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, expect, it } from "vitest";
import { describe, expect, it } from "bun:test";
import { pagefind } from "../src";

describe("pagefind", () => {
Expand Down

0 comments on commit 1858a5d

Please sign in to comment.