|
| 1 | +import { describe, it, expect } from "vitest"; |
| 2 | +import { existsSync } from "node:fs"; |
| 3 | +import { readFile, writeFile } from "node:fs/promises"; |
| 4 | +import { createFixture } from "./test-utils.ts"; |
| 5 | + |
| 6 | +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 | + }); |
| 14 | + |
| 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 | + }); |
| 23 | + |
| 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 | + }); |
| 28 | + |
| 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 | + }); |
| 35 | + |
| 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 | + }); |
| 43 | +}); |
0 commit comments