Skip to content

Commit ea264ec

Browse files
authored
jest-snapshot: Display change counts in annotation lines (#8982)
* jest-snapshot: Display change counts in annotation lines * Edit comment * Update CHANGELOG.md * Move indicators for change counts from postfix to prefix * Display with labels if neither quoted string is multiline * Delete obsolete work-around variable assignment * Rename Padding variables
1 parent db7b1b4 commit ea264ec

File tree

12 files changed

+453
-258
lines changed

12 files changed

+453
-258
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
- `[jest-get-type]` Add `BigInt` support. ([#8382](https://github.com/facebook/jest/pull/8382))
2121
- `[jest-matcher-utils]` Add `BigInt` support to `ensureNumbers` `ensureActualIsNumber`, `ensureExpectedIsNumber` ([#8382](https://github.com/facebook/jest/pull/8382))
2222
- `[jest-runner]` Warn if a worker had to be force exited ([#8206](https://github.com/facebook/jest/pull/8206))
23+
- `[jest-snapshot]` Display change counts in annotation lines ([#8982](https://github.com/facebook/jest/pull/8982))
2324
- `[@jest/test-result]` Create method to create empty `TestResult` ([#8867](https://github.com/facebook/jest/pull/8867))
2425
- `[jest-worker]` [**BREAKING**] Return a promise from `end()`, resolving with the information whether workers exited gracefully ([#8206](https://github.com/facebook/jest/pull/8206))
2526

e2e/__tests__/toMatchSnapshot.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,9 @@ test('first snapshot fails, second passes', () => {
108108
writeFiles(TESTS_DIR, {[filename]: template([`'kiwi'`, `'banana'`])});
109109
const {stderr, exitCode} = runJest(DIR, ['-w=1', '--ci=false', filename]);
110110
expect(stderr).toMatch('Snapshot name: `snapshots 1`');
111-
expect(stderr).toMatch('Snapshot: "apple"\n Received: "kiwi"');
111+
// Match lines separately because empty line has been replaced with space:
112+
expect(stderr).toMatch('Snapshot: "apple"');
113+
expect(stderr).toMatch('Received: "kiwi"');
112114
expect(stderr).not.toMatch('1 obsolete snapshot found');
113115
expect(exitCode).toBe(1);
114116
}

packages/jest-diff/README.md

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,8 @@ const difference = diffLinesUnified(splitLines0(a), splitLines0(b), options);
173173
Given an empty string, `splitLines0(b)` returns `[]` an empty array, formatted as no `Received` lines:
174174

175175
```diff
176-
- Expected 3
177-
+ Received 0
176+
- Expected - 3
177+
+ Received + 0
178178

179179
- multi
180180
- line
@@ -194,8 +194,8 @@ const difference = diffLinesUnified(a.split('\n'), b.split('\n'), options);
194194
Given an empty string, `b.split('\n')` returns `['']` an array that contains an empty string, formatted as one empty `Received` line, which is **ambiguous** with an empty line:
195195

196196
```diff
197-
- Expected 3
198-
+ Received 1
197+
- Expected - 3
198+
+ Received + 1
199199

200200
- multi
201201
- line
@@ -220,16 +220,16 @@ You might call this function for case insensitive or Unicode equivalence compari
220220
import format from 'pretty-format';
221221

222222
const a = {
223-
action: 'MOVE_TO',
224-
x: 1,
225-
y: 2,
223+
text: 'Ignore indentation in serialized object',
224+
time: '2019-09-19T12:34:56.000Z',
225+
type: 'CREATE_ITEM',
226226
};
227227
const b = {
228-
action: 'MOVE_TO',
229228
payload: {
230-
x: 1,
231-
y: 2,
229+
text: 'Ignore indentation in serialized object',
230+
time: '2019-09-19T12:34:56.000Z',
232231
},
232+
type: 'CREATE_ITEM',
233233
};
234234

235235
const difference = diffLinesUnified2(
@@ -242,18 +242,18 @@ const difference = diffLinesUnified2(
242242
);
243243
```
244244

245-
The `x` and `y` properties are common, because their only difference is indentation:
245+
The `text` and `time` properties are common, because their only difference is indentation:
246246

247247
```diff
248248
- Expected
249249
+ Received
250250

251251
Object {
252-
action: 'MOVE_TO',
253252
+ payload: Object {
254-
x: 1,
255-
y: 2,
253+
text: 'Ignore indentation in serialized object',
254+
time: '2019-09-19T12:34:56.000Z',
256255
+ },
256+
type: 'CREATE_ITEM',
257257
}
258258
```
259259

@@ -519,8 +519,8 @@ const difference = diffDefault(a, b, options);
519519
```
520520

521521
```diff
522-
- Expected 1 -
523-
+ Received 2 +
522+
- Expected - 1
523+
+ Received + 2
524524

525525
Array [
526526
"common",

packages/jest-diff/src/__tests__/__snapshots__/diff.test.ts.snap

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ exports[`color of text (expanded) 1`] = `
3434
`;
3535
3636
exports[`context number of lines: -1 (5 default) 1`] = `
37-
<g>- Expected 1 -</>
38-
<r>+ Received 1 +</>
37+
<g>- Expected - 1</>
38+
<r>+ Received + 1</>
3939
4040
<d>@@ -6,9 +6,9 @@</>
4141
<d> 4,</>
@@ -51,8 +51,8 @@ exports[`context number of lines: -1 (5 default) 1`] = `
5151
`;
5252
5353
exports[`context number of lines: 0 1`] = `
54-
<g>- Expected 1 -</>
55-
<r>+ Received 1 +</>
54+
<g>- Expected - 1</>
55+
<r>+ Received + 1</>
5656
5757
<y>@@ -11,1 +11,0 @@</>
5858
<g>- 9,</>
@@ -61,8 +61,8 @@ exports[`context number of lines: 0 1`] = `
6161
`;
6262
6363
exports[`context number of lines: 1 1`] = `
64-
<g>- Expected 1 -</>
65-
<r>+ Received 1 +</>
64+
<g>- Expected - 1</>
65+
<r>+ Received + 1</>
6666
6767
<y>@@ -10,4 +10,4 @@</>
6868
<d> 8,</>
@@ -73,8 +73,8 @@ exports[`context number of lines: 1 1`] = `
7373
`;
7474
7575
exports[`context number of lines: 2 1`] = `
76-
<g>- Expected 1 -</>
77-
<r>+ Received 1 +</>
76+
<g>- Expected - 1</>
77+
<r>+ Received + 1</>
7878
7979
<y>@@ -9,6 +9,6 @@</>
8080
<d> 7,</>
@@ -87,8 +87,8 @@ exports[`context number of lines: 2 1`] = `
8787
`;
8888
8989
exports[`context number of lines: 3.1 (5 default) 1`] = `
90-
<g>- Expected 1 -</>
91-
<r>+ Received 1 +</>
90+
<g>- Expected - 1</>
91+
<r>+ Received + 1</>
9292
9393
<d>@@ -6,9 +6,9 @@</>
9494
<d> 4,</>
@@ -104,8 +104,8 @@ exports[`context number of lines: 3.1 (5 default) 1`] = `
104104
`;
105105
106106
exports[`context number of lines: undefined (5 default) 1`] = `
107-
<g>- Expected 1 -</>
108-
<r>+ Received 1 +</>
107+
<g>- Expected - 1</>
108+
<r>+ Received + 1</>
109109
110110
<d>@@ -6,9 +6,9 @@</>
111111
<d> 4,</>
@@ -121,36 +121,36 @@ exports[`context number of lines: undefined (5 default) 1`] = `
121121
`;
122122
123123
exports[`diffStringsUnified edge cases empty both a and b 1`] = `
124-
<g>- Expected 0 -</>
125-
<r>+ Received 0 +</>
124+
<g>- Expected - 0</>
125+
<r>+ Received + 0</>
126126
127127
128128
`;
129129
130130
exports[`diffStringsUnified edge cases empty only a 1`] = `
131-
<g>- Expected 0 -</>
132-
<r>+ Received 1 +</>
131+
<g>- Expected - 0</>
132+
<r>+ Received + 1</>
133133
134134
<r>+ one-line string</>
135135
`;
136136
137137
exports[`diffStringsUnified edge cases empty only b 1`] = `
138-
<g>- Expected 1 -</>
139-
<r>+ Received 0 +</>
138+
<g>- Expected - 1</>
139+
<r>+ Received + 0</>
140140
141141
<g>- one-line string</>
142142
`;
143143
144144
exports[`diffStringsUnified edge cases equal both non-empty 1`] = `
145-
<g>- Expected 0 -</>
146-
<r>+ Received 0 +</>
145+
<g>- Expected - 0</>
146+
<r>+ Received + 0</>
147147
148148
<d> one-line string</>
149149
`;
150150
151151
exports[`diffStringsUnified edge cases multiline has no common after clean up chaff 1`] = `
152-
<g>- Expected 2 -</>
153-
<r>+ Received 2 +</>
152+
<g>- Expected - 2</>
153+
<r>+ Received + 2</>
154154
155155
<g>- delete</>
156156
<g>- two</>
@@ -159,16 +159,16 @@ exports[`diffStringsUnified edge cases multiline has no common after clean up ch
159159
`;
160160
161161
exports[`diffStringsUnified edge cases one-line has no common after clean up chaff 1`] = `
162-
<g>- Expected 1 -</>
163-
<r>+ Received 1 +</>
162+
<g>- Expected - 1</>
163+
<r>+ Received + 1</>
164164
165165
<g>- delete</>
166166
<r>+ insert</>
167167
`;
168168
169169
exports[`falls back to not call toJSON if it throws and then objects have differences 1`] = `
170-
<g>- Expected 1 -</>
171-
<r>+ Received 1 +</>
170+
<g>- Expected - 1</>
171+
<r>+ Received + 1</>
172172
173173
<d> Object {</>
174174
<g>- "line": 1,</>
@@ -181,8 +181,8 @@ exports[`falls back to not call toJSON if serialization has no differences but t
181181
<d>Compared values serialize to the same structure.</>
182182
<d>Printing internal object structure without calling \`toJSON\` instead.</>
183183
184-
<g>- Expected 1 -</>
185-
<r>+ Received 1 +</>
184+
<g>- Expected - 1</>
185+
<r>+ Received + 1</>
186186
187187
<d> Object {</>
188188
<g>- "line": 1,</>
@@ -192,33 +192,33 @@ exports[`falls back to not call toJSON if serialization has no differences but t
192192
`;
193193
194194
exports[`oneline strings 1`] = `
195-
<g>- Expected 1 -</>
196-
<r>+ Received 1 +</>
195+
<g>- Expected - 1</>
196+
<r>+ Received + 1</>
197197
198198
<g>- ab</>
199199
<r>+ aa</>
200200
`;
201201
202202
exports[`oneline strings 2`] = `
203-
<g>- Expected 1 -</>
204-
<r>+ Received 1 +</>
203+
<g>- Expected - 1</>
204+
<r>+ Received + 1</>
205205
206206
<g>- 123456789</>
207207
<r>+ 234567890</>
208208
`;
209209
210210
exports[`oneline strings 3`] = `
211-
<g>- Expected 1 -</>
212-
<r>+ Received 2 +</>
211+
<g>- Expected - 1</>
212+
<r>+ Received + 2</>
213213
214214
<g>- oneline</>
215215
<r>+ multi</>
216216
<r>+ line</>
217217
`;
218218
219219
exports[`oneline strings 4`] = `
220-
<g>- Expected 2 -</>
221-
<r>+ Received 1 +</>
220+
<g>- Expected - 2</>
221+
<r>+ Received + 1</>
222222
223223
<g>- multi</>
224224
<g>- line</>
@@ -301,8 +301,8 @@ exports[`options includeChangeCounts false diffStringsUnified 1`] = `
301301
`;
302302
303303
exports[`options includeChangeCounts true padding diffLinesUnified a has 2 digits 1`] = `
304-
<g>- Before 10 -</>
305-
<r>+ After 1 +</>
304+
<g>- Before - 10</>
305+
<r>+ After + 1</>
306306
307307
<d> common</>
308308
<g>- a</>
@@ -319,8 +319,8 @@ exports[`options includeChangeCounts true padding diffLinesUnified a has 2 digit
319319
`;
320320
321321
exports[`options includeChangeCounts true padding diffLinesUnified b has 2 digits 1`] = `
322-
<g>- Before 1 -</>
323-
<r>+ After 10 +</>
322+
<g>- Before - 1</>
323+
<r>+ After + 10</>
324324
325325
<d> common</>
326326
<g>- a</>
@@ -337,8 +337,8 @@ exports[`options includeChangeCounts true padding diffLinesUnified b has 2 digit
337337
`;
338338
339339
exports[`options includeChangeCounts true padding diffStringsUnified 1`] = `
340-
<g>- Before 1 -</>
341-
<r>+ After 1 +</>
340+
<g>- Before - 1</>
341+
<r>+ After + 1</>
342342
343343
<g>- change <i>from</i></>
344344
<r>+ change <i>to</i></>

packages/jest-diff/src/printDiffs.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -162,16 +162,20 @@ export const printAnnotation = (
162162
const aCount = String(changeCounts.a);
163163
const bCount = String(changeCounts.b);
164164

165-
const aPadding =
166-
Math.max(bAnnotation.length - aAnnotation.length, 0) +
167-
Math.max(bCount.length - aCount.length, 0);
168-
const bPadding =
169-
Math.max(aAnnotation.length - bAnnotation.length, 0) +
170-
Math.max(aCount.length - bCount.length, 0);
171-
172-
// Separate annotation from count by padding plus margin of 2 spaces.
173-
aRest = ' '.repeat(aPadding + 2) + aCount + ' ' + aIndicator;
174-
bRest = ' '.repeat(bPadding + 2) + bCount + ' ' + bIndicator;
165+
// Padding right aligns the ends of the annotations.
166+
const baAnnotationLengthDiff = bAnnotation.length - aAnnotation.length;
167+
const aAnnotationPadding = ' '.repeat(Math.max(0, baAnnotationLengthDiff));
168+
const bAnnotationPadding = ' '.repeat(Math.max(0, -baAnnotationLengthDiff));
169+
170+
// Padding left aligns the ends of the counts.
171+
const baCountLengthDiff = bCount.length - aCount.length;
172+
const aCountPadding = ' '.repeat(Math.max(0, baCountLengthDiff));
173+
const bCountPadding = ' '.repeat(Math.max(0, -baCountLengthDiff));
174+
175+
aRest =
176+
aAnnotationPadding + ' ' + aIndicator + ' ' + aCountPadding + aCount;
177+
bRest =
178+
bAnnotationPadding + ' ' + bIndicator + ' ' + bCountPadding + bCount;
175179
}
176180

177181
return (

0 commit comments

Comments
 (0)