Skip to content

Commit

Permalink
support jest-diffing numbers and booleans
Browse files Browse the repository at this point in the history
  • Loading branch information
jeysal committed Jan 20, 2019
1 parent 643beb1 commit 367343a
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
### Features

- `[jest-runtime]` Add `jest.isolateModules` for scoped module initialization ([#6701](https://github.com/facebook/jest/pull/6701))
- `[jest-diff]` [**BREAKING**] Support diffing numbers and booleans instead of returning null for different ones ([#7605](https://github.com/facebook/jest/pull/7605))
- `[jest-diff]` [**BREAKING**] Replace `diff` with `diff-sequences` package ([#6961](https://github.com/facebook/jest/pull/6961))
- `[jest-cli]` [**BREAKING**] Only set error process error codes when they are non-zero ([#7363](https://github.com/facebook/jest/pull/7363))
- `[jest-config]` [**BREAKING**] Deprecate `setupTestFrameworkScriptFile` in favor of new `setupFilesAfterEnv` ([#7119](https://github.com/facebook/jest/pull/7119))
Expand Down
6 changes: 5 additions & 1 deletion packages/expect/src/matchers.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

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

import diff from 'jest-diff';
import jestDiff from 'jest-diff';
import getType from 'jest-get-type';
import {escapeStrForRegex} from 'jest-regex-util';
import {
Expand All @@ -25,6 +25,7 @@ import {
printReceived,
printExpected,
printWithType,
shouldPrintDiff,
} from 'jest-matcher-utils';
import {
getObjectSubset,
Expand All @@ -44,6 +45,9 @@ type ContainIterable =
| DOMTokenList
| HTMLCollection<any>;

const diff: typeof jestDiff = (a, b, options) =>
shouldPrintDiff(a, b) ? jestDiff(a, b, options) : null;

const matchers: MatchersObject = {
toBe(received: any, expected: any) {
const comment = 'Object.is equality';
Expand Down
15 changes: 11 additions & 4 deletions packages/jest-diff/src/__tests__/diff.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,12 @@ describe('no visual difference', () => {
[[], []],
[[1, 2], [1, 2]],
[11, 11],
[NaN, NaN],
[Number.NaN, NaN],
[() => {}, () => {}],
[null, null],
[undefined, undefined],
[false, false],
[{a: 1}, {a: 1}],
[{a: {b: 5}}, {a: {b: 5}}],
].forEach(values => {
Expand Down Expand Up @@ -178,13 +181,17 @@ describe('objects', () => {
});

test('numbers', () => {
const result = diff(123, 234);
expect(result).toBe(null);
expect(stripped(1, 2)).toEqual(expect.stringContaining('- 1\n+ 2'));
});

test('-0 and 0', () => {
expect(stripped(-0, 0)).toEqual(expect.stringContaining('- -0\n+ 0'));
});

test('booleans', () => {
const result = diff(true, false);
expect(result).toBe(null);
expect(stripped(false, true)).toEqual(
expect.stringContaining('- false\n+ true'),
);
});

describe('multiline string non-snapshot', () => {
Expand Down
16 changes: 14 additions & 2 deletions packages/jest-diff/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ function diff(a: any, b: any, options: ?DiffOptions): ?string {
switch (aType) {
case 'string':
return diffStrings(a, b, options);
case 'number':
case 'boolean':
return null;
case 'number':
return comparePrimitive(a, b, options);
case 'map':
return compareObjects(sortMap(a), sortMap(b), options);
case 'set':
Expand All @@ -95,6 +95,18 @@ function diff(a: any, b: any, options: ?DiffOptions): ?string {
}
}

function comparePrimitive(
a: number | boolean,
b: number | boolean,
options: ?DiffOptions,
) {
return diffStrings(
prettyFormat(a, FORMAT_OPTIONS),
prettyFormat(b, FORMAT_OPTIONS),
options,
);
}

function sortMap(map) {
return new Map(Array.from(map.entries()).sort());
}
Expand Down
25 changes: 25 additions & 0 deletions packages/jest-matcher-utils/src/__tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
ensureNoExpected,
getLabelPrinter,
pluralize,
shouldPrintDiff,
stringify,
} from '../';

Expand Down Expand Up @@ -129,6 +130,30 @@ describe('.ensureNoExpected()', () => {
});
});

describe('shouldPrintDiff', () => {
test('true', () => {
[
['a', 'b'],
['a', {}],
['a', null],
['a', undefined],
['a', 1],
['a', true],
[1, true],
].forEach(([actual, expected]) =>
expect(shouldPrintDiff(actual, expected)).toBe(true),
);
});

test('two booleans', () => {
expect(shouldPrintDiff(false, true)).toBe(false);
});

test('two numbers', () => {
expect(shouldPrintDiff(1, 2)).toBe(false);
});
});

describe('.pluralize()', () => {
test('one', () => expect(pluralize('apple', 1)).toEqual('one apple'));
test('two', () => expect(pluralize('apple', 2)).toEqual('two apples'));
Expand Down
13 changes: 13 additions & 0 deletions packages/jest-matcher-utils/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,19 @@ export const ensureNumbers = (
ensureExpectedIsNumber(expected, matcherName);
};

// Sometimes, e.g. when comparing two numbers, the output from jest-diff
// does not contain more information than the `Expected:` / `Received:` already gives.
// In those cases, we do not print a diff to make the output shorter and not redundant.
export const shouldPrintDiff = (actual: any, expected: any) => {
if (typeof actual === 'number' && typeof expected === 'number') {
return false;
}
if (typeof actual === 'boolean' && typeof expected === 'boolean') {
return false;
}
return true;
};

export const pluralize = (word: string, count: number) =>
(NUMBERS[count] || count) + ' ' + word + (count === 1 ? '' : 's');

Expand Down

0 comments on commit 367343a

Please sign in to comment.