From 48afeb7c1351a4620733003894e9883c65737fc2 Mon Sep 17 00:00:00 2001 From: Mark Pedrotti Date: Wed, 7 Aug 2019 16:32:57 -0400 Subject: [PATCH 1/5] jest-matcher-utils: Add color options to matcherHint --- .../src/__tests__/index.test.ts | 48 +++++++++++++++++++ packages/jest-matcher-utils/src/index.ts | 14 ++++-- 2 files changed, 59 insertions(+), 3 deletions(-) diff --git a/packages/jest-matcher-utils/src/__tests__/index.test.ts b/packages/jest-matcher-utils/src/__tests__/index.test.ts index e5f0ad94cca1..b895b5b7e1ee 100644 --- a/packages/jest-matcher-utils/src/__tests__/index.test.ts +++ b/packages/jest-matcher-utils/src/__tests__/index.test.ts @@ -6,11 +6,13 @@ * */ +import chalk from 'chalk'; import { diff, ensureNumbers, ensureNoExpected, getLabelPrinter, + matcherHint, pluralize, stringify, MatcherHintOptions, @@ -286,3 +288,49 @@ describe('getLabelPrinter', () => { }).toThrow(); }); }); + +describe('matcherHint', () => { + test('expectedColor', () => { + const expectedColor = (arg: string): string => arg; // default (black) color + const expectedArgument = 'n'; + const received = matcherHint( + 'toHaveBeenNthCalledWith', + 'jest.fn()', + expectedArgument, + {expectedColor, secondArgument: '...expected'}, + ); + + const substringNegative = chalk.green(expectedArgument); + + expect(received).not.toMatch(substringNegative); + }); + + test('receivedColor', () => { + const receivedColor = chalk.cyan.bgAnsi256(158); + const receivedArgument = 'received'; + const received = matcherHint('toMatchSnapshot', receivedArgument, '', { + receivedColor, + }); + + const substringNegative = chalk.red(receivedArgument); + const substringPositive = receivedColor(receivedArgument); + + expect(received).not.toMatch(substringNegative); + expect(received).toMatch(substringPositive); + }); + + test('secondArgumentColor', () => { + const secondArgumentColor = chalk.bold; + const secondArgument = 'hint'; + const received = matcherHint('toMatchSnapshot', undefined, 'properties', { + secondArgument, + secondArgumentColor, + }); + + const substringNegative = chalk.green(secondArgumentColor(secondArgument)); + const substringPositive = secondArgumentColor(secondArgument); + + expect(received).not.toMatch(substringNegative); + expect(received).toMatch(substringPositive); + }); +}); diff --git a/packages/jest-matcher-utils/src/index.ts b/packages/jest-matcher-utils/src/index.ts index 682614aa31d8..61da9039e3c1 100644 --- a/packages/jest-matcher-utils/src/index.ts +++ b/packages/jest-matcher-utils/src/index.ts @@ -28,12 +28,17 @@ const PLUGINS = [ AsymmetricMatcher, ]; +type Color = (arg: string) => string; + export type MatcherHintOptions = { comment?: string; + expectedColor?: Color; isDirectExpectCall?: boolean; isNot?: boolean; promise?: string; + receivedColor?: Color; secondArgument?: string; + secondArgumentColor?: Color; }; export {DiffOptions}; @@ -362,16 +367,19 @@ export const matcherHint = ( ) => { const { comment = '', + expectedColor = EXPECTED_COLOR, isDirectExpectCall = false, // seems redundant with received === '' isNot = false, promise = '', + receivedColor = RECEIVED_COLOR, secondArgument = '', + secondArgumentColor = EXPECTED_COLOR, } = options; let hint = ''; let dimString = 'expect'; // concatenate adjacent dim substrings if (!isDirectExpectCall && received !== '') { - hint += DIM_COLOR(dimString + '(') + RECEIVED_COLOR(received); + hint += DIM_COLOR(dimString + '(') + receivedColor(received); dimString = ')'; } @@ -398,9 +406,9 @@ export const matcherHint = ( if (expected === '') { dimString += '()'; } else { - hint += DIM_COLOR(dimString + '(') + EXPECTED_COLOR(expected); + hint += DIM_COLOR(dimString + '(') + expectedColor(expected); if (secondArgument) { - hint += DIM_COLOR(', ') + EXPECTED_COLOR(secondArgument); + hint += DIM_COLOR(', ') + secondArgumentColor(secondArgument); } dimString = ')'; } From e3f97e31e700051448ea930916a361db38b75cf0 Mon Sep 17 00:00:00 2001 From: Mark Pedrotti Date: Wed, 7 Aug 2019 16:41:08 -0400 Subject: [PATCH 2/5] Correct substringNegative in secondArgumentColor test --- packages/jest-matcher-utils/src/__tests__/index.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/jest-matcher-utils/src/__tests__/index.test.ts b/packages/jest-matcher-utils/src/__tests__/index.test.ts index b895b5b7e1ee..22e8d24fb749 100644 --- a/packages/jest-matcher-utils/src/__tests__/index.test.ts +++ b/packages/jest-matcher-utils/src/__tests__/index.test.ts @@ -327,7 +327,7 @@ describe('matcherHint', () => { secondArgumentColor, }); - const substringNegative = chalk.green(secondArgumentColor(secondArgument)); + const substringNegative = chalk.green(secondArgument); const substringPositive = secondArgumentColor(secondArgument); expect(received).not.toMatch(substringNegative); From 4cd599fcf8e4ccd763a435535dcc0c7ea16c7527 Mon Sep 17 00:00:00 2001 From: Mark Pedrotti Date: Wed, 7 Aug 2019 16:42:11 -0400 Subject: [PATCH 3/5] Add comment about Color type --- packages/jest-matcher-utils/src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/jest-matcher-utils/src/index.ts b/packages/jest-matcher-utils/src/index.ts index 61da9039e3c1..a7a8c0d824f8 100644 --- a/packages/jest-matcher-utils/src/index.ts +++ b/packages/jest-matcher-utils/src/index.ts @@ -28,7 +28,7 @@ const PLUGINS = [ AsymmetricMatcher, ]; -type Color = (arg: string) => string; +type Color = (arg: string) => string; // relevant subset of Chalk type export type MatcherHintOptions = { comment?: string; From 7a9ce0d1f4b0c16023704d508af7dd870c4d6161 Mon Sep 17 00:00:00 2001 From: Mark Pedrotti Date: Wed, 7 Aug 2019 16:46:37 -0400 Subject: [PATCH 4/5] Export type MatcherHintColor --- packages/jest-matcher-utils/src/index.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/jest-matcher-utils/src/index.ts b/packages/jest-matcher-utils/src/index.ts index a7a8c0d824f8..984f031c871a 100644 --- a/packages/jest-matcher-utils/src/index.ts +++ b/packages/jest-matcher-utils/src/index.ts @@ -28,17 +28,17 @@ const PLUGINS = [ AsymmetricMatcher, ]; -type Color = (arg: string) => string; // relevant subset of Chalk type +type MatcherHintColor = (arg: string) => string; // subset of Chalk type export type MatcherHintOptions = { comment?: string; - expectedColor?: Color; + expectedColor?: MatcherHintColor; isDirectExpectCall?: boolean; isNot?: boolean; promise?: string; - receivedColor?: Color; + receivedColor?: MatcherHintColor; secondArgument?: string; - secondArgumentColor?: Color; + secondArgumentColor?: MatcherHintColor; }; export {DiffOptions}; From 87e4a3561e1be7797aa814c17371885e86b6a4b3 Mon Sep 17 00:00:00 2001 From: Mark Pedrotti Date: Wed, 7 Aug 2019 16:52:47 -0400 Subject: [PATCH 5/5] Update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6283466826be..e60cf5d47945 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ - `[@jest/core, @jest/test-sequencer]` Support async sort in custom `testSequencer` ([#8642](https://github.com/facebook/jest/pull/8642)) - `[jest-runtime, @jest/fake-timers]` Add `jest.advanceTimersToNextTimer` ([#8713](https://github.com/facebook/jest/pull/8713)) - `[@jest-transform]` Extract transforming require logic within `jest-core` into `@jest-transform` ([#8756](https://github.com/facebook/jest/pull/8756)) +- `[jest-matcher-utils]` Add color options to `matcherHint` ([#8795](https://github.com/facebook/jest/pull/8795)) ### Fixes