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

Support returning diff from oneline strings #6221

Merged
merged 4 commits into from
May 22, 2018
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -92,6 +92,8 @@
([#6181](https://github.com/facebook/jest/pull/6181))
* `[expect]` Include custom mock names in error messages
([#6199](https://github.com/facebook/jest/pull/6199))
* `[jest-diff]` Support returning diff from oneline strings
([#6221](https://github.com/facebook/jest/pull/6221))
* `[expect]` Improve return matchers
([#6172](https://github.com/facebook/jest/pull/6172))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,9 @@ exports[`works with named snapshot failures 1`] = `

Received value does not match stored snapshot \\"failing named snapshot: snapname 1\\".

- Snapshot
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

odd that this shows up. fair enough, though

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm surprised too, but that's probably because I've adjusted the regex to check multiline/online

+ Received

- \\"bar\\"
+ \\"foo\\"

Expand Down Expand Up @@ -840,6 +843,9 @@ exports[`works with snapshot failures 1`] = `

Received value does not match stored snapshot \\"failing snapshot 1\\".

- Snapshot
+ Received

- \\"bar\\"
+ \\"foo\\"

Expand Down
17 changes: 8 additions & 9 deletions packages/expect/src/matchers.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
iterableEquality,
subsetEquality,
typeEquality,
isOneline,
} from './utils';
import {equals} from './jasmine_utils';

Expand Down Expand Up @@ -59,10 +60,8 @@ const matchers: MatchersObject = {
getType(received) === getType(expected) &&
(getType(received) === 'object' || getType(expected) === 'array') &&
equals(received, expected, [iterableEquality]);

const diffString = diff(expected, received, {
expand: this.expand,
});
const oneline = isOneline(expected, received);
const diffString = diff(expected, received, {expand: this.expand});

return (
matcherHint('.toBe', undefined, undefined, {
Expand All @@ -72,7 +71,7 @@ const matchers: MatchersObject = {
'\n\n' +
`Expected: ${printExpected(expected)}\n` +
`Received: ${printReceived(received)}` +
(diffString ? `\n\nDifference:\n\n${diffString}` : '') +
(diffString && !oneline ? `\n\nDifference:\n\n${diffString}` : '') +
(suggestToEqual ? ` ${SUGGEST_TO_EQUAL}` : '')
);
};
Expand Down Expand Up @@ -375,17 +374,17 @@ const matchers: MatchersObject = {
`Received:\n` +
` ${printReceived(received)}`
: () => {
const diffString = diff(expected, received, {
expand: this.expand,
});
const oneline = isOneline(expected, received);
const diffString = diff(expected, received, {expand: this.expand});

return (
matcherHint('.toEqual') +
'\n\n' +
`Expected value to equal:\n` +
` ${printExpected(expected)}\n` +
`Received:\n` +
` ${printReceived(received)}` +
(diffString ? `\n\nDifference:\n\n${diffString}` : '')
(diffString && !oneline ? `\n\nDifference:\n\n${diffString}` : '')
);
};

Expand Down
5 changes: 3 additions & 2 deletions packages/expect/src/spy_matchers.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
RECEIVED_COLOR,
} from 'jest-matcher-utils';
import {equals} from './jasmine_utils';
import {iterableEquality, partition} from './utils';
import {iterableEquality, partition, isOneline} from './utils';
import diff from 'jest-diff';

const createToBeCalledMatcher = matcherName => (received, expected) => {
Expand Down Expand Up @@ -555,12 +555,13 @@ const formatMismatchedArgs = (expected, received) => {
const printedArgs = [];
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]);
printedArgs.push(
` ${printExpected(expected[i])}\n` +
`as argument ${i + 1}, but it was called with\n` +
` ${printReceived(received[i])}.` +
(diffString ? `\n\nDifference:\n\n${diffString}` : ''),
(diffString && !oneline ? `\n\nDifference:\n\n${diffString}` : ''),
);
} else if (i >= expected.length) {
printedArgs.push(
Expand Down
7 changes: 7 additions & 0 deletions packages/expect/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,3 +221,10 @@ export const isError = (value: any) => {
export function emptyObject(obj: any) {
return obj && typeof obj === 'object' ? !Object.keys(obj).length : false;
}

const MULTILINE_REGEXP = /[\r\n]/;

export const isOneline = (expected: any, received: any) =>
typeof expected === 'string' &&
typeof received === 'string' &&
(!MULTILINE_REGEXP.test(expected) || !MULTILINE_REGEXP.test(received));
34 changes: 34 additions & 0 deletions packages/jest-diff/src/__tests__/__snapshots__/diff.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -208,3 +208,37 @@ exports[`highlight only the last in odd length of leading spaces (expanded) 1`]
<red>+ }, {});</>
<dim> </pre></>"
`;

exports[`oneline strings 1`] = `
"<green>- Expected</>
<red>+ Received</>

<green>- ab</>
<red>+ aa</>"
`;

exports[`oneline strings 2`] = `
"<green>- Expected</>
<red>+ Received</>

<green>- 123456789</>
<red>+ 234567890</>"
`;

exports[`oneline strings 3`] = `
"<green>- Expected</>
<red>+ Received</>

<green>- oneline</>
<red>+ multi</>
<red>+ line</>"
`;

exports[`oneline strings 4`] = `
"<green>- Expected</>
<red>+ Received</>

<green>- multi</>
<green>- line</>
<red>+ oneline</>"
`;
10 changes: 4 additions & 6 deletions packages/jest-diff/src/__tests__/diff.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,10 @@ describe('no visual difference', () => {
});

test('oneline strings', () => {
// oneline strings don't produce a diff currently.
expect(diff('ab', 'aa')).toBe(null);
expect(diff('123456789', '234567890')).toBe(null);
// if either string is oneline
expect(diff('oneline', 'multi\nline')).toBe(null);
expect(diff('multi\nline', 'oneline')).toBe(null);
expect(diff('ab', 'aa')).toMatchSnapshot();
expect(diff('123456789', '234567890')).toMatchSnapshot();
expect(diff('oneline', 'multi\nline')).toMatchSnapshot();
expect(diff('multi\nline', 'oneline')).toMatchSnapshot();
});

describe('falls back to not call toJSON', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-diff/src/diff_strings.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@

import chalk from 'chalk';
import {diffLines, structuredPatch} from 'diff';

import {NO_DIFF_MESSAGE} from './constants.js';

const DIFF_CONTEXT_DEFAULT = 5;

export type DiffOptions = {|
Expand Down
8 changes: 1 addition & 7 deletions packages/jest-diff/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ const FALLBACK_FORMAT_OPTIONS_0 = Object.assign({}, FALLBACK_FORMAT_OPTIONS, {
indent: 0,
});

const MULTILINE_REGEXP = /[\r\n]/;

// 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 {
Expand Down Expand Up @@ -88,11 +86,7 @@ function diff(a: any, b: any, options: ?DiffOptions): ?string {

switch (aType) {
case 'string':
const multiline = MULTILINE_REGEXP.test(a) && b.indexOf('\n') !== -1;
if (multiline) {
return diffStrings(a, b, options);
}
return null;
return diffStrings(a, b, options);
case 'number':
case 'boolean':
return null;
Expand Down