1
1
import { TestCaseRecorder } from '../internal/logging/test_case_recorder.js' ;
2
2
import { JSONWithUndefined } from '../internal/params_utils.js' ;
3
- import { assert , unreachable } from '../util/util.js' ;
3
+ import { assert , ExceptionCheckOptions , unreachable } from '../util/util.js' ;
4
4
5
5
export class SkipTestCase extends Error { }
6
6
export class UnexpectedPassError extends Error { }
@@ -237,16 +237,26 @@ export class Fixture<S extends SubcaseBatchState = SubcaseBatchState> {
237
237
}
238
238
239
239
/** Expect that the provided promise rejects, with the provided exception name. */
240
- shouldReject ( expectedName : string , p : Promise < unknown > , msg ?: string ) : void {
240
+ shouldReject (
241
+ expectedName : string ,
242
+ p : Promise < unknown > ,
243
+ { allowMissingStack = false , message } : ExceptionCheckOptions = { }
244
+ ) : void {
241
245
this . eventualAsyncExpectation ( async niceStack => {
242
- const m = msg ? ': ' + msg : '' ;
246
+ const m = message ? ': ' + message : '' ;
243
247
try {
244
248
await p ;
245
249
niceStack . message = 'DID NOT REJECT' + m ;
246
250
this . rec . expectationFailed ( niceStack ) ;
247
251
} catch ( ex ) {
248
- niceStack . message = 'rejected as expected' + m ;
249
252
this . expectErrorValue ( expectedName , ex , niceStack ) ;
253
+ if ( ! allowMissingStack ) {
254
+ if ( ! ( ex instanceof Error && typeof ex . stack === 'string' ) ) {
255
+ const exMessage = ex instanceof Error ? ex . message : '?' ;
256
+ niceStack . message = `rejected as expected, but missing stack (${ exMessage } )${ m } ` ;
257
+ this . rec . expectationFailed ( niceStack ) ;
258
+ }
259
+ }
250
260
}
251
261
} ) ;
252
262
}
@@ -257,8 +267,12 @@ export class Fixture<S extends SubcaseBatchState = SubcaseBatchState> {
257
267
*
258
268
* MAINTENANCE_TODO: Change to `string | false` so the exception name is always checked.
259
269
*/
260
- shouldThrow ( expectedError : string | boolean , fn : ( ) => void , msg ?: string ) : void {
261
- const m = msg ? ': ' + msg : '' ;
270
+ shouldThrow (
271
+ expectedError : string | boolean ,
272
+ fn : ( ) => void ,
273
+ { allowMissingStack = false , message } : ExceptionCheckOptions = { }
274
+ ) {
275
+ const m = message ? ': ' + message : '' ;
262
276
try {
263
277
fn ( ) ;
264
278
if ( expectedError === false ) {
@@ -271,6 +285,11 @@ export class Fixture<S extends SubcaseBatchState = SubcaseBatchState> {
271
285
this . rec . expectationFailed ( new Error ( 'threw unexpectedly' + m ) ) ;
272
286
} else {
273
287
this . expectErrorValue ( expectedError , ex , new Error ( m ) ) ;
288
+ if ( ! allowMissingStack ) {
289
+ if ( ! ( ex instanceof Error && typeof ex . stack === 'string' ) ) {
290
+ this . rec . expectationFailed ( new Error ( 'threw as expected, but missing stack' + m ) ) ;
291
+ }
292
+ }
274
293
}
275
294
}
276
295
}
0 commit comments