From 7c472fda0d2e336774f18a66fcc59abaef7bb775 Mon Sep 17 00:00:00 2001 From: Alistair Brown Date: Thu, 7 May 2020 17:31:44 +0100 Subject: [PATCH] fix: Control no diff message color with diff options --- .../__tests__/__snapshots__/diff.test.ts.snap | 4 +++- packages/jest-diff/src/__tests__/diff.test.ts | 14 +++++++++--- packages/jest-diff/src/constants.ts | 11 +++------- packages/jest-diff/src/index.ts | 22 +++++++++++++------ 4 files changed, 32 insertions(+), 19 deletions(-) diff --git a/packages/jest-diff/src/__tests__/__snapshots__/diff.test.ts.snap b/packages/jest-diff/src/__tests__/__snapshots__/diff.test.ts.snap index 6ed36e34275c..962c937514f7 100644 --- a/packages/jest-diff/src/__tests__/__snapshots__/diff.test.ts.snap +++ b/packages/jest-diff/src/__tests__/__snapshots__/diff.test.ts.snap @@ -249,9 +249,11 @@ exports[`options change color diffStringsUnified 1`] = ` - changed from + changed to + insert - common + common `; +exports[`options change color no diff 1`] = `Compared values have no visual difference.`; + exports[`options change indicators diff 1`] = ` < Expected > Received diff --git a/packages/jest-diff/src/__tests__/diff.test.ts b/packages/jest-diff/src/__tests__/diff.test.ts index 3367839b8cfe..e63d9b999b70 100644 --- a/packages/jest-diff/src/__tests__/diff.test.ts +++ b/packages/jest-diff/src/__tests__/diff.test.ts @@ -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) || ''); @@ -975,6 +974,7 @@ describe('options', () => { describe('change color', () => { const options = { changeColor: chalk.bold, + commonColor: chalk.yellow, }; test('diffStringsUnified', () => { @@ -982,17 +982,25 @@ describe('options', () => { 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', () => { diff --git a/packages/jest-diff/src/constants.ts b/packages/jest-diff/src/constants.ts index c8cb4b4bc7f8..b031eacbb578 100644 --- a/packages/jest-diff/src/constants.ts +++ b/packages/jest-diff/src/constants.ts @@ -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.'; diff --git a/packages/jest-diff/src/index.ts b/packages/jest-diff/src/index.ts index 75957a113d88..d8d3bb0d1dea 100644 --- a/packages/jest-diff/src/index.ts +++ b/packages/jest-diff/src/index.ts @@ -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'; @@ -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, @@ -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); @@ -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); } @@ -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); @@ -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); @@ -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; } }