Skip to content

cronn/file-snapshots

Repository files navigation

File Snapshot Testing

This is a monorepo for writing tests with file snapshots, providing integrations for different testing libraries.

Available Integrations

File Snapshots

Introduction

File snapshots are stored in two directories: validationDir and outputDir. validationDir contains the golden masters which should be under version control. outputDir contains the snapshots from the latest test run. By default, the directories are located under /data/test/validation and /data/test/output.

Using two directories to store snapshots enables diffing using directory comparison. This gives fine-grained control when updating snapshots and facilitates features like detecting unused snapshots.

Adding New File Snapshots

When no validation file exists for a file snapshot, a new validation file is created containing a marker in the first line:

===== missing file =====

This explicitly marks the file as new. To use the snapshot as validation file:

  • Remove the marker line from the file
  • Add the file to version control

Updating Changed File Snapshots

Each test run generates new file snapshots in outputDir. Differences can be viewed using directory comparison.

If a file snapshot changed intentionally:

  • Apply the changes to the validation files
  • Commit the changed validation file to version control

Tips and Tricks

Navigating to validation files

Since validation files are named after the test, you can navigate to a validation file by using file search with the test title (e.g. Ctrl + Shift + N in IntelliJ)

Clearing the output directory

When regularly switching between branches with changed validation files, it can be helpful to clear the output directory. Otherwise, updated validation files will appear in the diff even though they did not change in the current branch.

Removing unused validation files

Unused validation files are validation files from tests which no longer exist, e.g. because the test was renamed or deleted. To avoid confusion, unused validation files should always be deleted.

Tools for Comparing File Snapshots

Effectively working with file snapshots requires a tool which supports comparing directories. We can recommend the following tools:

See Also

Development

This monorepo uses Turborepo as build system to manage task dependencies and enable task caching.

Common Tasks

Command Description
pnpm turbo check Runs code checks, including TypeScript compilation, linting, formatting and tests
pnpm turbo fix Applies automatic fixes, including linting and formatting
pnpm turbo build Runs the build
pnpm turbo ci Runs all tasks required for CI, including checks and builds

To run tasks for a specific package only, use filters:

pnpm turbo --filter=@cronn/playwright-file-snapshots check

About

Write tests with file snapshots in Playwright and Vitest.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •