All Projects → jest-community → Snapshot Diff

jest-community / Snapshot Diff

Licence: mit
Diffing snapshot utility for Jest

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Snapshot Diff

reducer-tester
Utilities for testing redux reducers
Stars: ✭ 19 (-96.12%)
Mutual labels:  jest, snapshot-testing, snapshot
Typescript Snapshots Plugin
Snapshots language service support for Typescript
Stars: ✭ 122 (-75.1%)
Mutual labels:  jest, snapshot, snapshot-testing
Enzyme To Json
Snapshot test your Enzyme wrappers
Stars: ✭ 954 (+94.69%)
Mutual labels:  jest, snapshot-testing
Babel Test
An opinionated library to make testing babel plugins easier.
Stars: ✭ 79 (-83.88%)
Mutual labels:  jest, snapshot
React Native Testing Example
Example of unit testing a React Native & Redux app using Jest (and Snapshots!)
Stars: ✭ 159 (-67.55%)
Mutual labels:  jest, snapshot
Snap Shot It
Smarter snapshot utility for Mocha and BDD test runners + data-driven testing!
Stars: ✭ 138 (-71.84%)
Mutual labels:  snapshot, snapshot-testing
Verify
Verify is a snapshot tool that simplifies the assertion of complex data models and documents.
Stars: ✭ 210 (-57.14%)
Mutual labels:  snapshot, snapshot-testing
React Nativeish
React Native / React Native Web Boilerplate
Stars: ✭ 106 (-78.37%)
Mutual labels:  jest, snapshot
Playbook Ios
📘A library for isolated developing UI components and automatically taking snapshots of them.
Stars: ✭ 830 (+69.39%)
Mutual labels:  snapshot, snapshot-testing
jest-file-snapshot
Jest matcher to write snapshots to a separate file instead of the default snapshot file used by Jest
Stars: ✭ 35 (-92.86%)
Mutual labels:  jest, snapshot-testing
Jest Image Snapshot
✨ Jest matcher for image comparisons. Most commonly used for visual regression testing.
Stars: ✭ 2,940 (+500%)
Mutual labels:  jest, snapshot
pytest-snapshot
A plugin for snapshot testing with pytest.
Stars: ✭ 68 (-86.12%)
Mutual labels:  snapshot-testing, snapshot
Cupaloy
Simple Go snapshot testing
Stars: ✭ 131 (-73.27%)
Mutual labels:  snapshot, snapshot-testing
Snapshooter
Snapshooter is a snapshot testing tool for .NET Core and .NET Framework
Stars: ✭ 118 (-75.92%)
Mutual labels:  snapshot, snapshot-testing
Preact Jest Snapshot Test Boilerplate
🚀 Test Preact components using Jest snapshots
Stars: ✭ 24 (-95.1%)
Mutual labels:  jest, snapshot
Syrupy
🥞 The sweeter pytest snapshot plugin
Stars: ✭ 73 (-85.1%)
Mutual labels:  snapshot, snapshot-testing
Snap Shot
Jest-like snapshot feature for the rest of us, works magically by finding the right caller function
Stars: ✭ 170 (-65.31%)
Mutual labels:  jest, snapshot
GFontsSpace
Preview: https://pankajladhar.github.io/GFontsSpace
Stars: ✭ 88 (-82.04%)
Mutual labels:  jest, snapshot-testing
Snapguidist
Snapshot testing for React Styleguidist
Stars: ✭ 287 (-41.43%)
Mutual labels:  jest, snapshot-testing
Gwt Material
A Google Material Design wrapper for GWT
Stars: ✭ 386 (-21.22%)
Mutual labels:  snapshot

snapshot-diff

Greenkeeper badge npm version

Diffing snapshot utility for Jest. Takes two values, and return their difference as a string, ready to be snapshotted with toMatchSnapshot(). Especially helpful when testing the difference between different React component states.

Installation

yarn add --dev snapshot-diff

Usage

With default jest matcher

const snapshotDiff = require('snapshot-diff');

test('snapshot difference between 2 strings', () => {
  expect(snapshotDiff(a, b)).toMatchSnapshot();
});

const React = require('react');
const Component = require('./Component');

test('snapshot difference between 2 React components state', () => {
  expect(
    snapshotDiff(<Component test="say" />, <Component test="my name" />)
  ).toMatchSnapshot();
});

With custom matcher

const { toMatchDiffSnapshot } = require('snapshot-diff');

expect.extend({ toMatchDiffSnapshot });

test('snapshot difference between 2 strings', () => {
  expect(a).toMatchDiffSnapshot(b);
});

const React = require('react');
const Component = require('./Component');

test('snapshot difference between 2 React components state', () => {
  expect(<Component test="say" />).toMatchDiffSnapshot(
    <Component test="my name" />
  );
});

... alternatively import it once, for instance in your tests setup file:

require('snapshot-diff/extend-expect');

Produced snapshot:

exports[`snapshot difference between 2 strings 1`] = `
"- First value
+ Second value


-  abcx
+  abcy
   "
`;

exports[`snapshot difference between 2 React components state 1`] = `
"- <Component test=\\"say\\" />
+ <Component test=\\"my name\\" />

@@ -27,11 +27,11 @@
   <span />
   <span />
   <span />
   <span />
   <span>
-    say
+    my name
   </span>
   <span />
   <span />
   <span />
   <span />"
`;

Custom serializers

By default, snapshot-diff uses a built in React serializer based on react-test-renderer. The serializers used can be set by calling setSerializers with an array of serializers to use. The order of serializers in this array may be important to you as serializers are tested in order until a match is found.

setSerializers can be used to add new serializers for unsupported data types, or to set a different serializer for React components. If you want to keep the default React serializer in place, don't forget to add the default serializers to your list of serializers!

Adding a new custom serializer

const snapshotDiff = require('snapshot-diff');
const myCustomSerializer = require('./my-custom-serializer');

snapshotDiff.setSerializers([
  ...snapshotDiff.defaultSerializers, // use default React serializer - add this if you want to serialise React components!
  myCustomSerializer
]);

Serializing React components with a different serializer

You can replace the default React serializer by omitting it from the serializer list. The following uses enzymes to-json serializer instead:

const snapshotDiff = require('snapshot-diff');
const enzymeToJson = require('enzyme-to-json/serializer');
const myCustomSerializer = require('./my-custom-serializer');

snapshotDiff.setSerializers([
  enzymeToJson, // using enzymes serializer instead
  myCustomSerializer
]);

Snapshot serializer

By default Jest adds extra quotes around strings so it makes diff snapshots of objects too noisy. To fix this – snapshot-diff comes with custom serializer, which you can add directly in your tests or in setupFiles script:

const snapshotDiff = require('snapshot-diff');

expect.addSnapshotSerializer(snapshotDiff.getSnapshotDiffSerializer());

test('snapshot difference between 2 objects', () => {
  expect(snapshotDiff({ foo: 'bar' }, { foo: 'baz' })).toMatchSnapshot();
});

...or add it globally to your jest config:

// jest.config.js
module.exports = {
  snapshotSerializers: [
    require.resolve('snapshot-diff/serializer.js'),
  ],
};

API

type Options = {
  expand?: boolean,
  colors?: boolean,
  contextLines?: number
};

// default export
snapshotDiff(valueA: any, valueB: any, options?: Options) => string
// custom matcher
expect(valueA: any).toMatchDiffSnapshot(valueB: any, options?: Options, testName?: string) => void

Options

  • expand: boolean (default: false) – expand the diff, so the whole information is preserved
  • colors: boolean (default: false) – preserve color information from Jest diff
  • contextLines: number (default: 5) - number of context lines to be shown at the beginning and at the end of a snapshot
  • stablePatchmarks: boolean (default: false) - prevent line number patch marks from appearing in diffs. This can be helpful when diffs are breaking only because of the patch marks. Changes @@ -1,1 +1,2 @@ to @@ --- --- @@.
  • aAnnotation: string (default: 'First Value') - the annotation indicating from which serialization the - lines are
  • bAnnotation: string (default: 'Second Value') - the annotation indicating from which serialization the + lines are

Project is MIT-licensed. Pull Requests welcome!

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].