Skip to content

Commit cc2c92b

Browse files
authored
jest-diff: Rename some new options and change their default values (#9077)
* jest-diff: Rename some new options and change their default values * Edit noColor in README.md * Copy edit prose in README.md * Swap args consistent with destructuring in printDiffs * Update CHANGELOG.md * Fix prettier lint error in README.md * Modify yellowish test for common but not change * Replace example of hex with rgb in README.md * Replace arg with string in identify function in README
1 parent ca2d3a3 commit cc2c92b

File tree

11 files changed

+121
-65
lines changed

11 files changed

+121
-65
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
- `[jest-console]` Add missing `console.group` calls to `NullConsole` ([#9024](https://github.com/facebook/jest/pull/9024))
3535
- `[jest-core]` Don't include unref'd timers in --detectOpenHandles results ([#8941](https://github.com/facebook/jest/pull/8941))
3636
- `[jest-diff]` Do not inverse format if line consists of one change ([#8903](https://github.com/facebook/jest/pull/8903))
37+
- `[jest-diff]` Rename some new options and change their default values ([#9077](https://github.com/facebook/jest/pull/9077))
3738
- `[jest-fake-timers]` `getTimerCount` will not include cancelled immediates ([#8764](https://github.com/facebook/jest/pull/8764))
3839
- `[jest-leak-detector]` [**BREAKING**] Use `weak-napi` instead of `weak` package ([#8686](https://github.com/facebook/jest/pull/8686))
3940
- `[jest-mock]` Fix for mockReturnValue overriding mockImplementationOnce ([#8398](https://github.com/facebook/jest/pull/8398))

packages/jest-diff/README.md

Lines changed: 48 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -361,24 +361,25 @@ For other applications, you can provide an options object as a third argument:
361361

362362
### Properties of options object
363363

364-
| name | default |
365-
| :-------------------------------- | :--------------- |
366-
| `aAnnotation` | `'Expected'` |
367-
| `aColor` | `chalk.green` |
368-
| `aIndicator` | `'-'` |
369-
| `bAnnotation` | `'Received'` |
370-
| `bColor` | `chalk.red` |
371-
| `bIndicator` | `'+'` |
372-
| `changeColor` | `chalk.inverse` |
373-
| `commonColor` | `chalk.dim` |
374-
| `commonIndicator` | `' '` |
375-
| `contextLines` | `5` |
376-
| `expand` | `true` |
377-
| `firstOrLastEmptyLineReplacement` | `'↵'` |
378-
| `includeChangeCounts` | `false` |
379-
| `omitAnnotationLines` | `false` |
380-
| `patchColor` | `chalk.yellow` |
381-
| `trailingSpaceFormatter` | `chalk.bgYellow` |
364+
| name | default |
365+
| :-------------------------------- | :----------------- |
366+
| `aAnnotation` | `'Expected'` |
367+
| `aColor` | `chalk.green` |
368+
| `aIndicator` | `'-'` |
369+
| `bAnnotation` | `'Received'` |
370+
| `bColor` | `chalk.red` |
371+
| `bIndicator` | `'+'` |
372+
| `changeColor` | `chalk.inverse` |
373+
| `changeLineTrailingSpaceColor` | `string => string` |
374+
| `commonColor` | `chalk.dim` |
375+
| `commonIndicator` | `' '` |
376+
| `commonLineTrailingSpaceColor` | `string => string` |
377+
| `contextLines` | `5` |
378+
| `emptyFirstOrLastLinePlaceholder` | `''` |
379+
| `expand` | `true` |
380+
| `includeChangeCounts` | `false` |
381+
| `omitAnnotationLines` | `false` |
382+
| `patchColor` | `chalk.yellow` |
382383

383384
For more information about the options, see the following examples.
384385

@@ -425,27 +426,45 @@ Although the default inverse of foreground and background colors is hard to beat
425426
import chalk from 'chalk';
426427

427428
const options = {
428-
changeColor: chalk.bold.bgAnsi256(226), // #ffff00
429+
changeColor: chalk.bold.bgYellowBright,
429430
};
430431
```
431432

432433
### Example of option to format trailing spaces
433434

434435
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.
435436

436-
The formatter is a function, which given a string, returns a string.
437+
- If change lines have a background color, then you can see trailing spaces.
438+
- If common lines have default dim color, then you cannot see trailing spaces. You might want yellowish background color to see them.
437439

438-
If instead of yellowish background color, you want to replace trailing spaces with middle dot characters:
440+
```js
441+
const options = {
442+
aColor: chalk.rgb(128, 0, 128).bgRgb(255, 215, 255), // magenta
443+
bColor: chalk.rgb(0, 95, 0).bgRgb(215, 255, 215), // green
444+
commonLineTrailingSpaceColor: chalk.bgYellow,
445+
};
446+
```
447+
448+
The value of a Color option is a function, which given a string, returns a string.
449+
450+
If you want to replace trailing spaces with middle dot characters:
439451

440452
```js
453+
const replaceSpacesWithMiddleDot = string => '·'.repeat(string.length);
454+
441455
const options = {
442-
trailingSpaceFormatter: string => '·'.repeat(string.length),
456+
changeLineTrailingSpaceColor: replaceSpacesWithMiddleDot,
457+
commonLineTrailingSpaceColor: replaceSpacesWithMiddleDot,
443458
};
444459
```
445460

446-
### Example of options for no colors
461+
If you need the TypeScript type of a Color option:
447462

448-
The value of a color or formatter option is a function, which given a string, returns a string.
463+
```ts
464+
import {DiffOptionsColor} from 'jest-diff';
465+
```
466+
467+
### Example of options for no colors
449468

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

@@ -458,7 +477,6 @@ const options = {
458477
changeColor: noColor,
459478
commonColor: noColor,
460479
patchColor: noColor,
461-
trailingSpaceFormatter: noColor,
462480
};
463481
```
464482

@@ -551,19 +569,19 @@ const difference = diffStringsUnified(a, b, options);
551569
+ changed to
552570
```
553571

554-
### Example of option not to replace first or last empty lines
572+
### Example of option for empty first or last lines
555573

556574
If the **first** or **last** comparison line is **empty**, because the content is empty and the indicator is a space, you might not notice it.
557575

558-
Also, because Jest trims the report when a matcher fails, it deletes an empty last line.
576+
The replacement option is a string whose default value is `''` empty string.
559577

560-
The replacement is a string whose default value is `'↵'` U+21B5 downwards arrow with corner leftwards.
578+
Because Jest trims the report when a matcher fails, it deletes an empty last line.
561579

562-
To store the difference in a file without a replacement, because it could be ambiguous with the content of the arguments, provide an empty string:
580+
Therefore, Jest uses as placeholder the downwards arrow with corner leftwards:
563581

564582
```js
565583
const options = {
566-
firstOrLastEmptyLineReplacement: '',
584+
emptyFirstOrLastLinePlaceholder: '', // U+21B5
567585
};
568586
```
569587

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -363,15 +363,15 @@ exports[`options omitAnnotationLines true diffStringsUnified and includeChangeCo
363363

364364
exports[`options omitAnnotationLines true diffStringsUnified empty strings 1`] = ``;
365365

366-
exports[`options trailingSpaceFormatter diffDefault default yellowish 1`] = `
366+
exports[`options trailingSpaceFormatter diffDefault default no color 1`] = `
367367
<g>- Expected</>
368368
<r>+ Received</>
369369

370-
<g>- delete 1 trailing space:<Y> </></>
370+
<g>- delete 1 trailing space: </>
371371
<r>+ delete 1 trailing space:</>
372-
<d> common 2 trailing spaces:<Y> </></>
372+
<d> common 2 trailing spaces: </>
373373
<g>- insert 1 trailing space:</>
374-
<r>+ insert 1 trailing space:<Y> </></>
374+
<r>+ insert 1 trailing space: </>
375375
`;
376376

377377
exports[`options trailingSpaceFormatter diffDefault middle dot 1`] = `
@@ -384,3 +384,14 @@ exports[`options trailingSpaceFormatter diffDefault middle dot 1`] = `
384384
<g>- insert 1 trailing space:</>
385385
<r>+ insert 1 trailing space:·</>
386386
`;
387+
388+
exports[`options trailingSpaceFormatter diffDefault yellowish common 1`] = `
389+
<g>- Expected</>
390+
<r>+ Received</>
391+
392+
<g>- delete 1 trailing space: </>
393+
<r>+ delete 1 trailing space:</>
394+
<d> common 2 trailing spaces:<Y> </></>
395+
<g>- insert 1 trailing space:</>
396+
<r>+ insert 1 trailing space: </>
397+
`;

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import stripAnsi from 'strip-ansi';
1010
import {alignedAnsiStyleSerializer} from '@jest/test-utils';
1111

1212
import diff from '../';
13+
import {noColor} from '../normalizeDiffOptions';
1314
import {diffStringsUnified} from '../printDiffs';
1415
import {DiffOptions} from '../types';
1516

@@ -23,7 +24,6 @@ const NO_DIFF_MESSAGE = 'Compared values have no visual difference.';
2324
const stripped = (a: unknown, b: unknown) => stripAnsi(diff(a, b) || '');
2425

2526
// Use in toBe assertions for comparison lines.
26-
const noColor = (string: string) => string;
2727
const optionsBe: DiffOptions = {
2828
aColor: noColor,
2929
bColor: noColor,
@@ -905,13 +905,23 @@ describe('options', () => {
905905
'insert 1 trailing space: ',
906906
].join('\n');
907907

908-
test('diffDefault default yellowish', () => {
908+
test('diffDefault default no color', () => {
909909
expect(diff(aTrailingSpaces, bTrailingSpaces)).toMatchSnapshot();
910910
});
911911

912912
test('diffDefault middle dot', () => {
913+
const replaceSpacesWithMiddleDot = string => '·'.repeat(string.length);
913914
const options = {
914-
trailingSpaceFormatter: string => '·'.repeat(string.length),
915+
changeLineTrailingSpaceColor: replaceSpacesWithMiddleDot,
916+
commonLineTrailingSpaceColor: replaceSpacesWithMiddleDot,
917+
};
918+
919+
expect(diff(aTrailingSpaces, bTrailingSpaces, options)).toMatchSnapshot();
920+
});
921+
922+
test('diffDefault yellowish common', () => {
923+
const options = {
924+
commonLineTrailingSpaceColor: chalk.bgYellow,
915925
};
916926

917927
expect(diff(aTrailingSpaces, bTrailingSpaces, options)).toMatchSnapshot();

packages/jest-diff/src/__tests__/joinAlignedDiffs.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,19 @@ import {
1010
joinAlignedDiffsExpand,
1111
joinAlignedDiffsNoExpand,
1212
} from '../joinAlignedDiffs';
13-
import {normalizeDiffOptions} from '../normalizeDiffOptions';
13+
import {noColor, normalizeDiffOptions} from '../normalizeDiffOptions';
1414

1515
// To align columns so people can review snapshots confidently:
1616

1717
// 1. Use options to omit line colors.
18-
const identity = (string: string) => string;
1918
const changeColor = (string: string) => '<i>' + string + '</i>';
2019
const optionsNoColor = {
21-
aColor: identity,
22-
bColor: identity,
20+
aColor: noColor,
21+
bColor: noColor,
2322
changeColor,
24-
commonColor: identity,
25-
patchColor: identity,
23+
commonColor: noColor,
24+
emptyFirstOrLastLinePlaceholder: '↵', // U+21B5
25+
patchColor: noColor,
2626
};
2727

2828
// 2. Add string serializer to omit double quote marks.

packages/jest-diff/src/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,13 @@ import {DIFF_DELETE, DIFF_EQUAL, DIFF_INSERT, Diff} from './cleanupSemantic';
1212
import {diffLinesRaw, diffLinesUnified, diffLinesUnified2} from './diffLines';
1313
import {diffStringsRaw, diffStringsUnified, splitLines0} from './printDiffs';
1414
import {NO_DIFF_MESSAGE, SIMILAR_MESSAGE} from './constants';
15-
import {DiffOptions as ImportDiffOptions} from './types';
15+
import {
16+
DiffOptions as ImportDiffOptions,
17+
DiffOptionsColor as ImportDiffOptionsColor,
18+
} from './types';
1619

1720
export type DiffOptions = ImportDiffOptions;
21+
export type DiffOptionsColor = ImportDiffOptionsColor;
1822

1923
export {diffLinesRaw, diffLinesUnified, diffLinesUnified2};
2024
export {diffStringsRaw, diffStringsUnified, splitLines0};

packages/jest-diff/src/normalizeDiffOptions.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import chalk from 'chalk';
99

1010
import {DiffOptions, DiffOptionsNormalized} from './types';
1111

12+
export const noColor = (string: string) => string;
13+
1214
const DIFF_CONTEXT_DEFAULT = 5;
1315

1416
const OPTIONS_DEFAULT: DiffOptionsNormalized = {
@@ -19,15 +21,16 @@ const OPTIONS_DEFAULT: DiffOptionsNormalized = {
1921
bColor: chalk.red,
2022
bIndicator: '+',
2123
changeColor: chalk.inverse,
24+
changeLineTrailingSpaceColor: noColor,
2225
commonColor: chalk.dim,
2326
commonIndicator: ' ',
27+
commonLineTrailingSpaceColor: noColor,
2428
contextLines: DIFF_CONTEXT_DEFAULT,
29+
emptyFirstOrLastLinePlaceholder: '',
2530
expand: true,
26-
firstOrLastEmptyLineReplacement: '\u{21B5}', // downwards arrow with corner leftwards
2731
includeChangeCounts: false,
2832
omitAnnotationLines: false,
2933
patchColor: chalk.yellow,
30-
trailingSpaceFormatter: chalk.bgYellow,
3134
};
3235

3336
const getContextLines = (contextLines?: number): number =>

packages/jest-diff/src/printDiffs.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,17 @@ const printDiffLine = (
3232
isFirstOrLast: boolean,
3333
color: DiffOptionsColor,
3434
indicator: string,
35-
firstOrLastEmptyLineReplacement: string,
3635
trailingSpaceFormatter: DiffOptionsColor,
36+
emptyFirstOrLastLinePlaceholder: string,
3737
): string =>
3838
line.length !== 0
3939
? color(
4040
indicator + ' ' + formatTrailingSpaces(line, trailingSpaceFormatter),
4141
)
4242
: indicator !== ' '
4343
? color(indicator)
44-
: isFirstOrLast && firstOrLastEmptyLineReplacement.length !== 0
45-
? color(indicator + ' ' + firstOrLastEmptyLineReplacement)
44+
: isFirstOrLast && emptyFirstOrLastLinePlaceholder.length !== 0
45+
? color(indicator + ' ' + emptyFirstOrLastLinePlaceholder)
4646
: '';
4747

4848
export const printDeleteLine = (
@@ -51,17 +51,17 @@ export const printDeleteLine = (
5151
{
5252
aColor,
5353
aIndicator,
54-
firstOrLastEmptyLineReplacement,
55-
trailingSpaceFormatter,
54+
changeLineTrailingSpaceColor,
55+
emptyFirstOrLastLinePlaceholder,
5656
}: DiffOptionsNormalized,
5757
): string =>
5858
printDiffLine(
5959
line,
6060
isFirstOrLast,
6161
aColor,
6262
aIndicator,
63-
firstOrLastEmptyLineReplacement,
64-
trailingSpaceFormatter,
63+
changeLineTrailingSpaceColor,
64+
emptyFirstOrLastLinePlaceholder,
6565
);
6666

6767
export const printInsertLine = (
@@ -70,17 +70,17 @@ export const printInsertLine = (
7070
{
7171
bColor,
7272
bIndicator,
73-
firstOrLastEmptyLineReplacement,
74-
trailingSpaceFormatter,
73+
changeLineTrailingSpaceColor,
74+
emptyFirstOrLastLinePlaceholder,
7575
}: DiffOptionsNormalized,
7676
): string =>
7777
printDiffLine(
7878
line,
7979
isFirstOrLast,
8080
bColor,
8181
bIndicator,
82-
firstOrLastEmptyLineReplacement,
83-
trailingSpaceFormatter,
82+
changeLineTrailingSpaceColor,
83+
emptyFirstOrLastLinePlaceholder,
8484
);
8585

8686
export const printCommonLine = (
@@ -89,17 +89,17 @@ export const printCommonLine = (
8989
{
9090
commonColor,
9191
commonIndicator,
92-
firstOrLastEmptyLineReplacement,
93-
trailingSpaceFormatter,
92+
commonLineTrailingSpaceColor,
93+
emptyFirstOrLastLinePlaceholder,
9494
}: DiffOptionsNormalized,
9595
): string =>
9696
printDiffLine(
9797
line,
9898
isFirstOrLast,
9999
commonColor,
100100
commonIndicator,
101-
firstOrLastEmptyLineReplacement,
102-
trailingSpaceFormatter,
101+
commonLineTrailingSpaceColor,
102+
emptyFirstOrLastLinePlaceholder,
103103
);
104104

105105
export const hasCommonDiff = (diffs: Array<Diff>, isMultiline: boolean) => {

0 commit comments

Comments
 (0)