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

jest-matcher-utils: Add color options to matcherHint #8795

Merged
merged 5 commits into from
Aug 9, 2019
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 @@ -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

Expand Down
48 changes: 48 additions & 0 deletions packages/jest-matcher-utils/src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
*
*/

import chalk from 'chalk';
import {
diff,
ensureNumbers,
ensureNoExpected,
getLabelPrinter,
matcherHint,
pluralize,
stringify,
MatcherHintOptions,
Expand Down Expand Up @@ -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(secondArgument);
const substringPositive = secondArgumentColor(secondArgument);

expect(received).not.toMatch(substringNegative);
expect(received).toMatch(substringPositive);
});
});
14 changes: 11 additions & 3 deletions packages/jest-matcher-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,17 @@ const PLUGINS = [
AsymmetricMatcher,
];

type MatcherHintColor = (arg: string) => string; // subset of Chalk type
SimenB marked this conversation as resolved.
Show resolved Hide resolved

export type MatcherHintOptions = {
comment?: string;
expectedColor?: MatcherHintColor;
isDirectExpectCall?: boolean;
isNot?: boolean;
promise?: string;
receivedColor?: MatcherHintColor;
secondArgument?: string;
secondArgumentColor?: MatcherHintColor;
};

export {DiffOptions};
Expand Down Expand Up @@ -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 = ')';
}

Expand All @@ -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 = ')';
}
Expand Down