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

Highlight only last of odd length leading spaces in jest-diff #4558

Merged
merged 1 commit into from
Sep 28, 2017
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
15 changes: 15 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 @@ -183,4 +183,19 @@ exports[`falls back to not call toJSON if objects look identical 1`] = `
<dim> }</>"
`;

exports[`highlight only the last in odd length of leading spaces (expanded) 1`] = `
"<green>- Expected</>
<red>+ Received</>

<dim> <pre></>
<green>- attributes.reduce(function (props, attribute) {</>
<green>- <inverse> </>props[attribute.name] = attribute.value;</>
<red>+ attributes.reduce((props, {name, value}) =&gt; {</>
<red>+ props[name] = value;</>
<dim> return props;</>
<green>- <inverse> </>}, {});</>
<red>+ }, {});</>
<dim> </pre></>"
`;

exports[`prints a fallback message if two objects truly look identical 1`] = `"<dim>Compared values have no visual difference.</>"`;
34 changes: 34 additions & 0 deletions packages/jest-diff/src/__tests__/diff.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -761,6 +761,40 @@ describe('background color of spaces', () => {
});
});

describe('highlight only the last in odd length of leading spaces', () => {
const pre5 = {
$$typeof: elementSymbol,
props: {
children: [
'attributes.reduce(function (props, attribute) {',
' props[attribute.name] = attribute.value;', // 3 leading spaces
' return props;', // 2 leading spaces
' }, {});', // 1 leading space
].join('\n'),
},
type: 'pre',
};
const pre6 = {
$$typeof: elementSymbol,
props: {
children: [
'attributes.reduce((props, {name, value}) => {',
' props[name] = value;', // from 3 to 2 leading spaces
' return props;', // unchanged 2 leading spaces
'}, {});', // from 1 to 0 leading spaces
].join('\n'),
},
type: 'pre',
};
const received = diff(pre5, pre6, expanded);
test('(expanded)', () => {
expect(received).toMatchSnapshot();
});
test('(unexpanded)', () => {
expect(diff(pre5, pre6, unexpanded)).toBe(received);
});
});

test('collapses big diffs to patch format', () => {
const result = diff(
{test: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]},
Expand Down
7 changes: 6 additions & 1 deletion packages/jest-diff/src/diff_strings.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,12 @@ const highlightLeadingTrailingSpaces = (
line: string,
bgColor: Function,
): string =>
highlightTrailingSpaces(line.replace(/^\s+/, bgColor('$&')), bgColor);
// If line consists of ALL spaces: highlight all of them.
highlightTrailingSpaces(line, bgColor).replace(
// If line has an ODD length of leading spaces: highlight only the LAST.
/^(\s\s)*(\s)(?=[^\s])/,
'$1' + bgColor('$2'),
);

const getAnnotation = (options: ?DiffOptions): string =>
chalk.green('- ' + ((options && options.aAnnotation) || 'Expected')) +
Expand Down