Skip to content

Commit 23dc84f

Browse files
committed
support jest-diffing numbers and booleans
1 parent e0313be commit 23dc84f

File tree

11 files changed

+74
-13
lines changed

11 files changed

+74
-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 ([#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))

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, {
@@ -420,7 +423,10 @@ const matchers: MatchersObject = {
420423
`Expected: ${printExpected(expected)}\n` +
421424
`Received: ${printReceived(received)}`
422425
: () => {
423-
const diffString = diff(expected, received, {expand: this.expand});
426+
const diffString = diff(expected, received, {
427+
expand: this.expand,
428+
omitTrivial: true,
429+
});
424430

425431
return (
426432
matcherHint('.toEqual', undefined, undefined, {
@@ -552,7 +558,10 @@ const matchers: MatchersObject = {
552558
: () => {
553559
const diffString =
554560
valuePassed && hasEndProp
555-
? diff(value, result.value, {expand: this.expand})
561+
? diff(value, result.value, {
562+
expand: this.expand,
563+
omitTrivial: true,
564+
})
556565
: '';
557566
return (
558567
matcherHint('.toHaveProperty', 'object', 'path', {
@@ -677,6 +686,7 @@ const matchers: MatchersObject = {
677686
getObjectSubset(receivedObject, expectedObject),
678687
{
679688
expand: this.expand,
689+
omitTrivial: true,
680690
},
681691
);
682692
return (
@@ -712,6 +722,7 @@ const matchers: MatchersObject = {
712722
: () => {
713723
const diffString = diff(expected, received, {
714724
expand: this.expand,
725+
omitTrivial: true,
715726
});
716727
return hint + (diffString ? `\n\nDifference:\n\n${diffString}` : '');
717728
};

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
@@ -19,6 +19,10 @@ export type DiffOptions = {|
1919
bAnnotation?: string,
2020
expand?: boolean,
2121
contextLines?: number,
22+
// Return null instead of diffing numbers or booleans.
23+
// Jest uses this because the matchers already print a short failure reason,
24+
// so the diff output would be redundant for those types.
25+
omitTrivial?: boolean,
2226
|};
2327

2428
type Original = {|

packages/jest-diff/src/index.js

Lines changed: 19 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 compareTrivial(a, b, options);
92+
case 'number':
93+
return compareTrivial(a, b, options);
9394
case 'map':
9495
return compareObjects(sortMap(a), sortMap(b), options);
9596
case 'set':
@@ -99,6 +100,22 @@ function diff(a: any, b: any, options: ?DiffOptions): ?string {
99100
}
100101
}
101102

103+
function compareTrivial(
104+
a: number | boolean,
105+
b: number | boolean,
106+
options: ?DiffOptions,
107+
) {
108+
if (options && options.omitTrivial) {
109+
return null;
110+
}
111+
112+
return diffStrings(
113+
prettyFormat(a, FORMAT_OPTIONS),
114+
prettyFormat(b, FORMAT_OPTIONS),
115+
options,
116+
);
117+
}
118+
102119
function sortMap(map) {
103120
return new Map(Array.from(map.entries()).sort());
104121
}

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)