Skip to content

Commit c297f1d

Browse files
ericbiewenerthymikee
authored andcommitted
feat: add stablePatchmarks option (#72)
Solves #69. I'm identifying patch marks with a simple regex that looks for a line starting with `@`. This seems safe since `jest-diff` appears to add whitespace at the start of all lines. Therefore, even a line of content that begins with `@` will end up padded with whitespace in the diff itself. And rather than simply stripping out the patchmarks, I'm replacing them with `-------------`. Since the point of this is to avoid broken diffs, this solves that issue while still giving some indication that there are hidden lines in between those parts of the diff. Perhaps there's a better replacement indicator than that. We could certainly preserve the patch mark `@@` notation like `@@ ------------- @@` if we think that helps communicate the meaning of the new mark.
1 parent 1a898ca commit c297f1d

File tree

4 files changed

+87
-0
lines changed

4 files changed

+87
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,8 @@ expect(valueA: any).toMatchDiffSnapshot(valueB: any, options?: Options, testName
133133
- `expand: boolean` (default: `false`) – expand the diff, so the whole information is preserved
134134
- `colors: boolean` (default: `false`) – preserve color information from Jest diff
135135
- `contextLines: number` (default: 5) - number of context lines to be shown at the beginning and at the end of a snapshot
136+
- `stablePatchmarks: boolean` (default: `false`) - prevent line number patch marks from appearing in
137+
diffs. This can be helpful when diffs are breaking only because of the patch marks. Changes `@@ -1,1 +1,2 @@` to `@@ --- --- @@`.
136138
- `aAnnotation: string` (default: `'First Value'`) - the annotation indicating from which serialization the `-` lines are
137139
- `bAnnotation: string` (default: `'Second Value'`) - the annotation indicating from which serialization the `+` lines are
138140

__tests__/__snapshots__/snapshotDiff.test.js.snap

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,37 @@ exports[`can use contextLines with React components 1`] = `
6464
+ my name"
6565
`;
6666

67+
exports[`can use stablePatchmarks on diff 1`] = `
68+
"Snapshot Diff:
69+
- First value
70+
+ Second value
71+
72+
@@ --- --- @@
73+
@some
74+
@some
75+
@some
76+
@some
77+
@some
78+
- @foo
79+
+ @bar
80+
@some
81+
@some
82+
@some
83+
@some
84+
@some
85+
@@ --- --- @@
86+
@some
87+
@some
88+
@some
89+
@some
90+
@not
91+
+ @so
92+
@very
93+
@long
94+
@script
95+
"
96+
`;
97+
6798
exports[`collapses diffs and strips ansi by default 1`] = `
6899
"Snapshot Diff:
69100
- First value

__tests__/snapshotDiff.test.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,51 @@ const b = `
3030
long
3131
script
3232
`;
33+
const noIndentA = `
34+
@some
35+
@some
36+
@some
37+
@some
38+
@some
39+
@foo
40+
@some
41+
@some
42+
@some
43+
@some
44+
@some
45+
@some
46+
@some
47+
@some
48+
@some
49+
@some
50+
@not
51+
@very
52+
@long
53+
@script
54+
`;
55+
const noIndentB = `
56+
@some
57+
@some
58+
@some
59+
@some
60+
@some
61+
@bar
62+
@some
63+
@some
64+
@some
65+
@some
66+
@some
67+
@some
68+
@some
69+
@some
70+
@some
71+
@some
72+
@not
73+
@so
74+
@very
75+
@long
76+
@script
77+
`;
3378

3479
type Props = {
3580
test: string,
@@ -154,6 +199,10 @@ test('can use contextLines with React components', () => {
154199
).toMatchSnapshot();
155200
});
156201

202+
test('can use stablePatchmarks on diff', () => {
203+
expect(snapshotDiff(noIndentA, noIndentB, { stablePatchmarks: true })).toMatchSnapshot();
204+
});
205+
157206
describe('failed optional deps', () => {
158207
beforeEach(() => {
159208
jest.mock('react-test-renderer', () => {

src/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const defaultOptions = {
2121
expand: false,
2222
colors: false,
2323
contextLines: -1, // Forces to use default from Jest
24+
stablePatchmarks: false,
2425
aAnnotation: 'First value',
2526
bAnnotation: 'Second value',
2627
};
@@ -43,6 +44,10 @@ const snapshotDiff = (valueA: any, valueB: any, options?: Options): string => {
4344
difference = stripAnsi(difference);
4445
}
4546

47+
if (mergedOptions.stablePatchmarks && !mergedOptions.expand) {
48+
difference = difference.replace(/^@@ -[0-9]+,[0-9]+ \+[0-9]+,[0-9]+ @@$/gm, '@@ --- --- @@')
49+
}
50+
4651
return SNAPSHOT_TITLE + difference;
4752
};
4853

0 commit comments

Comments
 (0)