Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement toMatchInlineDiffSnapshot custom matcher #339

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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;