Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/old-mugs-stare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@bomb.sh/tools": patch
---

Adds `@bomb.sh/tools/test-utils` export with `createFixture()`
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
".": {
"import": "./dist/bin.mjs"
},
"./test-utils": {
"types": "./dist/test-utils.d.mts",
"import": "./dist/test-utils.mjs"
},
"./*": "./dist/*",
"./package.json": "./package.json",
"./tsconfig.json": "./tsconfig.json"
Expand Down
43 changes: 43 additions & 0 deletions src/test-utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { describe, it, expect } from "vitest";
import { existsSync } from "node:fs";
import { readFile, writeFile } from "node:fs/promises";
import { createFixture } from "./test-utils.ts";

describe("createFixture", () => {
it("creates files on disk from inline tree", async () => {
const fixture = await createFixture({
"hello.txt": "hello world",
});
const content = await readFile(fixture.resolve("hello.txt"), "utf8");
expect(content).toBe("hello world");
});

it("creates nested directories from slash-separated keys", async () => {
const fixture = await createFixture({
"src/index.ts": "export const x = 1",
"src/utils/helpers.ts": "export function help() {}",
});
expect(existsSync(fixture.resolve("src/index.ts"))).toBe(true);
expect(existsSync(fixture.resolve("src/utils/helpers.ts"))).toBe(true);
});

it("resolve returns absolute path within fixture root", async () => {
const fixture = await createFixture({ "a.txt": "" });
expect(fixture.resolve("a.txt").toString()).toContain(fixture.root.toString());
});

it("readFile reads the actual file", async () => {
const fixture = await createFixture({ "a.txt": "Empty" });
expect(await fixture.readFile("a.txt")).toEqual("Empty");
await writeFile(fixture.resolve("a.txt"), "Hello world!", { encoding: "utf-8" });
expect(await fixture.readFile("a.txt")).toEqual("Hello world!");
});

it("cleanup removes the temp directory", async () => {
const fixture = await createFixture({ "a.txt": "" });
const path = fixture.root;
expect(existsSync(path)).toBe(true);
await fixture.cleanup();
expect(existsSync(path)).toBe(false);
});
});
40 changes: 40 additions & 0 deletions src/test-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import type { PathLike } from "node:fs";
import { mkdtemp, mkdir, writeFile, rm, readFile as fsReadFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { sep } from "node:path";
import { fileURLToPath, pathToFileURL } from "node:url";
import { onTestFinished } from "vitest";

export interface Fixture {
root: URL;
resolve: (...segments: string[]) => URL;
readFile: (file: PathLike) => Promise<string>;
cleanup: () => Promise<void>;
}

export async function createFixture(files: Record<string, string>): Promise<Fixture> {
const root = new URL(`bsh-`, `file://${tmpdir()}/`);
const path = await mkdtemp(fileURLToPath(root));
const base = pathToFileURL(path + sep);

for (const [name, content] of Object.entries(files)) {
const url = new URL(name, base);
const dir = new URL("./", url);
await mkdir(dir, { recursive: true });
await writeFile(url, content, "utf8");
}

const cleanup = () => rm(path, { recursive: true, force: true });
onTestFinished(cleanup);

const resolve = (...segments: string[]) => new URL(`./${segments.join("/")}`, base);
const readFile = (file: PathLike) =>
fsReadFile(new URL(`./${file}`, base), { encoding: "utf-8" });

return {
root: pathToFileURL(path),
resolve,
readFile,
cleanup,
};
}
Loading