Skip to content

Commit

Permalink
jest-diff numbers and booleans (jestjs#7605)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeysal authored and captain-yossarian committed Jul 18, 2019
1 parent 94430f3 commit d85d0cb
Show file tree
Hide file tree
Showing 15 changed files with 78 additions and 29 deletions.
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
8 changes: 0 additions & 8 deletions e2e/__tests__/__snapshots__/failures.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -540,10 +540,6 @@ FAIL __tests__/assertionError.test.js
Received:
1
Difference:
Compared values have no visual difference.
33 |
34 | test('assert.notEqual', () => {
> 35 | assert.notEqual(1, 1);
Expand Down Expand Up @@ -677,10 +673,6 @@ FAIL __tests__/assertionError.test.js
Message:
My custom error message
Difference:
Compared values have no visual difference.
53 |
54 | test('assert.notStrictEqual', () => {
> 55 | assert.notStrictEqual(1, 1, 'My custom error message');
Expand Down
1 change: 0 additions & 1 deletion packages/expect/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
"browser": "build-es5/index.js",
"dependencies": {
"ansi-styles": "^3.2.0",
"jest-diff": "^23.6.0",
"jest-get-type": "^22.1.0",
"jest-matcher-utils": "^23.6.0",
"jest-message-util": "^23.4.0",
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
2 changes: 1 addition & 1 deletion packages/expect/src/matchers.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@

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

import diff from 'jest-diff';
import getType from 'jest-get-type';
import {escapeStrForRegex} from 'jest-regex-util';
import {
EXPECTED_COLOR,
RECEIVED_COLOR,
SUGGEST_TO_EQUAL,
SUGGEST_TO_CONTAIN_EQUAL,
diff,
ensureNoExpected,
ensureNumbers,
getLabelPrinter,
Expand Down
2 changes: 1 addition & 1 deletion packages/expect/src/spyMatchers.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const CALL_PRINT_LIMIT = 3;
const RETURN_PRINT_LIMIT = 5;
const LAST_CALL_PRINT_LIMIT = 1;
import {
diff,
ensureExpectedIsNumber,
ensureNoExpected,
EXPECTED_COLOR,
Expand All @@ -26,7 +27,6 @@ import {
} from 'jest-matcher-utils';
import {equals} from './jasmineUtils';
import {iterableEquality, partition, isOneline} from './utils';
import diff from 'jest-diff';

const createToBeCalledMatcher = matcherName => (received, expected) => {
ensureNoExpected(expected, matcherName);
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-circus/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
"co": "^4.6.0",
"expect": "^23.6.0",
"is-generator-fn": "^2.0.0",
"jest-diff": "^23.6.0",
"jest-each": "^23.6.0",
"jest-matcher-utils": "^23.6.0",
"jest-message-util": "^23.4.0",
Expand All @@ -25,6 +24,7 @@
},
"devDependencies": {
"execa": "^1.0.0",
"jest-diff": "^23.6.0",
"jest-runtime": "^23.6.0"
},
"engines": {
Expand Down
3 changes: 1 addition & 2 deletions packages/jest-circus/src/formatNodeAssertErrors.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@
import type {DiffOptions} from 'jest-diff/src/diffStrings';
import type {Event, State} from 'types/Circus';

import {printExpected, printReceived} from 'jest-matcher-utils';
import {diff, printExpected, printReceived} from 'jest-matcher-utils';
import chalk from 'chalk';
import diff from 'jest-diff';
import prettyFormat from 'pretty-format';

type AssertionError = {|
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
2 changes: 1 addition & 1 deletion packages/jest-jasmine2/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
"co": "^4.6.0",
"expect": "^23.6.0",
"is-generator-fn": "^2.0.0",
"jest-diff": "^23.6.0",
"jest-each": "^23.6.0",
"jest-matcher-utils": "^23.6.0",
"jest-message-util": "^23.4.0",
Expand All @@ -23,6 +22,7 @@
"pretty-format": "^23.6.0"
},
"devDependencies": {
"jest-diff": "^23.6.0",
"jest-runtime": "^23.6.0"
},
"engines": {
Expand Down
3 changes: 1 addition & 2 deletions packages/jest-jasmine2/src/assertionErrorMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@

import type {DiffOptions} from 'jest-diff/src/diffStrings';

import {printReceived, printExpected} from 'jest-matcher-utils';
import {diff, printReceived, printExpected} from 'jest-matcher-utils';
import chalk from 'chalk';
import diff from 'jest-diff';

type AssertionError = {|
actual: ?string,
Expand Down
1 change: 1 addition & 0 deletions packages/jest-matcher-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"main": "build/index.js",
"dependencies": {
"chalk": "^2.0.1",
"jest-diff": "^23.6.0",
"jest-get-type": "^22.1.0",
"pretty-format": "^23.6.0"
}
Expand Down
26 changes: 26 additions & 0 deletions packages/jest-matcher-utils/src/__tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/

import {
diff,
ensureNumbers,
ensureNoExpected,
getLabelPrinter,
Expand Down Expand Up @@ -129,6 +130,31 @@ describe('.ensureNoExpected()', () => {
});
});

jest.mock('jest-diff', () => () => 'diff output');
describe('diff', () => {
test('forwards to jest-diff', () => {
[
['a', 'b'],
['a', {}],
['a', null],
['a', undefined],
['a', 1],
['a', true],
[1, true],
].forEach(([actual, expected]) =>
expect(diff(actual, expected)).toBe('diff output'),
);
});

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

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

describe('.pluralize()', () => {
test('one', () => expect(pluralize('apple', 1)).toEqual('one apple'));
test('two', () => expect(pluralize('apple', 2)).toEqual('two apples'));
Expand Down
16 changes: 16 additions & 0 deletions packages/jest-matcher-utils/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import type {MatcherHintOptions} from 'types/Matchers';

import chalk from 'chalk';
import jestDiff from 'jest-diff';
import getType from 'jest-get-type';
import prettyFormat from 'pretty-format';
const {
Expand Down Expand Up @@ -160,6 +161,21 @@ 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.
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 diff: typeof jestDiff = (a, b, options) =>
shouldPrintDiff(a, b) ? jestDiff(a, b, options) : null;

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

Expand Down

0 comments on commit d85d0cb

Please sign in to comment.