Skip to content

Commit 367343a

Browse files
committed
support jest-diffing numbers and booleans
1 parent 643beb1 commit 367343a

File tree

6 files changed

+69
-7
lines changed

6 files changed

+69
-7
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: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

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

12-
import diff from 'jest-diff';
12+
import jestDiff from 'jest-diff';
1313
import getType from 'jest-get-type';
1414
import {escapeStrForRegex} from 'jest-regex-util';
1515
import {
@@ -25,6 +25,7 @@ import {
2525
printReceived,
2626
printExpected,
2727
printWithType,
28+
shouldPrintDiff,
2829
} from 'jest-matcher-utils';
2930
import {
3031
getObjectSubset,
@@ -44,6 +45,9 @@ type ContainIterable =
4445
| DOMTokenList
4546
| HTMLCollection<any>;
4647

48+
const diff: typeof jestDiff = (a, b, options) =>
49+
shouldPrintDiff(a, b) ? jestDiff(a, b, options) : null;
50+
4751
const matchers: MatchersObject = {
4852
toBe(received: any, expected: any) {
4953
const comment = 'Object.is equality';

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: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
ensureNoExpected,
1212
getLabelPrinter,
1313
pluralize,
14+
shouldPrintDiff,
1415
stringify,
1516
} from '../';
1617

@@ -129,6 +130,30 @@ describe('.ensureNoExpected()', () => {
129130
});
130131
});
131132

133+
describe('shouldPrintDiff', () => {
134+
test('true', () => {
135+
[
136+
['a', 'b'],
137+
['a', {}],
138+
['a', null],
139+
['a', undefined],
140+
['a', 1],
141+
['a', true],
142+
[1, true],
143+
].forEach(([actual, expected]) =>
144+
expect(shouldPrintDiff(actual, expected)).toBe(true),
145+
);
146+
});
147+
148+
test('two booleans', () => {
149+
expect(shouldPrintDiff(false, true)).toBe(false);
150+
});
151+
152+
test('two numbers', () => {
153+
expect(shouldPrintDiff(1, 2)).toBe(false);
154+
});
155+
});
156+
132157
describe('.pluralize()', () => {
133158
test('one', () => expect(pluralize('apple', 1)).toEqual('one apple'));
134159
test('two', () => expect(pluralize('apple', 2)).toEqual('two apples'));

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,19 @@ export const ensureNumbers = (
160160
ensureExpectedIsNumber(expected, matcherName);
161161
};
162162

163+
// Sometimes, e.g. when comparing two numbers, the output from jest-diff
164+
// does not contain more information than the `Expected:` / `Received:` already gives.
165+
// In those cases, we do not print a diff to make the output shorter and not redundant.
166+
export const shouldPrintDiff = (actual: any, expected: any) => {
167+
if (typeof actual === 'number' && typeof expected === 'number') {
168+
return false;
169+
}
170+
if (typeof actual === 'boolean' && typeof expected === 'boolean') {
171+
return false;
172+
}
173+
return true;
174+
};
175+
163176
export const pluralize = (word: string, count: number) =>
164177
(NUMBERS[count] || count) + ' ' + word + (count === 1 ? '' : 's');
165178

0 commit comments

Comments
 (0)