Skip to content

Commit 95688ac

Browse files
authored
jest-diff: Add includeChangeCounts and rename Indicator options (#8881)
* jest-diff: Add includeChangeCounts option * Update CHANGELOG.md * Rename Symbol options as Indicator like git diff * Display indicator following change counts * Update the example diff in README.md
1 parent ff81c28 commit 95688ac

File tree

8 files changed

+371
-124
lines changed

8 files changed

+371
-124
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
- `[jest-config]` [**BREAKING**] Set default display name color based on runner ([#8689](https://github.com/facebook/jest/pull/8689))
77
- `[jest-diff]` Add options for colors and symbols ([#8841](https://github.com/facebook/jest/pull/8841))
88
- `[jest-diff]` [**BREAKING**] Export as ECMAScript module ([#8873](https://github.com/facebook/jest/pull/8873))
9+
- `[jest-diff]` Add `includeChangeCounts` and rename `Indicator` options ([#8881](https://github.com/facebook/jest/pull/8881))
910
- `[jest-runner]` Warn if a worker had to be force exited ([#8206](https://github.com/facebook/jest/pull/8206))
1011
- `[@jest/test-result]` Create method to create empty `TestResult` ([#8867](https://github.com/facebook/jest/pull/8867))
1112
- `[jest-worker]` [**BREAKING**] Return a promise from `end()`, resolving with the information whether workers exited gracefully ([#8206](https://github.com/facebook/jest/pull/8206))

packages/jest-diff/README.md

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ const difference = diffLinesUnified(a, b);
4040

4141
The returned **string** consists of:
4242

43-
- annotation lines: describe the two change symbols with labels, and a blank line
43+
- annotation lines: describe the two change indicators with labels, and a blank line
4444
- comparison lines: similar to “unified” view on GitHub, but `Expected` lines are green, `Received` lines are red, and common lines are dim (by default, see Options)
4545

4646
```diff
@@ -90,7 +90,7 @@ const difference = diffStringsUnified(a, b);
9090

9191
The returned **string** consists of:
9292

93-
- annotation lines: describe the two change symbols with labels, and a blank line
93+
- annotation lines: describe the two change indicators with labels, and a blank line
9494
- comparison lines: similar to “unified” view on GitHub, and **changed substrings** have **inverted** foreground and background colors (which the following example does not show)
9595

9696
```diff
@@ -107,9 +107,9 @@ The returned **string** consists of:
107107
Here are edge cases for the return value:
108108

109109
- both `a` and `b` are empty strings: no comparison lines
110-
- only `a` is empty string: all comparison lines have `bColor` and `bSymbol` (see Options)
111-
- only `b` is empty string: all comparison lines have `aColor` and `aSymbol` (see Options)
112-
- `a` and `b` are equal non-empty strings: all comparison lines have `commonColor` and `commonSymbol` (see Options)
110+
- only `a` is empty string: all comparison lines have `bColor` and `bIndicator` (see Options)
111+
- only `b` is empty string: all comparison lines have `aColor` and `aIndicator` (see Options)
112+
- `a` and `b` are equal non-empty strings: all comparison lines have `commonColor` and `commonIndicator` (see Options)
113113

114114
### Performance of diffStringsUnified
115115

@@ -184,7 +184,7 @@ diffs[4][1] === 'm'
184184
*/
185185
```
186186

187-
## Advanced import for diffStringsRaw
187+
### Advanced import for diffStringsRaw
188188

189189
Here are all the named imports for the `diffStringsRaw` function:
190190

@@ -216,14 +216,15 @@ For other applications, you can provide an options object as a third argument:
216216
| :-------------------- | :------------ |
217217
| `aAnnotation` | `'Expected'` |
218218
| `aColor` | `chalk.green` |
219-
| `aSymbol` | `'-'` |
219+
| `aIndicator` | `'-'` |
220220
| `bAnnotation` | `'Received'` |
221221
| `bColor` | `chalk.red` |
222-
| `bSymbol` | `'+'` |
222+
| `bIndicator` | `'+'` |
223223
| `commonColor` | `chalk.dim` |
224-
| `commonSymbol` | `' '` |
224+
| `commonIndicator` | `' '` |
225225
| `contextLines` | `5` |
226226
| `expand` | `true` |
227+
| `includeChangeCounts` | `false` |
227228
| `omitAnnotationLines` | `false` |
228229

229230
### Example of options for labels
@@ -264,18 +265,18 @@ const options = {
264265
};
265266
```
266267

267-
### Example of options for symbols
268+
### Example of options for indicators
268269

269-
For consistency with the `diff` command, you might replace the symbols:
270+
For consistency with the `diff` command, you might replace the indicators:
270271

271272
```js
272273
const options = {
273-
aSymbol: '<',
274-
bSymbol: '>',
274+
aIndicator: '<',
275+
bIndicator: '>',
275276
};
276277
```
277278

278-
The `jest-diff` package assumes (but does not enforce) that the 3 symbols have equal length.
279+
The `jest-diff` package assumes (but does not enforce) that the 3 indicators have equal length.
279280

280281
### Example of options to limit common lines
281282

@@ -292,6 +293,32 @@ const options = {
292293

293294
A patch mark like `@@ -12,7 +12,9 @@` accounts for omitted common lines.
294295

296+
### Example of option to include change counts
297+
298+
To display the number of change lines at the right of annotation lines:
299+
300+
```js
301+
const a = ['change from', 'common'];
302+
const b = ['change to', 'insert', 'common'];
303+
const options = {
304+
includeChangeCounts: true,
305+
};
306+
307+
const difference = diffLinesUnified(a, b, options);
308+
```
309+
310+
```diff
311+
- Expected 1 -
312+
+ Received 2 +
313+
314+
Array [
315+
- "change from",
316+
+ "change to",
317+
+ "insert",
318+
"common",
319+
]
320+
```
321+
295322
### Example of option to omit annotation lines
296323

297324
To display only the comparison lines:

0 commit comments

Comments
 (0)