Skip to content

Commit 035b03c

Browse files
committed
support jest-diffing numbers and booleans
1 parent 5c30518 commit 035b03c

File tree

11 files changed

+76
-13
lines changed

11 files changed

+76
-13
lines changed

CHANGELOG.md

Lines changed: 1 addition & 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 (TODO link)
67
- `[jest-cli]` [**BREAKING**] Only set error process error codes when they are non-zero ([#7363](https://github.com/facebook/jest/pull/7363))
78
- `[jest-config]` [**BREAKING**] Deprecate `setupTestFrameworkScriptFile` in favor of new `setupFilesAfterEnv` ([#7119](https://github.com/facebook/jest/pull/7119))
89
- `[jest-worker]` [**BREAKING**] Add functionality to call a `setup` method in the worker before the first call and a `teardown` method when ending the farm ([#7014](https://github.com/facebook/jest/pull/7014))

packages/expect/src/matchers.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,10 @@ const matchers: MatchersObject = {
6666
(receivedType === 'object' || expectedType === 'array') &&
6767
equals(received, expected, [iterableEquality]);
6868
const oneline = isOneline(expected, received);
69-
const diffString = diff(expected, received, {expand: this.expand});
69+
const diffString = diff(expected, received, {
70+
expand: this.expand,
71+
omitTrivial: true,
72+
});
7073

7174
return (
7275
matcherHint('.toBe', undefined, undefined, {
@@ -391,7 +394,10 @@ const matchers: MatchersObject = {
391394
`Expected: ${printExpected(expected)}\n` +
392395
`Received: ${printReceived(received)}`
393396
: () => {
394-
const diffString = diff(expected, received, {expand: this.expand});
397+
const diffString = diff(expected, received, {
398+
expand: this.expand,
399+
omitTrivial: true,
400+
});
395401

396402
return (
397403
matcherHint('.toEqual', undefined, undefined, {
@@ -523,7 +529,10 @@ const matchers: MatchersObject = {
523529
: () => {
524530
const diffString =
525531
valuePassed && hasEndProp
526-
? diff(value, result.value, {expand: this.expand})
532+
? diff(value, result.value, {
533+
expand: this.expand,
534+
omitTrivial: true,
535+
})
527536
: '';
528537
return (
529538
matcherHint('.toHaveProperty', 'object', 'path', {
@@ -648,6 +657,7 @@ const matchers: MatchersObject = {
648657
getObjectSubset(receivedObject, expectedObject),
649658
{
650659
expand: this.expand,
660+
omitTrivial: true,
651661
},
652662
);
653663
return (
@@ -683,6 +693,7 @@ const matchers: MatchersObject = {
683693
: () => {
684694
const diffString = diff(expected, received, {
685695
expand: this.expand,
696+
omitTrivial: true,
686697
});
687698
return hint + (diffString ? `\n\nDifference:\n\n${diffString}` : '');
688699
};

packages/expect/src/spyMatchers.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -566,7 +566,7 @@ const formatMismatchedArgs = (expected, received) => {
566566
for (let i = 0; i < length; i++) {
567567
if (!equals(expected[i], received[i], [iterableEquality])) {
568568
const oneline = isOneline(expected[i], received[i]);
569-
const diffString = diff(expected[i], received[i]);
569+
const diffString = diff(expected[i], received[i], {omitTrivial: true});
570570
printedArgs.push(
571571
` ${printExpected(expected[i])}\n` +
572572
`as argument ${i + 1}, but it was called with\n` +

packages/jest-circus/src/formatNodeAssertErrors.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,12 @@ const formatNodeAssertErrors = (event: Event, state: State) => {
6666
error = errors;
6767
}
6868
return error.code === 'ERR_ASSERTION'
69-
? {message: assertionErrorMessage(error, {expand: state.expand})}
69+
? {
70+
message: assertionErrorMessage(error, {
71+
expand: state.expand,
72+
omitTrivial: true,
73+
}),
74+
}
7075
: errors;
7176
});
7277
}

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

Lines changed: 19 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,25 @@ 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'));
189+
});
190+
191+
test('numbers with omitTrivial', () => {
192+
expect(diff(1, 2, {omitTrivial: true})).toBeNull();
183193
});
184194

185195
test('booleans', () => {
186-
const result = diff(true, false);
187-
expect(result).toBe(null);
196+
expect(stripped(false, true)).toEqual(
197+
expect.stringContaining('- false\n+ true'),
198+
);
199+
});
200+
201+
test('booleans with omitTrivial', () => {
202+
expect(diff(false, true, {omitTrivial: true})).toBeNull();
188203
});
189204

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

packages/jest-diff/src/diffStrings.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ export type DiffOptions = {|
1818
bAnnotation?: string,
1919
expand?: boolean,
2020
contextLines?: number,
21+
// Return null instead of diffing numbers or booleans.
22+
// Jest uses this because the matchers already print a short failure reason,
23+
// so the diff output would be redundant for those types.
24+
omitTrivial?: boolean,
2125
|};
2226

2327
type Original = {|

packages/jest-diff/src/index.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,10 @@ function diff(a: any, b: any, options: ?DiffOptions): ?string {
8787
switch (aType) {
8888
case 'string':
8989
return diffStrings(a, b, options);
90-
case 'number':
9190
case 'boolean':
92-
return null;
91+
return compareBooleans(a, b, options);
92+
case 'number':
93+
return compareNumbers(a, b, options);
9394
case 'map':
9495
return compareObjects(sortMap(a), sortMap(b), options);
9596
case 'set':
@@ -99,6 +100,24 @@ function diff(a: any, b: any, options: ?DiffOptions): ?string {
99100
}
100101
}
101102

103+
function compareBooleans(a: boolean, b: boolean, options: ?DiffOptions) {
104+
if (options && options.omitTrivial) {
105+
return null;
106+
}
107+
108+
return diffStrings(String(a), String(b), options);
109+
}
110+
111+
function compareNumbers(a: number, b: number, options: ?DiffOptions) {
112+
if (options && options.omitTrivial) {
113+
return null;
114+
}
115+
116+
const aStr = Object.is(a, -0) ? '-0' : String(a);
117+
const bStr = Object.is(b, -0) ? '-0' : String(b);
118+
return diffStrings(aStr, bStr, options);
119+
}
120+
102121
function sortMap(map) {
103122
return new Map(Array.from(map.entries()).sort());
104123
}

packages/jest-jasmine2/src/jasmine/Env.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,10 @@ export default function(j$) {
578578

579579
if (error instanceof AssertionError) {
580580
checkIsError = false;
581-
message = assertionErrorMessage(error, {expand: j$.Spec.expand});
581+
message = assertionErrorMessage(error, {
582+
expand: j$.Spec.expand,
583+
omitTrivial: true,
584+
});
582585
} else {
583586
const check = isError(error);
584587

packages/jest-jasmine2/src/jasmine/Spec.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,10 @@ Spec.prototype.onException = function onException(error) {
153153
}
154154

155155
if (error instanceof AssertionError) {
156-
error = assertionErrorMessage(error, {expand: this.expand});
156+
error = assertionErrorMessage(error, {
157+
expand: this.expand,
158+
omitTrivial: true,
159+
});
157160
}
158161

159162
this.addExpectationResult(

packages/jest-snapshot/src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ const _toMatchSnapshot = ({
186186
aAnnotation: 'Snapshot',
187187
bAnnotation: 'Received',
188188
expand: snapshotState.expand,
189+
omitTrivial: true,
189190
});
190191

191192
report = () =>

0 commit comments

Comments
 (0)