Skip to content

Commit

Permalink
jest-matcher-utils diff
Browse files Browse the repository at this point in the history
  • Loading branch information
jeysal committed Jan 20, 2019
1 parent 367343a commit bb9a782
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 24 deletions.
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
6 changes: 1 addition & 5 deletions 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 jestDiff 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 All @@ -25,7 +25,6 @@ import {
printReceived,
printExpected,
printWithType,
shouldPrintDiff,
} from 'jest-matcher-utils';
import {
getObjectSubset,
Expand All @@ -45,9 +44,6 @@ 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
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
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
13 changes: 7 additions & 6 deletions packages/jest-matcher-utils/src/__tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
*/

import {
diff,
ensureNumbers,
ensureNoExpected,
getLabelPrinter,
pluralize,
shouldPrintDiff,
stringify,
} from '../';

Expand Down Expand Up @@ -130,8 +130,9 @@ describe('.ensureNoExpected()', () => {
});
});

describe('shouldPrintDiff', () => {
test('true', () => {
jest.mock('jest-diff', () => () => 'diff output');
describe('diff', () => {
test('forwards to jest-diff', () => {
[
['a', 'b'],
['a', {}],
Expand All @@ -141,16 +142,16 @@ describe('shouldPrintDiff', () => {
['a', true],
[1, true],
].forEach(([actual, expected]) =>
expect(shouldPrintDiff(actual, expected)).toBe(true),
expect(diff(actual, expected)).toBe('diff output'),
);
});

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

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

Expand Down
5 changes: 4 additions & 1 deletion 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 @@ -163,7 +164,7 @@ export const ensureNumbers = (
// 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) => {
const shouldPrintDiff = (actual: any, expected: any) => {
if (typeof actual === 'number' && typeof expected === 'number') {
return false;
}
Expand All @@ -172,6 +173,8 @@ export const shouldPrintDiff = (actual: any, expected: any) => {
}
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 bb9a782

Please sign in to comment.