diff --git a/README.md b/README.md index 6c9bfac..c887aca 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/__tests__/__snapshots__/snapshotDiff.test.js.snap b/__tests__/__snapshots__/snapshotDiff.test.js.snap index b3b9d94..cc50a69 100644 --- a/__tests__/__snapshots__/snapshotDiff.test.js.snap +++ b/__tests__/__snapshots__/snapshotDiff.test.js.snap @@ -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 diff --git a/__tests__/snapshotDiff.test.js b/__tests__/snapshotDiff.test.js index 29ce66b..8e6b1ff 100644 --- a/__tests__/snapshotDiff.test.js +++ b/__tests__/snapshotDiff.test.js @@ -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, @@ -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', () => { diff --git a/src/index.js b/src/index.js index 5809294..8651a6e 100644 --- a/src/index.js +++ b/src/index.js @@ -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', }; @@ -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; };