1. jest-specific-snapshot
[![CircleCI](https://circleci.com/gh/igor-dv/jest-specific-snapshot.svg?style=svg)](https://circleci.com/gh/igor-dv/jest-specific-snapshot)
jest-specific-snapshot
Package: jest-specific-snapshot
Created by: igor-dv
Last modified: Sun, 09 Jul 2023 22:10:13 GMT
Version: 8.0.0
License: MIT
Downloads: 887,762
Repository: https://github.com/igor-dv/jest-specific-snapshot

Install

npm install jest-specific-snapshot
yarn add jest-specific-snapshot

CircleCI


Jest Specific Snapshot

Jest matcher for multiple snapshot files per test

You can read about the implementation here

Installation

 npm i -D jest-specific-snapshot

Example

 const path = require('path');
// extend jest to have 'toMatchSpecificSnapshot' matcher
require('jest-specific-snapshot');

test('test', () => {
  // provides snapshot file with absolute file
  const pathToSnap = path.resolve(process.cwd(), './example/specific/dir/my.shot');
  expect(100).toMatchSpecificSnapshot(pathToSnap);

  //same snapshot but with relative file
  expect(14).toMatchSpecificSnapshot('./specific/dir/my.shot');

  // another snapshot file in the same test
  expect(19).toMatchSpecificSnapshot('./specific/another_dir/another.shot');
});

With Custom Serializer

 // extend jest to have 'toMatchSpecificSnapshot' matcher
const addSerializer = require('jest-specific-snapshot').addSerializer;

addSerializer(/* Add custom serializer here */);

test('test', () => {
  expect(/* thing that matches the custom serializer */).toMatchSpecificSnapshot(
    './specific/custom_serializer/test.shot'
  );
});

Extend toMatchSpecificSnapshot

 const toMatchSpecificSnapshot = require('jest-specific-snapshot').toMatchSpecificSnapshot;

expect.extend({
  toMatchDecoratedSpecificSnapshot(received, snapshotFile) {
    // You can modify received data or create dynamic snapshot path
    const data = doSomeThing(received);
    return toMatchSpecificSnapshot.call(this, data, snapshotFile);
  },
});

Limitations

  1. Snapshot files should have an extension other than .snap, since it conflicts with jest.
  2. In order to handle the --updateSnapshot (-u) parameter provided from CLI, there is an abuse of the SnapshotState._updateSnapshot private field. TBD - try to use the globalConfig to get this state.
  3. .toMatchSpecificSnapshot does ignore a custom serializers strategy. In order to support custom serializers, you should use the addSerializer method explicitly.

RELATED POST

Enhancing Vue.js Development: Harnessing the Potential of Vue-Loader

Enhancing Vue.js Development: Harnessing the Potential of Vue-Loader

Simplify Data Validation in Vue.js: A Step-by-Step Guide to Using Regex

Simplify Data Validation in Vue.js: A Step-by-Step Guide to Using Regex

Troubleshooting Made Easy: Common Issues and Solutions with vue-loader Without vue-cli

Troubleshooting Made Easy: Common Issues and Solutions with vue-loader Without vue-cli

Optimizing Webpack 4 with Vue CLI 3: Disabling the Cache-Loader

Optimizing Webpack 4 with Vue CLI 3: Disabling the Cache-Loader

Step-by-Step Guide: How to Add a Function to Your Vuex Plugin

Step-by-Step Guide: How to Add a Function to Your Vuex Plugin