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 10, 2019
1 parent 5c30518 commit 035b03c
Show file tree
Hide file tree
Showing 11 changed files with 76 additions and 13 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 (TODO link)
- `[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))
- `[jest-worker]` [**BREAKING**] Add functionality to call a `setup` method in the worker before the first call and a `teardown` method when ending the farm ([#7014](https://github.com/facebook/jest/pull/7014))
Expand Down
17 changes: 14 additions & 3 deletions packages/expect/src/matchers.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,10 @@ const matchers: MatchersObject = {
(receivedType === 'object' || expectedType === 'array') &&
equals(received, expected, [iterableEquality]);
const oneline = isOneline(expected, received);
const diffString = diff(expected, received, {expand: this.expand});
const diffString = diff(expected, received, {
expand: this.expand,
omitTrivial: true,
});

return (
matcherHint('.toBe', undefined, undefined, {
Expand Down Expand Up @@ -391,7 +394,10 @@ const matchers: MatchersObject = {
`Expected: ${printExpected(expected)}\n` +
`Received: ${printReceived(received)}`
: () => {
const diffString = diff(expected, received, {expand: this.expand});
const diffString = diff(expected, received, {
expand: this.expand,
omitTrivial: true,
});

return (
matcherHint('.toEqual', undefined, undefined, {
Expand Down Expand Up @@ -523,7 +529,10 @@ const matchers: MatchersObject = {
: () => {
const diffString =
valuePassed && hasEndProp
? diff(value, result.value, {expand: this.expand})
? diff(value, result.value, {
expand: this.expand,
omitTrivial: true,
})
: '';
return (
matcherHint('.toHaveProperty', 'object', 'path', {
Expand Down Expand Up @@ -648,6 +657,7 @@ const matchers: MatchersObject = {
getObjectSubset(receivedObject, expectedObject),
{
expand: this.expand,
omitTrivial: true,
},
);
return (
Expand Down Expand Up @@ -683,6 +693,7 @@ const matchers: MatchersObject = {
: () => {
const diffString = diff(expected, received, {
expand: this.expand,
omitTrivial: true,
});
return hint + (diffString ? `\n\nDifference:\n\n${diffString}` : '');
};
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 @@ -566,7 +566,7 @@ const formatMismatchedArgs = (expected, received) => {
for (let i = 0; i < length; i++) {
if (!equals(expected[i], received[i], [iterableEquality])) {
const oneline = isOneline(expected[i], received[i]);
const diffString = diff(expected[i], received[i]);
const diffString = diff(expected[i], received[i], {omitTrivial: true});
printedArgs.push(
` ${printExpected(expected[i])}\n` +
`as argument ${i + 1}, but it was called with\n` +
Expand Down
7 changes: 6 additions & 1 deletion packages/jest-circus/src/formatNodeAssertErrors.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,12 @@ const formatNodeAssertErrors = (event: Event, state: State) => {
error = errors;
}
return error.code === 'ERR_ASSERTION'
? {message: assertionErrorMessage(error, {expand: state.expand})}
? {
message: assertionErrorMessage(error, {
expand: state.expand,
omitTrivial: true,
}),
}
: errors;
});
}
Expand Down
23 changes: 19 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,25 @@ 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('numbers with omitTrivial', () => {
expect(diff(1, 2, {omitTrivial: true})).toBeNull();
});

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

test('booleans with omitTrivial', () => {
expect(diff(false, true, {omitTrivial: true})).toBeNull();
});

describe('multiline string non-snapshot', () => {
Expand Down
4 changes: 4 additions & 0 deletions packages/jest-diff/src/diffStrings.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ export type DiffOptions = {|
bAnnotation?: string,
expand?: boolean,
contextLines?: number,
// Return null instead of diffing numbers or booleans.
// Jest uses this because the matchers already print a short failure reason,
// so the diff output would be redundant for those types.
omitTrivial?: boolean,
|};

type Original = {|
Expand Down
23 changes: 21 additions & 2 deletions packages/jest-diff/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,10 @@ function diff(a: any, b: any, options: ?DiffOptions): ?string {
switch (aType) {
case 'string':
return diffStrings(a, b, options);
case 'number':
case 'boolean':
return null;
return compareBooleans(a, b, options);
case 'number':
return compareNumbers(a, b, options);
case 'map':
return compareObjects(sortMap(a), sortMap(b), options);
case 'set':
Expand All @@ -99,6 +100,24 @@ function diff(a: any, b: any, options: ?DiffOptions): ?string {
}
}

function compareBooleans(a: boolean, b: boolean, options: ?DiffOptions) {
if (options && options.omitTrivial) {
return null;
}

return diffStrings(String(a), String(b), options);
}

function compareNumbers(a: number, b: number, options: ?DiffOptions) {
if (options && options.omitTrivial) {
return null;
}

const aStr = Object.is(a, -0) ? '-0' : String(a);
const bStr = Object.is(b, -0) ? '-0' : String(b);
return diffStrings(aStr, bStr, options);
}

function sortMap(map) {
return new Map(Array.from(map.entries()).sort());
}
Expand Down
5 changes: 4 additions & 1 deletion packages/jest-jasmine2/src/jasmine/Env.js
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,10 @@ export default function(j$) {

if (error instanceof AssertionError) {
checkIsError = false;
message = assertionErrorMessage(error, {expand: j$.Spec.expand});
message = assertionErrorMessage(error, {
expand: j$.Spec.expand,
omitTrivial: true,
});
} else {
const check = isError(error);

Expand Down
5 changes: 4 additions & 1 deletion packages/jest-jasmine2/src/jasmine/Spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,10 @@ Spec.prototype.onException = function onException(error) {
}

if (error instanceof AssertionError) {
error = assertionErrorMessage(error, {expand: this.expand});
error = assertionErrorMessage(error, {
expand: this.expand,
omitTrivial: true,
});
}

this.addExpectationResult(
Expand Down
1 change: 1 addition & 0 deletions packages/jest-snapshot/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ const _toMatchSnapshot = ({
aAnnotation: 'Snapshot',
bAnnotation: 'Received',
expand: snapshotState.expand,
omitTrivial: true,
});

report = () =>
Expand Down
1 change: 1 addition & 0 deletions packages/pretty-format/src/__tests__/getPrettyPrint.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const getPrettyPrint = (plugins: Plugins) =>
: () => {
const diffString = diff(expected, prettyFormatted, {
expand: this.expand,
omitTrivial: true,
});
return (
this.utils.matcherHint('.toBe') +
Expand Down

0 comments on commit 035b03c

Please sign in to comment.