Skip to content

Commit 92095ab

Browse files
authored
Use named exports for jest-diff (#11371)
1 parent 3f6b420 commit 92095ab

File tree

9 files changed

+28
-26
lines changed

9 files changed

+28
-26
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
- `[jest-console]` `console.dir` now respects the second argument correctly ([#10638](https://github.com/facebook/jest/pull/10638))
6666
- `[jest-core]` Don't report PerformanceObserver as open handle ([#11123](https://github.com/facebook/jest/pull/11123))
6767
- `[jest-core]` Use `WeakRef` to hold timers when detecting open handles ([#11277](https://github.com/facebook/jest/pull/11277))
68+
- `[jest-diff]` [**BREAKING**] Use only named exports ([#11371](https://github.com/facebook/jest/pull/11371))
6869
- `[jest-each]` [**BREAKING**] Ignore excess words in headings ([#8766](https://github.com/facebook/jest/pull/8766))
6970
- `[jest-each]` Support array index with template strings ([#10763](https://github.com/facebook/jest/pull/10763))
7071
- `[jest-each]` Interpolate `%%` correctly ([#11364](https://github.com/facebook/jest/pull/11364))

docs/ExpectAPI.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ There are a number of helpful tools exposed on `this.utils` primarily consisting
156156
The most useful ones are `matcherHint`, `printExpected` and `printReceived` to format the error messages nicely. For example, take a look at the implementation for the `toBe` matcher:
157157

158158
```js
159-
const diff = require('jest-diff');
159+
const {diff} = require('jest-diff');
160160
expect.extend({
161161
toBe(received, expected) {
162162
const options = {

docs/JestPlatform.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Tool for visualizing changes in data. Exports a function that compares two value
3232
### Example
3333

3434
```javascript
35-
const diff = require('jest-diff').default;
35+
const {diff} = require('jest-diff');
3636

3737
const a = {a: {b: {c: 5}}};
3838
const b = {a: {b: {c: 6}}};

packages/jest-diff/README.md

+11-11
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
Display differences clearly so people can review changes confidently.
44

5-
The default export serializes JavaScript **values**, compares them line-by-line, and returns a string which includes comparison lines.
5+
The `diff` named export serializes JavaScript **values**, compares them line-by-line, and returns a string which includes comparison lines.
66

77
Two named exports compare **strings** character-by-character:
88

@@ -21,26 +21,26 @@ To add this package as a dependency of a project, run either of the following co
2121
- `npm install jest-diff`
2222
- `yarn add jest-diff`
2323

24-
## Usage of default export
24+
## Usage of `diff()`
2525

26-
Given JavaScript **values**, `diffDefault(a, b, options?)` does the following:
26+
Given JavaScript **values**, `diff(a, b, options?)` does the following:
2727

2828
1. **serialize** the values as strings using the `pretty-format` package
2929
2. **compare** the strings line-by-line using the `diff-sequences` package
3030
3. **format** the changed or common lines using the `chalk` package
3131

3232
To use this function, write either of the following:
3333

34-
- `const diffDefault = require('jest-diff').default;` in CommonJS modules
35-
- `import diffDefault from 'jest-diff';` in ECMAScript modules
34+
- `const {diff} = require('jest-diff');` in CommonJS modules
35+
- `import {diff} from 'jest-diff';` in ECMAScript modules
3636

37-
### Example of default export
37+
### Example of `diff()`
3838

3939
```js
4040
const a = ['delete', 'common', 'changed from'];
4141
const b = ['common', 'changed to', 'insert'];
4242

43-
const difference = diffDefault(a, b);
43+
const difference = diff(a, b);
4444
```
4545

4646
The returned **string** consists of:
@@ -61,7 +61,7 @@ The returned **string** consists of:
6161
]
6262
```
6363

64-
### Edge cases of default export
64+
### Edge cases of `diff()`
6565

6666
Here are edge cases for the return value:
6767

@@ -374,7 +374,7 @@ The default options are for the report when an assertion fails from the `expect`
374374

375375
For other applications, you can provide an options object as a third argument:
376376

377-
- `diffDefault(a, b, options)`
377+
- `diff(a, b, options)`
378378
- `diffStringsUnified(a, b, options)`
379379
- `diffLinesUnified(aLines, bLines, options)`
380380
- `diffLinesUnified2(aLinesDisplay, bLinesDisplay, aLinesCompare, bLinesCompare, options)`
@@ -452,7 +452,7 @@ const options = {
452452

453453
### Example of option to format trailing spaces
454454

455-
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.
455+
Because `diff()` 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.
456456

457457
- If change lines have a background color, then you can see trailing spaces.
458458
- If common lines have default dim color, then you cannot see trailing spaces. You might want yellowish background color to see them.
@@ -553,7 +553,7 @@ const options = {
553553
includeChangeCounts: true,
554554
};
555555

556-
const difference = diffDefault(a, b, options);
556+
const difference = diff(a, b, options);
557557
```
558558

559559
```diff

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ exports[`options omitAnnotationLines true diffStringsUnified and includeChangeCo
365365

366366
exports[`options omitAnnotationLines true diffStringsUnified empty strings 1`] = ``;
367367

368-
exports[`options trailingSpaceFormatter diffDefault default no color 1`] = `
368+
exports[`options trailingSpaceFormatter diff default no color 1`] = `
369369
<g>- Expected</>
370370
<r>+ Received</>
371371

@@ -376,7 +376,7 @@ exports[`options trailingSpaceFormatter diffDefault default no color 1`] = `
376376
<r>+ insert 1 trailing space: </>
377377
`;
378378

379-
exports[`options trailingSpaceFormatter diffDefault middle dot 1`] = `
379+
exports[`options trailingSpaceFormatter diff middle dot 1`] = `
380380
<g>- Expected</>
381381
<r>+ Received</>
382382

@@ -387,7 +387,7 @@ exports[`options trailingSpaceFormatter diffDefault middle dot 1`] = `
387387
<r>+ insert 1 trailing space:·</>
388388
`;
389389

390-
exports[`options trailingSpaceFormatter diffDefault yellowish common 1`] = `
390+
exports[`options trailingSpaceFormatter diff yellowish common 1`] = `
391391
<g>- Expected</>
392392
<r>+ Received</>
393393

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

+5-5
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import chalk = require('chalk');
99
import stripAnsi = require('strip-ansi');
1010
import {alignedAnsiStyleSerializer} from '@jest/test-utils';
11-
import diff from '../';
11+
import {diff} from '../';
1212
import {NO_DIFF_MESSAGE} from '../constants';
1313
import {diffLinesUnified, diffLinesUnified2} from '../diffLines';
1414
import {noColor} from '../normalizeDiffOptions';
@@ -1072,11 +1072,11 @@ describe('options', () => {
10721072
'insert 1 trailing space: ',
10731073
].join('\n');
10741074

1075-
test('diffDefault default no color', () => {
1075+
test('diff default no color', () => {
10761076
expect(diff(aTrailingSpaces, bTrailingSpaces)).toMatchSnapshot();
10771077
});
10781078

1079-
test('diffDefault middle dot', () => {
1079+
test('diff middle dot', () => {
10801080
const replaceSpacesWithMiddleDot = string => '·'.repeat(string.length);
10811081
const options = {
10821082
changeLineTrailingSpaceColor: replaceSpacesWithMiddleDot,
@@ -1086,7 +1086,7 @@ describe('options', () => {
10861086
expect(diff(aTrailingSpaces, bTrailingSpaces, options)).toMatchSnapshot();
10871087
});
10881088

1089-
test('diffDefault yellowish common', () => {
1089+
test('diff yellowish common', () => {
10901090
const options = {
10911091
commonLineTrailingSpaceColor: chalk.bgYellow,
10921092
};
@@ -1112,7 +1112,7 @@ describe('options', () => {
11121112
'',
11131113
].join('\n');
11141114

1115-
test('diffDefault', () => {
1115+
test('diff', () => {
11161116
expect(diff(aEmpty, bEmpty, options)).toBe(expected);
11171117
});
11181118

packages/jest-diff/src/index.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ const FALLBACK_FORMAT_OPTIONS_0 = {...FALLBACK_FORMAT_OPTIONS, indent: 0};
6060
// Generate a string that will highlight the difference between two values
6161
// with green and red. (similar to how github does code diffing)
6262
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
63-
function diff(a: any, b: any, options?: DiffOptions): string | null {
63+
export function diff(a: any, b: any, options?: DiffOptions): string | null {
6464
if (Object.is(a, b)) {
6565
return getCommonMessage(NO_DIFF_MESSAGE, options);
6666
}
@@ -190,5 +190,3 @@ function compareObjects(
190190

191191
return difference;
192192
}
193-
194-
export default diff;

packages/jest-matcher-utils/src/__tests__/index.test.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,9 @@ describe('ensureNoExpected()', () => {
213213
});
214214
});
215215

216-
jest.mock('jest-diff', () => () => 'diff output');
216+
jest.mock('jest-diff', () => ({
217+
diff: () => 'diff output',
218+
}));
217219
describe('diff', () => {
218220
test('forwards to jest-diff', () => {
219221
[

packages/jest-matcher-utils/src/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@
88
/* eslint-disable local/ban-types-eventually */
99

1010
import chalk = require('chalk');
11-
import diffDefault, {
11+
import {
1212
DIFF_DELETE,
1313
DIFF_EQUAL,
1414
DIFF_INSERT,
1515
Diff,
1616
DiffOptions as ImportDiffOptions,
17+
diff as diffDefault,
1718
diffStringsRaw,
1819
diffStringsUnified,
1920
} from 'jest-diff';

0 commit comments

Comments
 (0)