testdirs
TypeScript icon, indicating that this package has built-in type declarations

3.0.0 • Public • Published

testdirs

npm version npm downloads jsr version

A utility to create isolated test directories

[!NOTE] If you want to use this package with Vitest, we recommend using the vitest-testdirs with added support for Vitest.

📦 Installation

npm install testdirs --save-dev

🚀 Usage

import type { TestdirOptions } from "testdirs";
import { testdir } from "testdirs";

const testdirOptions = {
  dirname: "testdir", // default: a random directory name
} satisfies TestdirOptions;

const dir = await testdir({
  "file1.txt": "Hello, World!",
  "nested": {
    "file2.txt": "Hello, Nested!",
    "tests": {
      "file3.txt": "Hello, Tests!"
    }
  },

  // Alternatively, you can create nested directories with a flat path
  "nested/tests/file4.txt": "Hello, Tests!"
}, testdirOptions);

console.log(dir.path);

// you need to handle the removal process yourself!
await dir.remove();

Different Types of Files

You can create different types of files using the following methods:

import type { TestdirOptions } from "testdirs";
import { link, metadata, symlink, testdir } from "testdirs";

const dir = await testdir({
  "file1.txt": "Hello, World!",

  "nested": {
    "file2.txt": symlink("../file1.txt"),
    "file3.txt": metadata("Hello, World!", { mode: 0o444 }), // read-only file
  },

  "readonly": metadata({
    "file4.txt": "Hello, World!",
  }, { mode: 0o444 }), // read-only directory

  // creating a symlink pointing to file1.txt
  "symlink.txt": symlink("file1.txt"),
  "symlink2.txt": symlink("nested/file2.txt"),
  "link.txt": link("file1.txt"),
});

Create testdir from existing directory

You can create a testdir from an existing directory using the from method:

import type { FromFileSystemOptions, TestdirOptions, TestdirSyncFromOptions } from "testdirs";
import assert from "node:assert";
import { fromFileSystem, testdir } from "testdirs";

const fromFSOptions = {
  ignore: [".git"], // ignore everything inside the .git directory
  followLinks: false, // don't follow symlinks
  extras: {}, // extra files to add to the files object
  getEncodingForFile: (file) => "utf-8", // get the encoding for the file (default: utf-8)
} satisfies FromFileSystemOptions;

const dir = await testdir.from("path/to/existing/directory", {
  dirname: "testdir", // default: a random directory name
  fromFS: fromFSOptions,
});

// Alternatively, you can also use the `fromFileSystem` method to create the files object from the file system
const files = await fromFileSystem("path/to/existing/directory", fromFSOptions);

assert(files["file1.txt"] === "Hello, World!");

const testdirOptions = {
  dirname: "testdir", // default: a random directory name
} satisfies TestdirOptions;

const dir = await testdir(files, testdirOptions);

📄 License

Published under MIT License.

Readme

Keywords

Package Sidebar

Install

npm i testdirs

Weekly Downloads

764

Version

3.0.0

License

MIT

Unpacked Size

25.3 kB

Total Files

9

Last publish

Collaborators

  • luxass