Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jest diff numbers and booleans #7605

Merged
merged 5 commits into from
Jan 20, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 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 Expand Up @@ -54,6 +55,7 @@

### Fixes

- `[jest-diff]` Do not claim that `-0` and `0` have no visual difference ([#7605](https://github.com/facebook/jest/pull/7605))
- `[jest-mock]` Fix automock for numeric function names ([#7653](https://github.com/facebook/jest/pull/7653))
- `[jest-config]` Ensure `existsSync` is only called with a string parameter ([#7607](https://github.com/facebook/jest/pull/7607))
- `[expect]` `toStrictEqual` considers sparseness of arrays. ([#7591](https://github.com/facebook/jest/pull/7591))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -360,11 +360,7 @@ exports[`.toBe() fails for: -0 and 0 1`] = `
"<dim>expect(</><red>received</><dim>).toBe(</><green>expected</><dim>) // Object.is equality</>

Expected: <green>0</>
Received: <red>-0</>

Difference:

<dim>Compared values have no visual difference.</>"
Received: <red>-0</>"
`;

exports[`.toBe() fails for: 1 and 2 1`] = `
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) =>
SimenB marked this conversation as resolved.
Show resolved Hide resolved
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
18 changes: 15 additions & 3 deletions packages/jest-diff/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const FALLBACK_FORMAT_OPTIONS_0 = {...FALLBACK_FORMAT_OPTIONS, indent: 0};
// Generate a string that will highlight the difference between two values
// with green and red. (similar to how github does code diffing)
function diff(a: any, b: any, options: ?DiffOptions): ?string {
if (a === b) {
if (Object.is(a, b)) {
return NO_DIFF_MESSAGE;
}

Expand Down 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