Go Back to projects

vitest-testdirs

A utility for Vitest to create isolated test directories

📦 Installation

npm install vitest-testdirs --save-dev

🚀 Usage

import { readFileSync } from "node:fs";
// index.test.ts
import { readFile } from "node:fs/promises";
import { describe, expect, vi } from "vitest";
import { testdir, testdirSync } from "vitest-testdirs";

describe("testdir", () => {
  it("isolated-test", async () => {
    const path = await testdir({
      "file1.txt": "Hello, World!",
      "file2.txt": "Hello, Vitest!",
    });

    expect(path).toBeDefined();
    expect(path).toContain(".vitest-testdirs/vitest-testdir-isolated-test");

    const file = await readFile(`${path}/file1.txt`, "utf8");
    expect(file).toBe("Hello, World!");
  });
});

describe("testdirSync", () => {
  it("isolated-test", () => {
    const path = testdirSync({
      "file1.txt": "Hello, World!",
      "file2.txt": "Hello, Vitest!",
    });

    expect(path).toBeDefined();
    expect(path).toContain(".vitest-testdirs/vitest-testdirSync-isolated-test");

    const file = readFileSync(`${path}/file1.txt`, "utf8");
    expect(file).toBe("Hello, World!");
  });
});

📄 License

Published under MIT License.