Skip to content

Commit fd10650

Browse files
committed
add diff and hint colors to context
1 parent c8890be commit fd10650

File tree

8 files changed

+85
-63
lines changed

8 files changed

+85
-63
lines changed

packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3949,7 +3949,7 @@ Received has value: <r>undefined</>
39493949
exports[`.toMatch() throws if non String/RegExp expected value passed: ["foo", []] 1`] = `
39503950
expect(received</>).toMatch(<y>expected</>)
39513951

3952-
<b>Matcher error</>: expected</> value must be a string or regular expression
3952+
<b>Matcher error</>: <y>expected</> value must be a string or regular expression
39533953

39543954
Expected has type: array
39553955
Expected has value: <g>[]</>
@@ -3958,7 +3958,7 @@ Expected has value: <g>[]</>
39583958
exports[`.toMatch() throws if non String/RegExp expected value passed: ["foo", [Function anonymous]] 1`] = `
39593959
expect(received</>).toMatch(<y>expected</>)
39603960

3961-
<b>Matcher error</>: expected</> value must be a string or regular expression
3961+
<b>Matcher error</>: <y>expected</> value must be a string or regular expression
39623962

39633963
Expected has type: function
39643964
Expected has value: <g>[Function anonymous]</>
@@ -3967,7 +3967,7 @@ Expected has value: <g>[Function anonymous]</>
39673967
exports[`.toMatch() throws if non String/RegExp expected value passed: ["foo", {}] 1`] = `
39683968
expect(received</>).toMatch(<y>expected</>)
39693969

3970-
<b>Matcher error</>: expected</> value must be a string or regular expression
3970+
<b>Matcher error</>: <y>expected</> value must be a string or regular expression
39713971

39723972
Expected has type: object
39733973
Expected has value: <g>{}</>
@@ -3976,7 +3976,7 @@ Expected has value: <g>{}</>
39763976
exports[`.toMatch() throws if non String/RegExp expected value passed: ["foo", 1] 1`] = `
39773977
expect(received</>).toMatch(<y>expected</>)
39783978

3979-
<b>Matcher error</>: expected</> value must be a string or regular expression
3979+
<b>Matcher error</>: <y>expected</> value must be a string or regular expression
39803980

39813981
Expected has type: number
39823982
Expected has value: <g>1</>
@@ -3985,7 +3985,7 @@ Expected has value: <g>1</>
39853985
exports[`.toMatch() throws if non String/RegExp expected value passed: ["foo", true] 1`] = `
39863986
expect(received</>).toMatch(<y>expected</>)
39873987

3988-
<b>Matcher error</>: expected</> value must be a string or regular expression
3988+
<b>Matcher error</>: <y>expected</> value must be a string or regular expression
39893989

39903990
Expected has type: boolean
39913991
Expected has value: <g>true</>
@@ -3994,7 +3994,7 @@ Expected has value: <g>true</>
39943994
exports[`.toMatch() throws if non String/RegExp expected value passed: ["foo", undefined] 1`] = `
39953995
expect(received</>).toMatch(<y>expected</>)
39963996

3997-
<b>Matcher error</>: expected</> value must be a string or regular expression
3997+
<b>Matcher error</>: <y>expected</> value must be a string or regular expression
39983998

39993999
Expected has value: <g>undefined</>
40004000
`;
@@ -4741,7 +4741,7 @@ Received has value: <r>"44"</>
47414741
exports[`toMatchObject() throws expect({}).toMatchObject("some string") 1`] = `
47424742
expect(received</>).toMatchObject(<y>expected</>)
47434743

4744-
<b>Matcher error</>: expected</> value must be a non-null object
4744+
<b>Matcher error</>: <y>expected</> value must be a non-null object
47454745

47464746
Expected has type: string
47474747
Expected has value: <g>"some string"</>
@@ -4750,7 +4750,7 @@ Expected has value: <g>"some string"</>
47504750
exports[`toMatchObject() throws expect({}).toMatchObject(4) 1`] = `
47514751
expect(received</>).toMatchObject(<y>expected</>)
47524752

4753-
<b>Matcher error</>: expected</> value must be a non-null object
4753+
<b>Matcher error</>: <y>expected</> value must be a non-null object
47544754

47554755
Expected has type: number
47564756
Expected has value: <g>4</>
@@ -4759,15 +4759,15 @@ Expected has value: <g>4</>
47594759
exports[`toMatchObject() throws expect({}).toMatchObject(null) 1`] = `
47604760
expect(received</>).toMatchObject(<y>expected</>)
47614761

4762-
<b>Matcher error</>: expected</> value must be a non-null object
4762+
<b>Matcher error</>: <y>expected</> value must be a non-null object
47634763

47644764
Expected has value: <g>null</>
47654765
`;
47664766

47674767
exports[`toMatchObject() throws expect({}).toMatchObject(true) 1`] = `
47684768
expect(received</>).toMatchObject(<y>expected</>)
47694769

4770-
<b>Matcher error</>: expected</> value must be a non-null object
4770+
<b>Matcher error</>: <y>expected</> value must be a non-null object
47714771

47724772
Expected has type: boolean
47734773
Expected has value: <g>true</>
@@ -4776,7 +4776,7 @@ Expected has value: <g>true</>
47764776
exports[`toMatchObject() throws expect({}).toMatchObject(undefined) 1`] = `
47774777
expect(received</>).toMatchObject(<y>expected</>)
47784778

4779-
<b>Matcher error</>: expected</> value must be a non-null object
4779+
<b>Matcher error</>: <y>expected</> value must be a non-null object
47804780

47814781
Expected has value: <g>undefined</>
47824782
`;

packages/expect/src/__tests__/extend.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ it('exposes matcherUtils in context', () => {
9494
const pass: boolean = this.equals(
9595
this.utils,
9696
Object.assign(matcherUtils, {
97+
diffExpectedColor: matcherUtils.EXPECTED_COLOR,
98+
diffReceivedColor: matcherUtils.RECEIVED_COLOR,
99+
hintExpectedColor: matcherUtils.EXPECTED_COLOR,
100+
hintReceivedColor: matcherUtils.RECEIVED_COLOR,
97101
iterableEquality,
98102
subsetEquality,
99103
}),

packages/expect/src/asymmetricMatchers.ts

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
subsetEquality,
1414
} from '@jest/expect-utils';
1515
import * as matcherUtils from 'jest-matcher-utils';
16+
import {EXPECTED_COLOR, RECEIVED_COLOR} from 'jest-matcher-utils';
1617
import {pluralize} from 'jest-util';
1718
import {getCustomEqualityTesters, getState} from './jestMatchersObject';
1819
import type {
@@ -34,11 +35,25 @@ function fnNameFor(func: () => unknown) {
3435
return matches ? matches[1] : '<anonymous>';
3536
}
3637

37-
const utils = Object.freeze({
38-
...matcherUtils,
39-
iterableEquality,
40-
subsetEquality,
41-
});
38+
const utils = (function () {
39+
const state = getState<MatcherState>();
40+
const hintExpectedColor =
41+
state.matcherHintOptions?.expectedColor ?? EXPECTED_COLOR;
42+
const hintReceivedColor =
43+
state.matcherHintOptions?.receivedColor ?? RECEIVED_COLOR;
44+
const diffExpectedColor = state.diffOptions?.aColor ?? EXPECTED_COLOR;
45+
const diffReceivedColor = state.diffOptions?.bColor ?? RECEIVED_COLOR;
46+
47+
return Object.freeze({
48+
...matcherUtils,
49+
diffExpectedColor,
50+
diffReceivedColor,
51+
hintExpectedColor,
52+
hintReceivedColor,
53+
iterableEquality,
54+
subsetEquality,
55+
});
56+
})();
4257

4358
function getPrototype(obj: object) {
4459
if (Object.getPrototypeOf) {

packages/expect/src/index.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,8 +287,22 @@ const makeThrowingMatcher = (
287287
): ThrowingMatcherFn =>
288288
function throwingMatcher(...args): any {
289289
let throws = true;
290+
const state = getState<MatcherState>();
291+
const hintExpectedColor =
292+
state.matcherHintOptions?.expectedColor ?? matcherUtils.EXPECTED_COLOR;
293+
const hintReceivedColor =
294+
state.matcherHintOptions?.receivedColor ?? matcherUtils.RECEIVED_COLOR;
295+
const diffExpectedColor =
296+
state.diffOptions?.aColor ?? matcherUtils.EXPECTED_COLOR;
297+
const diffReceivedColor =
298+
state.diffOptions?.bColor ?? matcherUtils.RECEIVED_COLOR;
299+
290300
const utils: MatcherUtils['utils'] = {
291301
...matcherUtils,
302+
diffExpectedColor,
303+
diffReceivedColor,
304+
hintExpectedColor,
305+
hintReceivedColor,
292306
iterableEquality,
293307
subsetEquality,
294308
};

packages/expect/src/matchers.ts

Lines changed: 25 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@ import {noColor} from 'jest-diff';
2323
import {getType, isPrimitive} from 'jest-get-type';
2424
import {
2525
DIM_COLOR,
26-
EXPECTED_COLOR,
2726
MatcherHintOptions,
28-
RECEIVED_COLOR,
2927
SUGGEST_TO_CONTAIN_EQUAL,
3028
ensureExpectedIsNonNegativeInteger,
3129
ensureNoExpected,
@@ -157,14 +155,12 @@ const matchers: MatchersObject = {
157155
secondArgument,
158156
secondArgumentColor: (arg: string) => arg,
159157
};
160-
const hintExpectedColor = options.expectedColor ?? EXPECTED_COLOR;
161-
const hintReceivedColor = options.receivedColor ?? RECEIVED_COLOR;
162158

163159
if (typeof expected !== 'number') {
164160
throw new Error(
165161
matcherErrorMessage(
166162
matcherHint(matcherName, undefined, undefined, options),
167-
`${hintExpectedColor('expected')} value must be a number`,
163+
`${this.utils.hintExpectedColor('expected')} value must be a number`,
168164
printWithType('Expected', expected, printExpected),
169165
),
170166
);
@@ -174,7 +170,7 @@ const matchers: MatchersObject = {
174170
throw new Error(
175171
matcherErrorMessage(
176172
matcherHint(matcherName, undefined, undefined, options),
177-
`${hintReceivedColor('received')} value must be a number`,
173+
`${this.utils.hintReceivedColor('received')} value must be a number`,
178174
printWithType('Received', received, printReceived),
179175
),
180176
);
@@ -326,13 +322,13 @@ const matchers: MatchersObject = {
326322
promise: this.promise,
327323
};
328324

329-
const hintExpectedColor = options.expectedColor ?? EXPECTED_COLOR;
330-
331325
if (typeof expected !== 'function') {
332326
throw new Error(
333327
matcherErrorMessage(
334328
matcherHint(matcherName, undefined, undefined, options),
335-
`${hintExpectedColor('expected')} value must be a function`,
329+
`${this.utils.hintExpectedColor(
330+
'expected',
331+
)} value must be a function`,
336332
printWithType('Expected', expected, printExpected),
337333
),
338334
);
@@ -523,14 +519,11 @@ const matchers: MatchersObject = {
523519
promise: this.promise,
524520
};
525521

526-
const hintReceivedColor = options.receivedColor ?? RECEIVED_COLOR;
527-
const hintExpectedColor = options.expectedColor ?? EXPECTED_COLOR;
528-
529522
if (received == null) {
530523
throw new Error(
531524
matcherErrorMessage(
532525
matcherHint(matcherName, undefined, undefined, options),
533-
`${hintReceivedColor(
526+
`${this.utils.hintReceivedColor(
534527
'received',
535528
)} value must not be null nor undefined`,
536529
printWithType('Received', received, printReceived),
@@ -539,9 +532,9 @@ const matchers: MatchersObject = {
539532
}
540533

541534
if (typeof received === 'string') {
542-
const wrongTypeErrorMessage = `${hintExpectedColor(
535+
const wrongTypeErrorMessage = `${this.utils.hintExpectedColor(
543536
'expected',
544-
)} value must be a string if ${hintReceivedColor(
537+
)} value must be a string if ${this.utils.hintReceivedColor(
545538
'received',
546539
)} value is a string`;
547540

@@ -635,13 +628,11 @@ const matchers: MatchersObject = {
635628
promise: this.promise,
636629
};
637630

638-
const hintReceivedColor = options.receivedColor ?? RECEIVED_COLOR;
639-
640631
if (received == null) {
641632
throw new Error(
642633
matcherErrorMessage(
643634
matcherHint(matcherName, undefined, undefined, options),
644-
`${hintReceivedColor(
635+
`${this.utils.hintReceivedColor(
645636
'received',
646637
)} value must not be null nor undefined`,
647638
printWithType('Received', received, printReceived),
@@ -735,13 +726,11 @@ const matchers: MatchersObject = {
735726
promise: this.promise,
736727
};
737728

738-
const hintReceivedColor = options.receivedColor ?? RECEIVED_COLOR;
739-
740729
if (typeof received?.length !== 'number') {
741730
throw new Error(
742731
matcherErrorMessage(
743732
matcherHint(matcherName, undefined, undefined, options),
744-
`${hintReceivedColor(
733+
`${this.utils.hintReceivedColor(
745734
'received',
746735
)} value must have a length property whose value must be a number`,
747736
printWithType('Received', received, printReceived),
@@ -802,14 +791,11 @@ const matchers: MatchersObject = {
802791
secondArgument: hasValue ? 'value' : '',
803792
};
804793

805-
const hintExpectedColor = options.expectedColor ?? EXPECTED_COLOR;
806-
const hintReceivedColor = options.receivedColor ?? RECEIVED_COLOR;
807-
808794
if (received === null || received === undefined) {
809795
throw new Error(
810796
matcherErrorMessage(
811797
matcherHint(matcherName, undefined, expectedArgument, options),
812-
`${hintReceivedColor(
798+
`${this.utils.hintReceivedColor(
813799
'received',
814800
)} value must not be null nor undefined`,
815801
printWithType('Received', received, printReceived),
@@ -823,7 +809,9 @@ const matchers: MatchersObject = {
823809
throw new Error(
824810
matcherErrorMessage(
825811
matcherHint(matcherName, undefined, expectedArgument, options),
826-
`${hintExpectedColor('expected')} path must be a string or array`,
812+
`${this.utils.hintExpectedColor(
813+
'expected',
814+
)} path must be a string or array`,
827815
printWithType('Expected', expectedPath, printExpected),
828816
),
829817
);
@@ -838,7 +826,9 @@ const matchers: MatchersObject = {
838826
throw new Error(
839827
matcherErrorMessage(
840828
matcherHint(matcherName, undefined, expectedArgument, options),
841-
`${hintExpectedColor('expected')} path must not be an empty array`,
829+
`${this.utils.hintExpectedColor(
830+
'expected',
831+
)} path must not be an empty array`,
842832
printWithType('Expected', expectedPath, printExpected),
843833
),
844834
);
@@ -929,14 +919,11 @@ const matchers: MatchersObject = {
929919
promise: this.promise,
930920
};
931921

932-
const hintExpectedColor = options.receivedColor ?? EXPECTED_COLOR;
933-
const hintReceivedColor = options.receivedColor ?? RECEIVED_COLOR;
934-
935922
if (typeof received !== 'string') {
936923
throw new Error(
937924
matcherErrorMessage(
938925
matcherHint(matcherName, undefined, undefined, options),
939-
`${hintReceivedColor('received')} value must be a string`,
926+
`${this.utils.hintReceivedColor('received')} value must be a string`,
940927
printWithType('Received', received, printReceived),
941928
),
942929
);
@@ -949,7 +936,7 @@ const matchers: MatchersObject = {
949936
throw new Error(
950937
matcherErrorMessage(
951938
matcherHint(matcherName, undefined, undefined, options),
952-
`${hintExpectedColor(
939+
`${this.utils.hintExpectedColor(
953940
'expected',
954941
)} value must be a string or regular expression`,
955942
printWithType('Expected', expected, printExpected),
@@ -1023,14 +1010,13 @@ const matchers: MatchersObject = {
10231010
promise: this.promise,
10241011
};
10251012

1026-
const hintReceivedColor = options.receivedColor ?? RECEIVED_COLOR;
1027-
const hintExpectedColor = options.receivedColor ?? EXPECTED_COLOR;
1028-
10291013
if (typeof received !== 'object' || received === null) {
10301014
throw new Error(
10311015
matcherErrorMessage(
10321016
matcherHint(matcherName, undefined, undefined, options),
1033-
`${hintReceivedColor('received')} value must be a non-null object`,
1017+
`${this.utils.hintReceivedColor(
1018+
'received',
1019+
)} value must be a non-null object`,
10341020
printWithType('Received', received, printReceived),
10351021
),
10361022
);
@@ -1040,7 +1026,9 @@ const matchers: MatchersObject = {
10401026
throw new Error(
10411027
matcherErrorMessage(
10421028
matcherHint(matcherName, undefined, undefined, options),
1043-
`${hintExpectedColor('expected')} value must be a non-null object`,
1029+
`${this.utils.hintExpectedColor(
1030+
'expected',
1031+
)} value must be a non-null object`,
10441032
printWithType('Expected', expected, printExpected),
10451033
),
10461034
);

0 commit comments

Comments
 (0)