Socket
Book a DemoInstallSign in
Socket

vitest-directory-snapshot

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

vitest-directory-snapshot

## Overview

latest
npmnpm
Version
0.6.1
Version published
Maintainers
1
Created
Source

vitest-directory-snapshot

Overview

vitest-directory-snapshot is a testing utility for Vitest that enables snapshot testing of directory structures. It provides an extended version of Vitest's test function, along with a custom matcher, to assert that a directory (its structure and file contents) matches a stored snapshot. This allows you to easily compare changes in file system hierarchies during testing.

Installation

Install the package along with Vitest (if not already installed) using your package manager. For example, using pnpm:

pnpm install vitest-directory-snapshot
pnpm install vitest --save-dev

Public API

The package "vitest-directory-snapshot" exposes the following public API:

  • test
    • An extended version of Vitest's test function.
    • It automatically supports directory snapshot testing via a temporary directory fixture.

  • toMatchDirSnapshot(received: unknown)
    • A custom Vitest matcher that compares a given directory against a stored snapshot.
    • It expects an absolute path (string) to a directory and verifies that its structure and file contents match the stored snapshot.

Example Usage

import { describe, expect } from "vitest";
import { test, toMatchDirSnapshot } from "vitest-directory-snapshot";

describe("Directory Snapshot Tests", () => {
  test("snapshot comparison", () => {
    // Provide an absolute path to the directory you want to verify
    expect("/absolute/path/to/directory").toMatchDirSnapshot();
  });
});

Matcher Initialization

To register the custom matcher in Vitest, add a setup file (e.g. setupFile.ts) with the following content:

import { createToMatchDirSnapshot } from "vitest-directory-snapshot";

expect.extend({
  toMatchDirSnapshot: createToMatchDirSnapshot({ snapshotDirName: "custom_snapshots" }),
});

You can also configure the custom snapshot directory name by setting the environment variable VITEST_DIR_SNAPSHOT_DIR when running tests.

FAQs

Package last updated on 31 May 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts