Skip to content

Commit b450d78

Browse files
jeysalSimenB
authored andcommitted
jest-diff numbers and booleans (#7605)
1 parent 1982825 commit b450d78

File tree

15 files changed

+78
-29
lines changed

15 files changed

+78
-29
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
### Features
44

55
- `[jest-runtime]` Add `jest.isolateModules` for scoped module initialization ([#6701](https://github.com/facebook/jest/pull/6701))
6+
- `[jest-diff]` [**BREAKING**] Support diffing numbers and booleans instead of returning null for different ones ([#7605](https://github.com/facebook/jest/pull/7605))
67
- `[jest-diff]` [**BREAKING**] Replace `diff` with `diff-sequences` package ([#6961](https://github.com/facebook/jest/pull/6961))
78
- `[jest-cli]` [**BREAKING**] Only set error process error codes when they are non-zero ([#7363](https://github.com/facebook/jest/pull/7363))
89
- `[jest-config]` [**BREAKING**] Deprecate `setupTestFrameworkScriptFile` in favor of new `setupFilesAfterEnv` ([#7119](https://github.com/facebook/jest/pull/7119))
@@ -54,6 +55,7 @@
5455

5556
### Fixes
5657

58+
- `[jest-diff]` Do not claim that `-0` and `0` have no visual difference ([#7605](https://github.com/facebook/jest/pull/7605))
5759
- `[jest-mock]` Fix automock for numeric function names ([#7653](https://github.com/facebook/jest/pull/7653))
5860
- `[jest-config]` Ensure `existsSync` is only called with a string parameter ([#7607](https://github.com/facebook/jest/pull/7607))
5961
- `[expect]` `toStrictEqual` considers sparseness of arrays. ([#7591](https://github.com/facebook/jest/pull/7591))

e2e/__tests__/__snapshots__/failures.test.js.snap

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -540,10 +540,6 @@ FAIL __tests__/assertionError.test.js
540540
Received:
541541
1
542542
543-
Difference:
544-
545-
Compared values have no visual difference.
546-
547543
33 |
548544
34 | test('assert.notEqual', () => {
549545
> 35 | assert.notEqual(1, 1);
@@ -677,10 +673,6 @@ FAIL __tests__/assertionError.test.js
677673
Message:
678674
My custom error message
679675
680-
Difference:
681-
682-
Compared values have no visual difference.
683-
684676
53 |
685677
54 | test('assert.notStrictEqual', () => {
686678
> 55 | assert.notStrictEqual(1, 1, 'My custom error message');

packages/expect/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
"browser": "build-es5/index.js",
1212
"dependencies": {
1313
"ansi-styles": "^3.2.0",
14-
"jest-diff": "^23.6.0",
1514
"jest-get-type": "^22.1.0",
1615
"jest-matcher-utils": "^23.6.0",
1716
"jest-message-util": "^23.4.0",

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -360,11 +360,7 @@ exports[`.toBe() fails for: -0 and 0 1`] = `
360360
"<dim>expect(</><red>received</><dim>).toBe(</><green>expected</><dim>) // Object.is equality</>
361361

362362
Expected: <green>0</>
363-
Received: <red>-0</>
364-
365-
Difference:
366-
367-
<dim>Compared values have no visual difference.</>"
363+
Received: <red>-0</>"
368364
`;
369365

370366
exports[`.toBe() fails for: 1 and 2 1`] = `

packages/expect/src/matchers.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@
99

1010
import type {MatchersObject} from 'types/Matchers';
1111

12-
import diff from 'jest-diff';
1312
import getType from 'jest-get-type';
1413
import {escapeStrForRegex} from 'jest-regex-util';
1514
import {
1615
EXPECTED_COLOR,
1716
RECEIVED_COLOR,
1817
SUGGEST_TO_EQUAL,
1918
SUGGEST_TO_CONTAIN_EQUAL,
19+
diff,
2020
ensureNoExpected,
2121
ensureNumbers,
2222
getLabelPrinter,

packages/expect/src/spyMatchers.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const CALL_PRINT_LIMIT = 3;
1313
const RETURN_PRINT_LIMIT = 5;
1414
const LAST_CALL_PRINT_LIMIT = 1;
1515
import {
16+
diff,
1617
ensureExpectedIsNumber,
1718
ensureNoExpected,
1819
EXPECTED_COLOR,
@@ -26,7 +27,6 @@ import {
2627
} from 'jest-matcher-utils';
2728
import {equals} from './jasmineUtils';
2829
import {iterableEquality, partition, isOneline} from './utils';
29-
import diff from 'jest-diff';
3030

3131
const createToBeCalledMatcher = matcherName => (received, expected) => {
3232
ensureNoExpected(expected, matcherName);

packages/jest-circus/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
"co": "^4.6.0",
1515
"expect": "^23.6.0",
1616
"is-generator-fn": "^2.0.0",
17-
"jest-diff": "^23.6.0",
1817
"jest-each": "^23.6.0",
1918
"jest-matcher-utils": "^23.6.0",
2019
"jest-message-util": "^23.4.0",
@@ -25,6 +24,7 @@
2524
},
2625
"devDependencies": {
2726
"execa": "^1.0.0",
27+
"jest-diff": "^23.6.0",
2828
"jest-runtime": "^23.6.0"
2929
},
3030
"engines": {

packages/jest-circus/src/formatNodeAssertErrors.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@
1010
import type {DiffOptions} from 'jest-diff/src/diffStrings';
1111
import type {Event, State} from 'types/Circus';
1212

13-
import {printExpected, printReceived} from 'jest-matcher-utils';
13+
import {diff, printExpected, printReceived} from 'jest-matcher-utils';
1414
import chalk from 'chalk';
15-
import diff from 'jest-diff';
1615
import prettyFormat from 'pretty-format';
1716

1817
type AssertionError = {|

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,12 @@ describe('no visual difference', () => {
4848
[[], []],
4949
[[1, 2], [1, 2]],
5050
[11, 11],
51+
[NaN, NaN],
52+
[Number.NaN, NaN],
5153
[() => {}, () => {}],
5254
[null, null],
5355
[undefined, undefined],
56+
[false, false],
5457
[{a: 1}, {a: 1}],
5558
[{a: {b: 5}}, {a: {b: 5}}],
5659
].forEach(values => {
@@ -178,13 +181,17 @@ describe('objects', () => {
178181
});
179182

180183
test('numbers', () => {
181-
const result = diff(123, 234);
182-
expect(result).toBe(null);
184+
expect(stripped(1, 2)).toEqual(expect.stringContaining('- 1\n+ 2'));
185+
});
186+
187+
test('-0 and 0', () => {
188+
expect(stripped(-0, 0)).toEqual(expect.stringContaining('- -0\n+ 0'));
183189
});
184190

185191
test('booleans', () => {
186-
const result = diff(true, false);
187-
expect(result).toBe(null);
192+
expect(stripped(false, true)).toEqual(
193+
expect.stringContaining('- false\n+ true'),
194+
);
188195
});
189196

190197
describe('multiline string non-snapshot', () => {

packages/jest-diff/src/index.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ const FALLBACK_FORMAT_OPTIONS_0 = {...FALLBACK_FORMAT_OPTIONS, indent: 0};
4646
// Generate a string that will highlight the difference between two values
4747
// with green and red. (similar to how github does code diffing)
4848
function diff(a: any, b: any, options: ?DiffOptions): ?string {
49-
if (a === b) {
49+
if (Object.is(a, b)) {
5050
return NO_DIFF_MESSAGE;
5151
}
5252

@@ -83,9 +83,9 @@ function diff(a: any, b: any, options: ?DiffOptions): ?string {
8383
switch (aType) {
8484
case 'string':
8585
return diffStrings(a, b, options);
86-
case 'number':
8786
case 'boolean':
88-
return null;
87+
case 'number':
88+
return comparePrimitive(a, b, options);
8989
case 'map':
9090
return compareObjects(sortMap(a), sortMap(b), options);
9191
case 'set':
@@ -95,6 +95,18 @@ function diff(a: any, b: any, options: ?DiffOptions): ?string {
9595
}
9696
}
9797

98+
function comparePrimitive(
99+
a: number | boolean,
100+
b: number | boolean,
101+
options: ?DiffOptions,
102+
) {
103+
return diffStrings(
104+
prettyFormat(a, FORMAT_OPTIONS),
105+
prettyFormat(b, FORMAT_OPTIONS),
106+
options,
107+
);
108+
}
109+
98110
function sortMap(map) {
99111
return new Map(Array.from(map.entries()).sort());
100112
}

0 commit comments

Comments
 (0)