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 { ErrorWithStack , isPromise } from 'jest-util' ;
@@ -155,33 +154,32 @@ export const expect: Expect = (actual: any, ...rest: Array<any>) => {
155
154
return expectation ;
156
155
} ;
157
156
158
- const getMessage = ( message ?: ( ) => string ) =>
157
+ const getMessage = ( receivedColor : ChalkFunction , message ?: ( ) => string ) =>
159
158
( message && message ( ) ) ||
160
- matcherUtils . RECEIVED_COLOR ( 'No message was specified for this matcher.' ) ;
161
-
162
- const makeResolveMatcher =
163
- (
164
- matcherName : string ,
165
- matcher : RawMatcherFn ,
166
- isNot : boolean ,
167
- actual : Promise < any > ,
168
- outerErr : JestAssertionError ,
169
- ) : PromiseMatcherFn =>
170
- ( ...args ) => {
159
+ receivedColor ( 'No message was specified for this matcher.' ) ;
160
+
161
+ const makeResolveMatcher = (
162
+ matcherName : string ,
163
+ matcher : RawMatcherFn ,
164
+ isNot : boolean ,
165
+ actual : Promise < any > ,
166
+ outerErr : JestAssertionError ,
167
+ ) : PromiseMatcherFn =>
168
+ function ( ...args ) {
171
169
const options = {
172
170
isNot,
173
171
promise : 'resolves' ,
174
172
} ;
175
173
176
174
if ( ! isPromise ( actual ) ) {
177
175
throw new JestAssertionError (
178
- matcherUtils . matcherErrorMessage (
179
- matcherUtils . matcherHint ( matcherName , undefined , '' , options ) ,
180
- `${ matcherUtils . RECEIVED_COLOR ( 'received' ) } value must be a promise` ,
181
- matcherUtils . printWithType (
176
+ this . utils . matcherErrorMessage (
177
+ this . utils . matcherHint ( matcherName , undefined , '' , options ) ,
178
+ `${ this . utils . RECEIVED_COLOR ( 'received' ) } value must be a promise` ,
179
+ this . utils . printWithType (
182
180
'Received' ,
183
181
actual ,
184
- matcherUtils . printReceived ,
182
+ this . utils . printReceived ,
185
183
) ,
186
184
) ,
187
185
) ;
@@ -192,33 +190,27 @@ const makeResolveMatcher =
192
190
return actual . then (
193
191
result =>
194
192
makeThrowingMatcher ( matcher , isNot , 'resolves' , result , innerErr ) . apply (
195
- null ,
193
+ this ,
196
194
args ,
197
195
) ,
198
196
error => {
199
197
outerErr . message =
200
- `${ matcherUtils . matcherHint (
201
- matcherName ,
202
- undefined ,
203
- '' ,
204
- options ,
205
- ) } \n\n` +
198
+ `${ this . utils . matcherHint ( matcherName , undefined , '' , options ) } \n\n` +
206
199
'Received promise rejected instead of resolved\n' +
207
- `Rejected to value: ${ matcherUtils . printReceived ( error ) } ` ;
200
+ `Rejected to value: ${ this . utils . printReceived ( error ) } ` ;
208
201
throw outerErr ;
209
202
} ,
210
203
) ;
211
204
} ;
212
205
213
- const makeRejectMatcher =
214
- (
215
- matcherName : string ,
216
- matcher : RawMatcherFn ,
217
- isNot : boolean ,
218
- actual : Promise < any > | ( ( ) => Promise < any > ) ,
219
- outerErr : JestAssertionError ,
220
- ) : PromiseMatcherFn =>
221
- ( ...args ) => {
206
+ const makeRejectMatcher = (
207
+ matcherName : string ,
208
+ matcher : RawMatcherFn ,
209
+ isNot : boolean ,
210
+ actual : Promise < any > | ( ( ) => Promise < any > ) ,
211
+ outerErr : JestAssertionError ,
212
+ ) : PromiseMatcherFn =>
213
+ function ( ...args ) {
222
214
const options = {
223
215
isNot,
224
216
promise : 'rejects' ,
@@ -229,15 +221,15 @@ const makeRejectMatcher =
229
221
230
222
if ( ! isPromise ( actualWrapper ) ) {
231
223
throw new JestAssertionError (
232
- matcherUtils . matcherErrorMessage (
233
- matcherUtils . matcherHint ( matcherName , undefined , '' , options ) ,
234
- `${ matcherUtils . RECEIVED_COLOR (
224
+ this . utils . matcherErrorMessage (
225
+ this . utils . matcherHint ( matcherName , undefined , '' , options ) ,
226
+ `${ this . utils . RECEIVED_COLOR (
235
227
'received' ,
236
228
) } value must be a promise or a function returning a promise`,
237
- matcherUtils . printWithType (
229
+ this . utils . printWithType (
238
230
'Received' ,
239
231
actual ,
240
- matcherUtils . printReceived ,
232
+ this . utils . printReceived ,
241
233
) ,
242
234
) ,
243
235
) ;
@@ -248,24 +240,25 @@ const makeRejectMatcher =
248
240
return actualWrapper . then (
249
241
result => {
250
242
outerErr . message =
251
- `${ matcherUtils . matcherHint (
252
- matcherName ,
253
- undefined ,
254
- '' ,
255
- options ,
256
- ) } \n\n` +
243
+ `${ this . utils . matcherHint ( matcherName , undefined , '' , options ) } \n\n` +
257
244
'Received promise resolved instead of rejected\n' +
258
- `Resolved to value: ${ matcherUtils . printReceived ( result ) } ` ;
245
+ `Resolved to value: ${ this . utils . printReceived ( result ) } ` ;
259
246
throw outerErr ;
260
247
} ,
261
248
error =>
262
249
makeThrowingMatcher ( matcher , isNot , 'rejects' , error , innerErr ) . apply (
263
- null ,
250
+ this ,
264
251
args ,
265
252
) ,
266
253
) ;
267
254
} ;
268
255
256
+ const utils : MatcherUtils [ 'utils' ] = Object . freeze ( {
257
+ ...matcherUtils ,
258
+ iterableEquality,
259
+ subsetEquality,
260
+ } ) ;
261
+
269
262
const makeThrowingMatcher = (
270
263
matcher : RawMatcherFn ,
271
264
isNot : boolean ,
@@ -275,11 +268,6 @@ const makeThrowingMatcher = (
275
268
) : ThrowingMatcherFn =>
276
269
function throwingMatcher ( ...args ) : any {
277
270
let throws = true ;
278
- const utils : MatcherUtils [ 'utils' ] = {
279
- ...matcherUtils ,
280
- iterableEquality,
281
- subsetEquality,
282
- } ;
283
271
284
272
const matcherUtilsThing : MatcherUtils = {
285
273
customTesters : getCustomEqualityTesters ( ) ,
@@ -305,7 +293,7 @@ const makeThrowingMatcher = (
305
293
result : SyncExpectationResult ,
306
294
asyncError ?: JestAssertionError ,
307
295
) => {
308
- _validateResult ( result ) ;
296
+ _validateResult ( this . utils . stringify , result ) ;
309
297
310
298
getState ( ) . assertionCalls ++ ;
311
299
@@ -413,7 +401,10 @@ expect.objectContaining = objectContaining;
413
401
expect . stringContaining = stringContaining ;
414
402
expect . stringMatching = stringMatching ;
415
403
416
- const _validateResult = ( result : any ) => {
404
+ const _validateResult = (
405
+ stringify : ( typeof matcherUtils ) [ 'stringify' ] ,
406
+ result : any ,
407
+ ) => {
417
408
if (
418
409
typeof result !== 'object' ||
419
410
typeof result . pass !== 'boolean' ||
@@ -426,7 +417,7 @@ const _validateResult = (result: any) => {
426
417
'Matcher functions should ' +
427
418
'return an object in the following format:\n' +
428
419
' {message?: string | function, pass: boolean}\n' +
429
- `'${ matcherUtils . stringify ( result ) } ' was returned` ,
420
+ `'${ stringify ( result ) } ' was returned` ,
430
421
) ;
431
422
}
432
423
} ;
0 commit comments