Giter Site home page Giter Site logo

jest-specific-snapshot's Introduction

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.

jest-specific-snapshot's People

Contributors

dependabot[bot] avatar hulkish avatar igor-dv avatar mochiya98 avatar pixelatex avatar simenb avatar torifat avatar zvictor avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.