diff --git a/packages/jest-snapshot/src/__tests__/__snapshots__/printSnapshot.test.ts.snap b/packages/jest-snapshot/src/__tests__/__snapshots__/printSnapshot.test.ts.snap index 0a6ee670fc5e..54310f363706 100644 --- a/packages/jest-snapshot/src/__tests__/__snapshots__/printSnapshot.test.ts.snap +++ b/packages/jest-snapshot/src/__tests__/__snapshots__/printSnapshot.test.ts.snap @@ -375,6 +375,24 @@ Snapshot: "delete" Received: "insert" `; +exports[`printSnapshotAndReceived ignore indentation ignores custom indentation of unchanged line 1`] = ` +- Snapshot - 2 ++ Received + 2 + +- **some plugin-generated string with custom leading whitespace** ++ **some plugin-generated string with custom leading whitespace** + + Object { + "props": Object { + "alt": "Jest logo", +- "class": "logo", ++ "class": "logo round", + "src": "/img/jest.svg", + }, + "type": "img", + } +`; + exports[`printSnapshotAndReceived ignore indentation markup delete 1`] = ` - Snapshot - 2 + Received + 0 diff --git a/packages/jest-snapshot/src/__tests__/printSnapshot.test.ts b/packages/jest-snapshot/src/__tests__/printSnapshot.test.ts index 88bc60664c63..c427d89bea21 100644 --- a/packages/jest-snapshot/src/__tests__/printSnapshot.test.ts +++ b/packages/jest-snapshot/src/__tests__/printSnapshot.test.ts @@ -8,7 +8,7 @@ import ansiRegex = require('ansi-regex'); import styles = require('ansi-styles'); import chalk = require('chalk'); -import format from 'pretty-format'; +import format, {NewPlugin} from 'pretty-format'; import { aBackground2, aBackground3, @@ -20,6 +20,7 @@ import { bForeground3, } from '../colors'; import { + addSerializer, toMatchInlineSnapshot, toMatchSnapshot, toThrowErrorMatchingInlineSnapshot, @@ -137,6 +138,19 @@ expect.addSnapshotSerializer({ }, }); +let customSerializer: NewPlugin | null = null; +addSerializer({ + serialize(...args) { + return customSerializer!.serialize(...args); + }, + test(val) { + if (!customSerializer) { + return false; + } + return customSerializer.test(val); + }, +}); + describe('chalk', () => { // Because these tests give code coverage of get functions // and give confidence that the escape sequences are correct, @@ -1267,6 +1281,52 @@ describe('printSnapshotAndReceived', () => { expect(testWithStringify(expected, received, false)).toMatchSnapshot(); }); + test('ignores custom indentation of unchanged line', () => { + try { + const pluginGeneratedContent = + ' **some plugin-generated string with custom leading whitespace**\n\n'; + const seen = new WeakSet(); + + const plugin: NewPlugin = { + serialize: (val, config, indentation, depth, refs, printer) => { + seen.add(val); + const serialized = printer(val, config, indentation, depth, refs); + seen.delete(val); + return pluginGeneratedContent + serialized; + }, + test: val => typeof val === 'object' && val.props && !seen.has(val), + }; + + customSerializer = plugin; + + const snapshot = { + props: { + alt: 'Jest logo', + class: 'logo', + src: '/img/jest.svg', + }, + type: 'img', + }; + const received = { + ...snapshot, + props: { + ...snapshot.props, + class: 'logo round', + }, + }; + expect( + printSnapshotAndReceived( + serialize(snapshot), + serialize(received), + received, + true, + ), + ).toMatchSnapshot(); + } finally { + customSerializer = null; + } + }); + describe('object', () => { const text = 'Ignore indentation in snapshot'; const time = '2019-11-11';