Skip to content

Commit

Permalink
Implement toMatchInlineDiffSnapshot custom matcher
Browse files Browse the repository at this point in the history
  • Loading branch information
domsli committed Mar 13, 2022
1 parent f042074 commit 6ba8535
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 2 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,13 @@ exports[`snapshot difference between 2 React components state 1`] = `
`;
```

### Inline Snapshots

You might want to use inline snapshots.

With a default matcher, you can use `toMatchInlineSnapshot` instead of
`toMatchSnapshot`. With a custom matcher, you can use `toMatchInlineDiffSnapshot` instead of `toMatchDiffSnapshot`.

## Custom serializers

By default, `snapshot-diff` uses a built in React serializer based on `react-test-renderer`. The
Expand Down
82 changes: 82 additions & 0 deletions __tests__/toMatchInlineDiffSnapshot.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
const snapshotDiff = require('../src/index');

const a = `
some
some
some
some
some
some
some
not
very
long
script
`;
const b = `
some
some
some
some
some
some
some
not
so
very
long
script
`;

beforeAll(() => {
expect.extend({
toMatchInlineDiffSnapshot: snapshotDiff.toMatchInlineDiffSnapshot,
});
});

test('works with default options', () => {
expect(a).toMatchInlineDiffSnapshot(
b,
`
"Snapshot Diff:
- First value
+ Second value
@@ -5,9 +5,10 @@
some
some
some
some
not
+ so
very
long
script
"
`
);
});

test('works with non-default options', () => {
expect(a).toMatchInlineDiffSnapshot(
b,
{ stablePatchmarks: true },
`
"Snapshot Diff:
- First value
+ Second value
@@ --- --- @@
some
some
some
some
not
+ so
very
long
script
"
`
);
});
4 changes: 2 additions & 2 deletions extend-expect.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* global expect */
const { toMatchDiffSnapshot } = require('./build/');
const { toMatchDiffSnapshot, toMatchInlineDiffSnapshot } = require('./build/');

expect.extend({ toMatchDiffSnapshot });
expect.extend({ toMatchDiffSnapshot, toMatchInlineDiffSnapshot });
4 changes: 4 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ declare module 'snapshot-diff' {
* make it available via `expect.extend({ toMatchDiffSnapshot })`.
*/
toMatchDiffSnapshot: jest.CustomMatcher;
/**
* Compare the changes from a to b with the diff inlined into tests
*/
toMatchInlineDiffSnapshot: jest.CustomMatcher;
/**
* By default Jest adds extra quotes around strings so it makes diff
* snapshots of objects too noisy. To fix this – snapshot-diff comes
Expand Down
23 changes: 23 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,28 @@ function toMatchDiffSnapshot(
return snapshot.toMatchSnapshot.call(this, difference, testName || '');
}

function toMatchInlineDiffSnapshot(
valueA: any,
valueB: any,
optionsOrSnapshot?: Options | string,
inlineSnapshot?: string
) {
let options = undefined;
if (typeof optionsOrSnapshot === 'string') {
inlineSnapshot = optionsOrSnapshot;
} else {
options = optionsOrSnapshot;
}

const difference = snapshotDiff(valueA, valueB, options);

if (inlineSnapshot) {
return snapshot.toMatchInlineSnapshot.call(this, difference, inlineSnapshot);
} else {
return snapshot.toMatchInlineSnapshot.call(this, difference)
}
}

function getSnapshotDiffSerializer() {
return {
test(value: any) {
Expand All @@ -109,6 +131,7 @@ function setSerializers(customSerializers) {
module.exports = snapshotDiff;
module.exports.snapshotDiff = snapshotDiff;
module.exports.toMatchDiffSnapshot = toMatchDiffSnapshot;
module.exports.toMatchInlineDiffSnapshot = toMatchInlineDiffSnapshot;
module.exports.getSnapshotDiffSerializer = getSnapshotDiffSerializer;
module.exports.setSerializers = setSerializers;
module.exports.defaultSerializers = defaultSerializers;

0 comments on commit 6ba8535

Please sign in to comment.