Skip to content

feat: add stablePatchmarks option #72

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

Merged
merged 6 commits into from
Mar 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ expect(valueA: any).toMatchDiffSnapshot(valueB: any, options?: Options, testName
- `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

Expand Down
31 changes: 31 additions & 0 deletions __tests__/__snapshots__/snapshotDiff.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,37 @@ exports[`can use contextLines with React components 1`] = `
+ my name"
`;

exports[`can use stablePatchmarks on diff 1`] = `
"Snapshot Diff:
- First value
+ Second value

@@ --- --- @@
@some
@some
@some
@some
@some
- @foo
+ @bar
@some
@some
@some
@some
@some
@@ --- --- @@
@some
@some
@some
@some
@not
+ @so
@very
@long
@script
"
`;

exports[`collapses diffs and strips ansi by default 1`] = `
"Snapshot Diff:
- First value
Expand Down
49 changes: 49 additions & 0 deletions __tests__/snapshotDiff.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,51 @@ const b = `
long
script
`;
const noIndentA = `
@some
@some
@some
@some
@some
@foo
@some
@some
@some
@some
@some
@some
@some
@some
@some
@some
@not
@very
@long
@script
`;
const noIndentB = `
@some
@some
@some
@some
@some
@bar
@some
@some
@some
@some
@some
@some
@some
@some
@some
@some
@not
@so
@very
@long
@script
`;

type Props = {
test: string,
Expand Down Expand Up @@ -154,6 +199,10 @@ test('can use contextLines with React components', () => {
).toMatchSnapshot();
});

test('can use stablePatchmarks on diff', () => {
expect(snapshotDiff(noIndentA, noIndentB, { stablePatchmarks: true })).toMatchSnapshot();
});

describe('failed optional deps', () => {
beforeEach(() => {
jest.mock('react-test-renderer', () => {
Expand Down
5 changes: 5 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const defaultOptions = {
expand: false,
colors: false,
contextLines: -1, // Forces to use default from Jest
stablePatchmarks: false,
aAnnotation: 'First value',
bAnnotation: 'Second value',
};
Expand All @@ -43,6 +44,10 @@ const snapshotDiff = (valueA: any, valueB: any, options?: Options): string => {
difference = stripAnsi(difference);
}

if (mergedOptions.stablePatchmarks && !mergedOptions.expand) {
difference = difference.replace(/^@@ -[0-9]+,[0-9]+ \+[0-9]+,[0-9]+ @@$/gm, '@@ --- --- @@')
}

return SNAPSHOT_TITLE + difference;
};

Expand Down