Skip to content

Commit 736edd2

Browse files
authored
jest-diff: Add trailingSpaceFormatter option and replace cyan with commonColor (#8927)
* jest-diff: Add trailingSpaceFormatter option and replace cyan with commonColor * Update CHANGELOG.md
1 parent 70df308 commit 736edd2

File tree

12 files changed

+195
-396
lines changed

12 files changed

+195
-396
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
- `[jest-diff]` [**BREAKING**] Export as ECMAScript module ([#8873](https://github.com/facebook/jest/pull/8873))
1313
- `[jest-diff]` Add `includeChangeCounts` and rename `Indicator` options ([#8881](https://github.com/facebook/jest/pull/8881))
1414
- `[jest-diff]` Add `changeColor` and `patchColor` options ([#8911](https://github.com/facebook/jest/pull/8911))
15+
- `[jest-diff]` Add `trailingSpaceFormatter` option and replace cyan with `commonColor` ([#8927](https://github.com/facebook/jest/pull/8927))
1516
- `[jest-environment-jsdom]` Add `fakeTimersLolex` ([#8925](https://github.com/facebook/jest/pull/8925))
1617
- `[jest-environment-node]` Add `fakeTimersLolex` ([#8925](https://github.com/facebook/jest/pull/8925))
1718
- `[@jest/fake-timers]` Add Lolex as implementation of fake timers ([#8897](https://github.com/facebook/jest/pull/8897))

packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ trailing space" and "without trailing space" 1`] = `
325325
<red>+ Received</>
326326

327327
<green>- with<inverse>out</> trailing space</>
328-
<red>+ with·</>
328+
<red>+ with<bgYellow> </></>
329329
<red>+ trailing space</>"
330330
`;
331331

@@ -3603,8 +3603,8 @@ exports[`.toStrictEqual() displays substring diff for multiple lines 1`] = `
36033603
<green>- Expected</>
36043604
<red>+ Received</>
36053605

3606-
<green>- 6<inverse>9</> |·</>
3607-
<red>+ 6<inverse>8</> |·</>
3606+
<green>- 6<inverse>9</> |<bgYellow> </></>
3607+
<red>+ 6<inverse>8</> |<bgYellow> </></>
36083608
<green>- <inverse>70</> | test('assert.doesNotThrow', () => {</>
36093609
<red>+ <inverse>69</> | test('assert.doesNotThrow', () => {</>
36103610
<green>- > 7<inverse>1</> | assert.doesNotThrow(() => {</>

packages/jest-diff/README.md

Lines changed: 84 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,24 @@ To add this package as a dependency of a project, run either of the following co
1818

1919
## Usage of default export
2020

21-
Given values and optional options, `diffLinesUnified(a, b, options?)` does the following:
21+
Given values and optional options, `diffDefault(a, b, options?)` does the following:
2222

2323
- **serialize** the values as strings using the `pretty-format` package
2424
- **compare** the strings line-by-line using the `diff-sequences` package
2525
- **format** the changed or common lines using the `chalk` package
2626

2727
To use this function, write either of the following:
2828

29-
- `const diffLinesUnified = require('jest-diff').default;` in CommonJS modules
30-
- `import diffLinesUnified from 'jest-diff';` in ECMAScript modules
29+
- `const diffDefault = require('jest-diff').default;` in CommonJS modules
30+
- `import diffDefault from 'jest-diff';` in ECMAScript modules
3131

3232
### Example of default export
3333

3434
```js
35-
const a = ['delete', 'change from', 'common'];
36-
const b = ['change to', 'insert', 'common'];
35+
const a = ['delete', 'changed from', 'common'];
36+
const b = ['changed to', 'insert', 'common'];
3737

38-
const difference = diffLinesUnified(a, b);
38+
const difference = diffDefault(a, b);
3939
```
4040

4141
The returned **string** consists of:
@@ -49,8 +49,8 @@ The returned **string** consists of:
4949

5050
Array [
5151
- "delete",
52-
- "change from",
53-
+ "change to",
52+
- "changed from",
53+
+ "changed to",
5454
+ "insert",
5555
"common",
5656
]
@@ -82,8 +82,8 @@ Write either of the following:
8282
### Example of diffStringsUnified
8383

8484
```js
85-
const a = 'change from\ncommon';
86-
const b = 'change to\ncommon';
85+
const a = 'changed from\ncommon';
86+
const b = 'changed to\ncommon';
8787

8888
const difference = diffStringsUnified(a, b);
8989
```
@@ -97,8 +97,8 @@ The returned **string** consists of:
9797
- Expected
9898
+ Received
9999

100-
- change from
101-
+ change to
100+
- changed from
101+
+ changed to
102102
common
103103
```
104104

@@ -146,11 +146,11 @@ The value at index `1` is a substring of `a` or `b` or both.
146146
### Example of diffStringsRaw with cleanup
147147

148148
```js
149-
const diffs = diffStringsRaw('change from', 'change to', true);
149+
const diffs = diffStringsRaw('changed from', 'changed to', true);
150150

151151
/*
152152
diffs[0][0] === 0 // DIFF_EQUAL
153-
diffs[0][1] === 'change '
153+
diffs[0][1] === 'changed '
154154
155155
diffs[1][0] === -1 // DIFF_DELETE
156156
diffs[1][1] === 'from'
@@ -163,11 +163,11 @@ diffs[2][1] === 'to'
163163
### Example of diffStringsRaw without cleanup
164164

165165
```js
166-
const diffs = diffStringsRaw('change from', 'change to', false);
166+
const diffs = diffStringsRaw('changed from', 'changed to', false);
167167

168168
/*
169169
diffs[0][0] === 0 // DIFF_EQUAL
170-
diffs[0][1] === 'change '
170+
diffs[0][1] === 'changed '
171171
172172
diffs[1][0] === -1 // DIFF_DELETE
173173
diffs[1][1] === 'fr'
@@ -196,7 +196,7 @@ To write a **formatting** function, you might need the named constants (and `Dif
196196
If you write an application-specific **cleanup** algorithm, then you might need to call the `Diff` constructor:
197197

198198
```js
199-
const diffCommon = new Diff(DIFF_EQUAL, 'change ');
199+
const diffCommon = new Diff(DIFF_EQUAL, 'changed ');
200200
const diffDelete = new Diff(DIFF_DELETE, 'from');
201201
const diffInsert = new Diff(DIFF_INSERT, 'to');
202202
```
@@ -207,27 +207,30 @@ The default options are for the report when an assertion fails from the `expect`
207207

208208
For other applications, you can provide an options object as a third argument:
209209

210-
- `diffLinesUnified(a, b, options)`
210+
- `diffDefault(a, b, options)`
211211
- `diffStringsUnified(a, b, options)`
212212

213213
### Properties of options object
214214

215-
| name | default |
216-
| :-------------------- | :-------------- |
217-
| `aAnnotation` | `'Expected'` |
218-
| `aColor` | `chalk.green` |
219-
| `aIndicator` | `'-'` |
220-
| `bAnnotation` | `'Received'` |
221-
| `bColor` | `chalk.red` |
222-
| `bIndicator` | `'+'` |
223-
| `changeColor` | `chalk.inverse` |
224-
| `commonColor` | `chalk.dim` |
225-
| `commonIndicator` | `' '` |
226-
| `contextLines` | `5` |
227-
| `expand` | `true` |
228-
| `includeChangeCounts` | `false` |
229-
| `omitAnnotationLines` | `false` |
230-
| `patchColor` | `chalk.yellow` |
215+
| name | default |
216+
| :----------------------- | :--------------- |
217+
| `aAnnotation` | `'Expected'` |
218+
| `aColor` | `chalk.green` |
219+
| `aIndicator` | `'-'` |
220+
| `bAnnotation` | `'Received'` |
221+
| `bColor` | `chalk.red` |
222+
| `bIndicator` | `'+'` |
223+
| `changeColor` | `chalk.inverse` |
224+
| `commonColor` | `chalk.dim` |
225+
| `commonIndicator` | `' '` |
226+
| `contextLines` | `5` |
227+
| `expand` | `true` |
228+
| `includeChangeCounts` | `false` |
229+
| `omitAnnotationLines` | `false` |
230+
| `patchColor` | `chalk.yellow` |
231+
| `trailingSpaceFormatter` | `chalk.bgYellow` |
232+
233+
For more information about the options, see the following examples.
231234

232235
### Example of options for labels
233236

@@ -240,6 +243,15 @@ const options = {
240243
};
241244
```
242245

246+
```diff
247+
- Original
248+
+ Modified
249+
250+
- changed from
251+
+ changed to
252+
common
253+
```
254+
243255
The `jest-diff` package does not assume that the 2 labels have equal length.
244256

245257
### Example of options for colors of changed lines
@@ -257,29 +269,46 @@ const options = {
257269

258270
### Example of option for color of changed substrings
259271

260-
Although the default inverse of foreground and background colors is hard to beat for changed substrings **within lines**, especially because it highlights spaces, if you want bold font weight on yellow background:
272+
Although the default inverse of foreground and background colors is hard to beat for changed substrings **within lines**, especially because it highlights spaces, if you want bold font weight on yellow background color:
261273

262274
```js
275+
import chalk from 'chalk';
276+
263277
const options = {
264278
changeColor: chalk.bold.bgAnsi256(226), // #ffff00
265279
};
266280
```
267281

282+
### Example of option to format trailing spaces
283+
284+
Because the default export does not display substring differences within lines, formatting can help you see when lines differ by the presence or absence of trailing spaces found by `/\s+$/` regular expression.
285+
286+
The formatter is a function, which given a string, returns a string.
287+
288+
If instead of yellowish background color, you want to replace trailing spaces with middle dot characters:
289+
290+
```js
291+
const options = {
292+
trailingSpaceFormatter: string => '·'.repeat(string.length),
293+
};
294+
```
295+
268296
### Example of options for no colors
269297

270-
The value of a color option is a function, which given a string, returns a string.
298+
The value of a color or formatter option is a function, which given a string, returns a string.
271299

272300
To store the difference in a file without escape codes for colors, provide an identity function:
273301

274302
```js
275-
const identity = string => string;
303+
const noColor = string => string;
276304

277305
const options = {
278-
aColor: identity,
279-
bColor: identity,
280-
changeColor: identity,
281-
commonColor: identity,
282-
patchColor: identity,
306+
aColor: noColor,
307+
bColor: noColor,
308+
changeColor: noColor,
309+
commonColor: noColor,
310+
patchColor: noColor,
311+
trailingSpaceFormatter: noColor,
283312
};
284313
```
285314

@@ -316,6 +345,8 @@ A patch mark like `@@ -12,7 +12,9 @@` accounts for omitted common lines.
316345
If you want patch marks to have the same dim color as common lines:
317346

318347
```js
348+
import chalk from 'chalk';
349+
319350
const options = {
320351
expand: false,
321352
patchColor: chalk.dim,
@@ -327,22 +358,23 @@ const options = {
327358
To display the number of changed lines at the right of annotation lines:
328359

329360
```js
330-
const a = ['change from', 'common'];
331-
const b = ['change to', 'insert', 'common'];
361+
const a = ['changed from', 'common'];
362+
const b = ['changed to', 'insert', 'common'];
363+
332364
const options = {
333365
includeChangeCounts: true,
334366
};
335367

336-
const difference = diffLinesUnified(a, b, options);
368+
const difference = diffDefault(a, b, options);
337369
```
338370

339371
```diff
340372
- Expected 1 -
341373
+ Received 2 +
342374

343375
Array [
344-
- "change from",
345-
+ "change to",
376+
- "changed from",
377+
+ "changed to",
346378
+ "insert",
347379
"common",
348380
]
@@ -353,8 +385,9 @@ const difference = diffLinesUnified(a, b, options);
353385
To display only the comparison lines:
354386

355387
```js
356-
const a = 'change from\ncommon';
357-
const b = 'change to\ncommon';
388+
const a = 'changed from\ncommon';
389+
const b = 'changed to\ncommon';
390+
358391
const options = {
359392
omitAnnotationLines: true,
360393
};
@@ -363,7 +396,7 @@ const difference = diffStringsUnified(a, b, options);
363396
```
364397

365398
```diff
366-
- change from
367-
+ change to
399+
- changed from
400+
+ changed to
368401
common
369402
```

0 commit comments

Comments
 (0)