Skip to content

Commit 8d0b5e2

Browse files
authored
jest-snapshot: Omit irrelevant received properties when property matchers fail (#9198)
* jest-snapshot: Omit irrelevant received properties * Update CHANGELOG.md * Edit comment in test that hash is missing * Edit CHANGELOG.md * Add comment to explain import hack
1 parent 4643178 commit 8d0b5e2

File tree

4 files changed

+47
-11
lines changed

4 files changed

+47
-11
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
- `[jest-snapshot]` Distinguish empty string from external snapshot not written ([#8880](https://github.com/facebook/jest/pull/8880))
5353
- `[jest-snapshot]` [**BREAKING**] Distinguish empty string from internal snapshot not written ([#8898](https://github.com/facebook/jest/pull/8898))
5454
- `[jest-snapshot]` [**BREAKING**] Remove `report` method and throw matcher errors ([#9049](https://github.com/facebook/jest/pull/9049))
55+
- `[jest-snapshot]` Omit irrelevant `received` properties when property matchers fail ([#9198](https://github.com/facebook/jest/pull/9198))
5556
- `[jest-transform]` Properly cache transformed files across tests ([#8890](https://github.com/facebook/jest/pull/8890))
5657
- `[jest-transform]` Don't fail the test suite when a generated source map is invalid ([#9058](https://github.com/facebook/jest/pull/9058))
5758
- `[jest-utils]` Allow querying process.domain ([#9136](https://github.com/facebook/jest/pull/9136))

packages/jest-snapshot/src/__tests__/__snapshots__/printSnapshot.test.ts.snap

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -128,13 +128,11 @@ exports[`pass false toMatchInlineSnapshot with properties equals false with snap
128128
Snapshot name: \`with properties 1\`
129129

130130
<g>- Expected properties - 1</>
131-
<r>+ Received value + 3</>
131+
<r>+ Received value + 1</>
132132

133133
<d> Object {</>
134134
<g>- "id": "abcdef",</>
135135
<r>+ "id": "abcdefg",</>
136-
<r>+ "text": "Increase code coverage",</>
137-
<r>+ "type": "ADD_ITEM",</>
138136
<d> }</>
139137
`;
140138

@@ -144,13 +142,11 @@ exports[`pass false toMatchInlineSnapshot with properties equals false without s
144142
Snapshot name: \`with properties 1\`
145143

146144
<g>- Expected properties - 1</>
147-
<r>+ Received value + 3</>
145+
<r>+ Received value + 1</>
148146

149147
<d> Object {</>
150148
<g>- "id": "abcdef",</>
151149
<r>+ "id": "abcdefg",</>
152-
<r>+ "text": "Increase code coverage",</>
153-
<r>+ "type": "ADD_ITEM",</>
154150
<d> }</>
155151
`;
156152

@@ -211,13 +207,11 @@ exports[`pass false toMatchSnapshot with properties equals false isLineDiffable
211207
Snapshot name: \`with properties 1\`
212208

213209
<g>- Expected properties - 1</>
214-
<r>+ Received value + 3</>
210+
<r>+ Received value + 1</>
215211

216212
<d> Object {</>
217213
<g>- "id": "abcdef",</>
218214
<r>+ "id": "abcdefg",</>
219-
<r>+ "text": "Increase code coverage",</>
220-
<r>+ "type": "ADD_ITEM",</>
221215
<d> }</>
222216
`;
223217

@@ -246,6 +240,17 @@ Snapshot: <g>"inline snapshot"</>
246240
Received: <r>"received"</>
247241
`;
248242

243+
exports[`printPropertiesAndReceived omit missing properties 1`] = `
244+
<g>- Expected properties - 2</>
245+
<r>+ Received value + 1</>
246+
247+
<d> Object {</>
248+
<g>- "hash": Any<String>,</>
249+
<g>- "path": Any<String>,</>
250+
<r>+ "path": "…",</>
251+
<d> }</>
252+
`;
253+
249254
exports[`printSnapshotAndReceived backtick single line expected and received 1`] = `
250255
Snapshot: <g>"var foo = \`backtick\`;"</>
251256
Received: <r>"var foo = <i>tag</i>\`backtick\`;"</>

packages/jest-snapshot/src/__tests__/printSnapshot.test.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ import chalk = require('chalk');
1111
import format = require('pretty-format');
1212

1313
import jestSnapshot = require('../index');
14-
import {printSnapshotAndReceived} from '../printSnapshot';
14+
import {
15+
printPropertiesAndReceived,
16+
printSnapshotAndReceived,
17+
} from '../printSnapshot';
1518
import {serialize} from '../utils';
1619

1720
const convertAnsi = (val: string): string =>
@@ -599,6 +602,29 @@ describe('pass true', () => {
599602
});
600603
});
601604

605+
describe('printPropertiesAndReceived', () => {
606+
test('omit missing properties', () => {
607+
const received = {
608+
b: {},
609+
branchMap: {},
610+
f: {},
611+
fnMap: {},
612+
// hash is missing
613+
path: '…',
614+
s: {},
615+
statementMap: {},
616+
};
617+
const properties = {
618+
hash: expect.any(String),
619+
path: expect.any(String),
620+
};
621+
622+
expect(
623+
printPropertiesAndReceived(properties, received, false),
624+
).toMatchSnapshot();
625+
});
626+
});
627+
602628
describe('printSnapshotAndReceived', () => {
603629
// Simulate default serialization.
604630
const testWithStringify = (

packages/jest-snapshot/src/printSnapshot.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
*/
77

88
import chalk = require('chalk');
9+
// Temporary hack because getObjectSubset has known limitations,
10+
// is not in the public interface of the expect package,
11+
// and the long-term goal is to use a non-serialization diff.
12+
import {getObjectSubset} from 'expect/build/utils';
913
import {
1014
DIFF_DELETE,
1115
DIFF_EQUAL,
@@ -145,7 +149,7 @@ export const printPropertiesAndReceived = (
145149
if (isLineDiffable(properties) && isLineDiffable(received)) {
146150
return diffLinesUnified(
147151
serialize(properties).split('\n'),
148-
serialize(received).split('\n'),
152+
serialize(getObjectSubset(received, properties)).split('\n'),
149153
{
150154
aAnnotation,
151155
aColor: EXPECTED_COLOR,

0 commit comments

Comments
 (0)