
Security News
Inside Lodash’s Security Reset and Maintenance Reboot
Lodash 4.17.23 marks a security reset, with maintainers rebuilding governance and infrastructure to support long-term, sustainable maintenance.
snapshot-diff
Advanced tools
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.
yarn add --dev snapshot-diff
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();
});
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 />"
`;
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'),
],
};
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
expand: boolean (default: false) – expand the diff, so the whole information is preservedcolors: boolean (default: false) – preserve color information from Jest diffcontextLines: number (default: 5) - number of context lines to be shown at the beginning and at the end of a snapshotstablePatchmarks: 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 arebAnnotation: string (default: 'Second Value') - the annotation indicating from which serialization the + lines areProject is MIT-licensed. Pull Requests welcome!
FAQs
Diffing Jest snapshots utility
The npm package snapshot-diff receives a total of 67,650 weekly downloads. As such, snapshot-diff popularity was classified as popular.
We found that snapshot-diff demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Lodash 4.17.23 marks a security reset, with maintainers rebuilding governance and infrastructure to support long-term, sustainable maintenance.

Security News
n8n led JavaScript Rising Stars 2025 by a wide margin, with workflow platforms seeing the largest growth across categories.

Security News
The U.S. government is rolling back software supply chain mandates, shifting from mandatory SBOMs and attestations to a risk-based approach.