Skip to main content

Built and signed on GitHub Actions

A utility to create isolated test directories

This package works with Cloudflare Workers, Node.js, Deno, Bun
This package works with Cloudflare Workers
This package works with Node.js
This package works with Deno
This package works with Bun
JSR Score
100%
Published
3 weeks ago (0.1.4)

Testdirs - A utility to create a temporary directory with files and directories for testing.

Examples

Example 1

import { testdir, type TestdirOptions } 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();

Example 2

import { link, metadata, symlink, testdir, type TestdirOptions } 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"),
});

console.log(dir.path);

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

Example 3

import assert from "node:assert";
import { fromFileSystem, type FromFileSystemOptions, testdir, type TestdirOptions } 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);
Built and signed on
GitHub Actions
View transparency log

Add Package

deno add jsr:@luxass/testdirs

Import symbol

import * as testdirs from "@luxass/testdirs";

---- OR ----

Import directly with a jsr specifier

import * as testdirs from "jsr:@luxass/testdirs";

Add Package

npx jsr add @luxass/testdirs

Import symbol

import * as testdirs from "@luxass/testdirs";

Add Package

yarn dlx jsr add @luxass/testdirs

Import symbol

import * as testdirs from "@luxass/testdirs";

Add Package

pnpm dlx jsr add @luxass/testdirs

Import symbol

import * as testdirs from "@luxass/testdirs";

Add Package

bunx jsr add @luxass/testdirs

Import symbol

import * as testdirs from "@luxass/testdirs";