Skip to content

Commit 0eeee28

Browse files
committed
feat(test-utils): improve createFixture
1 parent ed01773 commit 0eeee28

File tree

5 files changed

+298
-80
lines changed

5 files changed

+298
-80
lines changed

.changeset/smooth-bushes-smell.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@bomb.sh/tools": patch
3+
---
4+
5+
Adds support for nested object syntax, dynamic file creation, and better fs bindings to createFixture

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@
4848
},
4949
"dependencies": {
5050
"@bomb.sh/args": "^0.3.1",
51+
"@humanfs/node": "^0.16.7",
52+
"@humanfs/types": "^0.15.0",
5153
"@typescript/native-preview": "7.0.0-dev.20260307.1",
5254
"knip": "^5.85.0",
5355
"oxfmt": "^0.36.0",

pnpm-lock.yaml

Lines changed: 33 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/test-utils.test.ts

Lines changed: 31 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,41 @@
11
import { describe, it, expect } from "vitest";
22
import { existsSync } from "node:fs";
3-
import { readFile, writeFile } from "node:fs/promises";
43
import { createFixture } from "./test-utils.ts";
54

65
describe("createFixture", () => {
7-
it("creates files on disk from inline tree", async () => {
8-
const fixture = await createFixture({
9-
"hello.txt": "hello world",
10-
});
11-
const content = await readFile(fixture.resolve("hello.txt"), "utf8");
12-
expect(content).toBe("hello world");
13-
});
6+
it("creates files on disk from inline tree", async () => {
7+
const fixture = await createFixture({
8+
"hello.txt": "hello world",
9+
});
10+
expect(await fixture.text("hello.txt")).toBe("hello world");
11+
});
1412

15-
it("creates nested directories from slash-separated keys", async () => {
16-
const fixture = await createFixture({
17-
"src/index.ts": "export const x = 1",
18-
"src/utils/helpers.ts": "export function help() {}",
19-
});
20-
expect(existsSync(fixture.resolve("src/index.ts"))).toBe(true);
21-
expect(existsSync(fixture.resolve("src/utils/helpers.ts"))).toBe(true);
22-
});
13+
it("creates nested directories from slash-separated keys", async () => {
14+
const fixture = await createFixture({
15+
"src/index.ts": "export const x = 1",
16+
"src/utils/helpers.ts": "export function help() {}",
17+
});
18+
expect(await fixture.isFile("src/index.ts")).toBe(true);
19+
expect(await fixture.isFile("src/utils/helpers.ts")).toBe(true);
20+
});
2321

24-
it("resolve returns absolute path within fixture root", async () => {
25-
const fixture = await createFixture({ "a.txt": "" });
26-
expect(fixture.resolve("a.txt").toString()).toContain(fixture.root.toString());
27-
});
22+
it("resolve returns absolute path within fixture root", async () => {
23+
const fixture = await createFixture({ "a.txt": "" });
24+
expect(fixture.resolve("a.txt").toString()).toContain(fixture.root.toString());
25+
});
2826

29-
it("readFile reads the actual file", async () => {
30-
const fixture = await createFixture({ "a.txt": "Empty" });
31-
expect(await fixture.readFile("a.txt")).toEqual("Empty");
32-
await writeFile(fixture.resolve("a.txt"), "Hello world!", { encoding: "utf-8" });
33-
expect(await fixture.readFile("a.txt")).toEqual("Hello world!");
34-
});
27+
it("text reads the actual file", async () => {
28+
const fixture = await createFixture({ "a.txt": "Empty" });
29+
expect(await fixture.text("a.txt")).toEqual("Empty");
30+
await fixture.write("a.txt", "Hello world!");
31+
expect(await fixture.text("a.txt")).toEqual("Hello world!");
32+
});
3533

36-
it("cleanup removes the temp directory", async () => {
37-
const fixture = await createFixture({ "a.txt": "" });
38-
const path = fixture.root;
39-
expect(existsSync(path)).toBe(true);
40-
await fixture.cleanup();
41-
expect(existsSync(path)).toBe(false);
42-
});
34+
it("cleanup removes the temp directory", async () => {
35+
const fixture = await createFixture({ "a.txt": "" });
36+
const path = fixture.root;
37+
expect(fixture.isDirectory(fixture.root)).toBe(true);
38+
await fixture.cleanup();
39+
expect(existsSync(path)).toBe(false);
40+
});
4341
});

0 commit comments

Comments
 (0)