1. jest-file-snapshot
Jest matcher to write snapshots to a separate file instead of the default snapshot file used by Jest
jest-file-snapshot
Package: jest-file-snapshot
Created by: satya164
Last modified: Mon, 10 Jul 2023 22:21:30 GMT
Version: 0.5.0
License: MIT
Downloads: 41,760
Repository: https://github.com/satya164/jest-file-snapshot

Install

npm install jest-file-snapshot
yarn add jest-file-snapshot

jest-file-snapshot

Jest matcher to write snapshots to a separate file instead of the default snapshot file used by Jest. Writing a snapshot to a separate file means you have proper syntax highlighting in the output file, and better readability without those pesky escape characters. It's also useful if you have binary content.

Installation

 npm install --save-dev jest-file-snapshot

or

 yarn add --dev jest-file-snapshot

Usage

Extend Jest's expect:

 import { toMatchFile } from 'jest-file-snapshot';

expect.extend({ toMatchFile });

Then use it in your tests:

 it('matches content of file on disk', () => {
  expect(content).toMatchFile();
});

The content passed can be of type string or a Buffer. The comparison be done using Buffer.equals() instead of === if a Buffer is passed.

The matcher takes two optional arguments:

  • filepath: path to the file whose content should be matched, e.g. expect(content).toMatchFile(path.join(__dirname, 'output.md'))
  • options: additional options object for the matcher, with following properties:

You should also exclude the output files from Jest's watcher so that updating the snapshot doesn't re-run the tests again.

For example, by default toMatchFile uses a folder named __file_snapshots__ which you can exclude by adding the following under the jest key in package.json:

 "watchPathIgnorePatterns": [
  "__file_snapshots__"
]

Credits

RELATED POST

10 Must-Know Windows Shortcuts That Will Save You Time

10 Must-Know Windows Shortcuts That Will Save You Time

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Navigating AWS Networking: Essential Hacks for Smooth Operation

Navigating AWS Networking: Essential Hacks for Smooth Operation

Achieving Stunning Visuals with Unity's Global Illumination

Achieving Stunning Visuals with Unity's Global Illumination

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code