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

fix: Control no diff message color with diff options #9997

Merged
merged 1 commit into from
May 7, 2020
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Fixes

- `[jest-jasmine2]` Stop adding `:` after an error that has no message ([#9990](https://github.com/facebook/jest/pull/9990))
- `[jest-diff]` Control no diff message color with `commonColor` in diff options ([#9997](https://github.com/facebook/jest/pull/9997))

### Chore & Maintenance

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,9 +249,11 @@ exports[`options change color diffStringsUnified 1`] = `
<g>- changed <b>from</></>
<r>+ changed <b>to</></>
<r>+ insert</>
<d> common</>
<y> common</>
`;

exports[`options change color no diff 1`] = `<y>Compared values have no visual difference.</>`;

exports[`options change indicators diff 1`] = `
<g>< Expected</>
<r>> Received</>
Expand Down
14 changes: 11 additions & 3 deletions packages/jest-diff/src/__tests__/diff.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,12 @@ import {diffLinesUnified, diffLinesUnified2} from '../diffLines';
import {noColor} from '../normalizeDiffOptions';
import {diffStringsUnified} from '../printDiffs';
import {DiffOptions} from '../types';
import {NO_DIFF_MESSAGE} from '../constants';

const optionsCounts: DiffOptions = {
includeChangeCounts: true,
};

const NO_DIFF_MESSAGE = 'Compared values have no visual difference.';

// Use only in toBe assertions for edge case messages.
const stripped = (a: unknown, b: unknown) => stripAnsi(diff(a, b) || '');

Expand Down Expand Up @@ -975,24 +974,33 @@ describe('options', () => {
describe('change color', () => {
const options = {
changeColor: chalk.bold,
commonColor: chalk.yellow,
};

test('diffStringsUnified', () => {
const aChanged = a.join('\n').replace('change', 'changed');
const bChanged = b.join('\n').replace('change', 'changed');
expect(diffStringsUnified(aChanged, bChanged, options)).toMatchSnapshot();
});

test('no diff', () => {
expect(diff(a, a, options)).toMatchSnapshot();
});
});

describe('common', () => {
const options = {
commonColor: line => line,
commonColor: noColor,
commonIndicator: '=',
};

test('diff', () => {
expect(diff(a, b, options)).toMatchSnapshot();
});

test('no diff', () => {
expect(diff(a, a, options)).toBe(NO_DIFF_MESSAGE);
});
});

describe('includeChangeCounts false', () => {
Expand Down
11 changes: 3 additions & 8 deletions packages/jest-diff/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,8 @@
* LICENSE file in the root directory of this source tree.
*/

import chalk = require('chalk');
export const NO_DIFF_MESSAGE = 'Compared values have no visual difference.';

export const NO_DIFF_MESSAGE = chalk.dim(
'Compared values have no visual difference.',
);

export const SIMILAR_MESSAGE = chalk.dim(
export const SIMILAR_MESSAGE =
'Compared values serialize to the same structure.\n' +
'Printing internal object structure without calling `toJSON` instead.',
);
'Printing internal object structure without calling `toJSON` instead.';
22 changes: 15 additions & 7 deletions packages/jest-diff/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import prettyFormat = require('pretty-format');
import chalk = require('chalk');
import getType = require('jest-get-type');
import {DIFF_DELETE, DIFF_EQUAL, DIFF_INSERT, Diff} from './cleanupSemantic';
import {normalizeDiffOptions} from './normalizeDiffOptions';
import {diffLinesRaw, diffLinesUnified, diffLinesUnified2} from './diffLines';
import {diffStringsRaw, diffStringsUnified} from './printDiffs';
import {NO_DIFF_MESSAGE, SIMILAR_MESSAGE} from './constants';
Expand All @@ -20,6 +21,11 @@ export {diffLinesRaw, diffLinesUnified, diffLinesUnified2};
export {diffStringsRaw, diffStringsUnified};
export {DIFF_DELETE, DIFF_EQUAL, DIFF_INSERT, Diff};

const getCommonMessage = (message: string, options?: DiffOptions) => {
const {commonColor} = normalizeDiffOptions(options);
return commonColor(message);
};

const {
AsymmetricMatcher,
DOMCollection,
Expand Down Expand Up @@ -53,7 +59,7 @@ const FALLBACK_FORMAT_OPTIONS_0 = {...FALLBACK_FORMAT_OPTIONS, indent: 0};
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
function diff(a: any, b: any, options?: DiffOptions): string | null {
if (Object.is(a, b)) {
return NO_DIFF_MESSAGE;
return getCommonMessage(NO_DIFF_MESSAGE, options);
}

const aType = getType(a);
Expand Down Expand Up @@ -109,7 +115,7 @@ function comparePrimitive(
const aFormat = prettyFormat(a, FORMAT_OPTIONS);
const bFormat = prettyFormat(b, FORMAT_OPTIONS);
return aFormat === bFormat
? NO_DIFF_MESSAGE
? getCommonMessage(NO_DIFF_MESSAGE, options)
: diffLinesUnified(aFormat.split('\n'), bFormat.split('\n'), options);
}

Expand All @@ -128,13 +134,14 @@ function compareObjects(
) {
let difference;
let hasThrown = false;
const noDiffMessage = getCommonMessage(NO_DIFF_MESSAGE, options);

try {
const aCompare = prettyFormat(a, FORMAT_OPTIONS_0);
const bCompare = prettyFormat(b, FORMAT_OPTIONS_0);

if (aCompare === bCompare) {
difference = NO_DIFF_MESSAGE;
difference = noDiffMessage;
} else {
const aDisplay = prettyFormat(a, FORMAT_OPTIONS);
const bDisplay = prettyFormat(b, FORMAT_OPTIONS);
Expand All @@ -153,12 +160,12 @@ function compareObjects(

// If the comparison yields no results, compare again but this time
// without calling `toJSON`. It's also possible that toJSON might throw.
if (difference === undefined || difference === NO_DIFF_MESSAGE) {
if (difference === undefined || difference === noDiffMessage) {
const aCompare = prettyFormat(a, FALLBACK_FORMAT_OPTIONS_0);
const bCompare = prettyFormat(b, FALLBACK_FORMAT_OPTIONS_0);

if (aCompare === bCompare) {
difference = NO_DIFF_MESSAGE;
difference = noDiffMessage;
} else {
const aDisplay = prettyFormat(a, FALLBACK_FORMAT_OPTIONS);
const bDisplay = prettyFormat(b, FALLBACK_FORMAT_OPTIONS);
Expand All @@ -172,8 +179,9 @@ function compareObjects(
);
}

if (difference !== NO_DIFF_MESSAGE && !hasThrown) {
difference = SIMILAR_MESSAGE + '\n\n' + difference;
if (difference !== noDiffMessage && !hasThrown) {
difference =
getCommonMessage(SIMILAR_MESSAGE, options) + '\n\n' + difference;
}
}

Expand Down