Skip to content

Add a failing test for printSnapshotAndReceived incorrectly recognizing sometimes indented lines as changed #9863

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

Open
wants to merge 3 commits into
base: main
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,24 @@ Snapshot: <m>"delete"</>
Received: <t>"insert"</>
`;

exports[`printSnapshotAndReceived ignore indentation ignores custom indentation of unchanged line 1`] = `
<m>- Snapshot - 2</>
<t>+ Received + 2</>

<m>- **some plugin-generated string with custom leading whitespace**</>
<t>+ **some plugin-generated string with custom leading whitespace**</>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should not be detected as changed because it's exactly the same line in both snapshot and received values


<d> Object {</>
<d> "props": Object {</>
<d> "alt": "Jest logo",</>
<m>- "class": "logo",</>
<t>+ "class": "logo round",</>
<d> "src": "/img/jest.svg",</>
<d> },</>
<d> "type": "img",</>
<d> }</>
`;

exports[`printSnapshotAndReceived ignore indentation markup delete 1`] = `
<m>- Snapshot - 2</>
<t>+ Received + 0</>
Expand Down
62 changes: 61 additions & 1 deletion packages/jest-snapshot/src/__tests__/printSnapshot.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -20,6 +20,7 @@ import {
bForeground3,
} from '../colors';
import {
addSerializer,
toMatchInlineSnapshot,
toMatchSnapshot,
toThrowErrorMatchingInlineSnapshot,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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';
Expand Down