6
6
*
7
7
*/
8
8
9
- /* eslint-disable local/prefer-spread-eventually */
10
-
9
+ import type { ChalkFunction } from 'chalk' ;
11
10
import { equals , iterableEquality , subsetEquality } from '@jest/expect-utils' ;
12
11
import * as matcherUtils from 'jest-matcher-utils' ;
13
12
import { isPromise } from 'jest-util' ;
@@ -150,33 +149,32 @@ export const expect: Expect = (actual: any, ...rest: Array<any>) => {
150
149
return expectation ;
151
150
} ;
152
151
153
- const getMessage = ( message ?: ( ) => string ) =>
152
+ const getMessage = ( receivedColor : ChalkFunction , message ?: ( ) => string ) =>
154
153
( message && message ( ) ) ||
155
- matcherUtils . RECEIVED_COLOR ( 'No message was specified for this matcher.' ) ;
156
-
157
- const makeResolveMatcher =
158
- (
159
- matcherName : string ,
160
- matcher : RawMatcherFn ,
161
- isNot : boolean ,
162
- actual : Promise < any > ,
163
- outerErr : JestAssertionError ,
164
- ) : PromiseMatcherFn =>
165
- ( ...args ) => {
154
+ receivedColor ( 'No message was specified for this matcher.' ) ;
155
+
156
+ const makeResolveMatcher = (
157
+ matcherName : string ,
158
+ matcher : RawMatcherFn ,
159
+ isNot : boolean ,
160
+ actual : Promise < any > ,
161
+ outerErr : JestAssertionError ,
162
+ ) : PromiseMatcherFn =>
163
+ function ( ...args ) {
166
164
const options = {
167
165
isNot,
168
166
promise : 'resolves' ,
169
167
} ;
170
168
171
169
if ( ! isPromise ( actual ) ) {
172
170
throw new JestAssertionError (
173
- matcherUtils . matcherErrorMessage (
174
- matcherUtils . matcherHint ( matcherName , undefined , '' , options ) ,
175
- `${ matcherUtils . RECEIVED_COLOR ( 'received' ) } value must be a promise` ,
176
- matcherUtils . printWithType (
171
+ this . utils . matcherErrorMessage (
172
+ this . utils . matcherHint ( matcherName , undefined , '' , options ) ,
173
+ `${ this . utils . RECEIVED_COLOR ( 'received' ) } value must be a promise` ,
174
+ this . utils . printWithType (
177
175
'Received' ,
178
176
actual ,
179
- matcherUtils . printReceived ,
177
+ this . utils . printReceived ,
180
178
) ,
181
179
) ,
182
180
) ;
@@ -187,33 +185,27 @@ const makeResolveMatcher =
187
185
return actual . then (
188
186
result =>
189
187
makeThrowingMatcher ( matcher , isNot , 'resolves' , result , innerErr ) . apply (
190
- null ,
188
+ this ,
191
189
args ,
192
190
) ,
193
191
reason => {
194
192
outerErr . message =
195
- `${ matcherUtils . matcherHint (
196
- matcherName ,
197
- undefined ,
198
- '' ,
199
- options ,
200
- ) } \n\n` +
193
+ `${ this . utils . matcherHint ( matcherName , undefined , '' , options ) } \n\n` +
201
194
'Received promise rejected instead of resolved\n' +
202
- `Rejected to value: ${ matcherUtils . printReceived ( reason ) } ` ;
195
+ `Rejected to value: ${ this . utils . printReceived ( reason ) } ` ;
203
196
return Promise . reject ( outerErr ) ;
204
197
} ,
205
198
) ;
206
199
} ;
207
200
208
- const makeRejectMatcher =
209
- (
210
- matcherName : string ,
211
- matcher : RawMatcherFn ,
212
- isNot : boolean ,
213
- actual : Promise < any > | ( ( ) => Promise < any > ) ,
214
- outerErr : JestAssertionError ,
215
- ) : PromiseMatcherFn =>
216
- ( ...args ) => {
201
+ const makeRejectMatcher = (
202
+ matcherName : string ,
203
+ matcher : RawMatcherFn ,
204
+ isNot : boolean ,
205
+ actual : Promise < any > | ( ( ) => Promise < any > ) ,
206
+ outerErr : JestAssertionError ,
207
+ ) : PromiseMatcherFn =>
208
+ function ( ...args ) {
217
209
const options = {
218
210
isNot,
219
211
promise : 'rejects' ,
@@ -224,15 +216,15 @@ const makeRejectMatcher =
224
216
225
217
if ( ! isPromise ( actualWrapper ) ) {
226
218
throw new JestAssertionError (
227
- matcherUtils . matcherErrorMessage (
228
- matcherUtils . matcherHint ( matcherName , undefined , '' , options ) ,
229
- `${ matcherUtils . RECEIVED_COLOR (
219
+ this . utils . matcherErrorMessage (
220
+ this . utils . matcherHint ( matcherName , undefined , '' , options ) ,
221
+ `${ this . utils . RECEIVED_COLOR (
230
222
'received' ,
231
223
) } value must be a promise or a function returning a promise`,
232
- matcherUtils . printWithType (
224
+ this . utils . printWithType (
233
225
'Received' ,
234
226
actual ,
235
- matcherUtils . printReceived ,
227
+ this . utils . printReceived ,
236
228
) ,
237
229
) ,
238
230
) ;
@@ -243,24 +235,25 @@ const makeRejectMatcher =
243
235
return actualWrapper . then (
244
236
result => {
245
237
outerErr . message =
246
- `${ matcherUtils . matcherHint (
247
- matcherName ,
248
- undefined ,
249
- '' ,
250
- options ,
251
- ) } \n\n` +
238
+ `${ this . utils . matcherHint ( matcherName , undefined , '' , options ) } \n\n` +
252
239
'Received promise resolved instead of rejected\n' +
253
- `Resolved to value: ${ matcherUtils . printReceived ( result ) } ` ;
240
+ `Resolved to value: ${ this . utils . printReceived ( result ) } ` ;
254
241
return Promise . reject ( outerErr ) ;
255
242
} ,
256
243
reason =>
257
244
makeThrowingMatcher ( matcher , isNot , 'rejects' , reason , innerErr ) . apply (
258
- null ,
245
+ this ,
259
246
args ,
260
247
) ,
261
248
) ;
262
249
} ;
263
250
251
+ const utils : MatcherUtils [ 'utils' ] = Object . freeze ( {
252
+ ...matcherUtils ,
253
+ iterableEquality,
254
+ subsetEquality,
255
+ } ) ;
256
+
264
257
const makeThrowingMatcher = (
265
258
matcher : RawMatcherFn ,
266
259
isNot : boolean ,
@@ -270,11 +263,6 @@ const makeThrowingMatcher = (
270
263
) : ThrowingMatcherFn =>
271
264
function throwingMatcher ( ...args ) : any {
272
265
let throws = true ;
273
- const utils : MatcherUtils [ 'utils' ] = {
274
- ...matcherUtils ,
275
- iterableEquality,
276
- subsetEquality,
277
- } ;
278
266
279
267
const matcherUtilsThing : MatcherUtils = {
280
268
// When throws is disabled, the matcher will not throw errors during test
@@ -299,7 +287,7 @@ const makeThrowingMatcher = (
299
287
result : SyncExpectationResult ,
300
288
asyncError ?: JestAssertionError ,
301
289
) => {
302
- _validateResult ( result ) ;
290
+ _validateResult ( this . utils . stringify , result ) ;
303
291
304
292
getState ( ) . assertionCalls ++ ;
305
293
@@ -400,7 +388,10 @@ expect.objectContaining = objectContaining;
400
388
expect . stringContaining = stringContaining ;
401
389
expect . stringMatching = stringMatching ;
402
390
403
- const _validateResult = ( result : any ) => {
391
+ const _validateResult = (
392
+ stringify : typeof matcherUtils [ 'stringify' ] ,
393
+ result : any ,
394
+ ) => {
404
395
if (
405
396
typeof result !== 'object' ||
406
397
typeof result . pass !== 'boolean' ||
@@ -413,7 +404,7 @@ const _validateResult = (result: any) => {
413
404
'Matcher functions should ' +
414
405
'return an object in the following format:\n' +
415
406
' {message?: string | function, pass: boolean}\n' +
416
- `'${ matcherUtils . stringify ( result ) } ' was returned` ,
407
+ `'${ stringify ( result ) } ' was returned` ,
417
408
) ;
418
409
}
419
410
} ;
0 commit comments