@@ -211,68 +211,73 @@ function expectedException(actual, expected) {
211
211
return expected . call ( { } , actual ) === true ;
212
212
}
213
213
214
- function tryBlock ( block ) {
214
+ function getActual ( block ) {
215
+ if ( typeof block !== 'function' ) {
216
+ throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , 'block' , 'Function' ,
217
+ block ) ;
218
+ }
215
219
try {
216
220
block ( ) ;
217
221
} catch ( e ) {
218
222
return e ;
219
223
}
220
224
}
221
225
222
- function innerThrows ( shouldThrow , block , expected , message ) {
223
- var details = '' ;
226
+ // Expected to throw an error.
227
+ assert . throws = function throws ( block , error , message ) {
228
+ const actual = getActual ( block ) ;
224
229
225
- if ( typeof block !== 'function' ) {
226
- throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , 'block' , 'Function' ,
227
- block ) ;
228
- }
230
+ if ( typeof error === 'string' ) {
231
+ if ( arguments . length === 3 )
232
+ throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' ,
233
+ 'error' ,
234
+ [ 'Function' , 'RegExp' ] ,
235
+ error ) ;
229
236
230
- if ( typeof expected === 'string' ) {
231
- message = expected ;
232
- expected = null ;
237
+ message = error ;
238
+ error = null ;
233
239
}
234
240
235
- const actual = tryBlock ( block ) ;
236
-
237
- if ( shouldThrow === true ) {
238
- if ( actual === undefined ) {
239
- if ( expected && expected . name ) {
240
- details += ` (${ expected . name } )` ;
241
- }
242
- details += message ? `: ${ message } ` : '.' ;
243
- innerFail ( {
244
- actual,
245
- expected,
246
- operator : 'throws' ,
247
- message : `Missing expected exception${ details } ` ,
248
- stackStartFn : assert . throws
249
- } ) ;
250
- }
251
- if ( expected && expectedException ( actual , expected ) === false ) {
252
- throw actual ;
253
- }
254
- } else if ( actual !== undefined ) {
255
- if ( ! expected || expectedException ( actual , expected ) ) {
256
- details = message ? `: ${ message } ` : '.' ;
257
- innerFail ( {
258
- actual,
259
- expected,
260
- operator : 'doesNotThrow' ,
261
- message : `Got unwanted exception${ details } \n${ actual . message } ` ,
262
- stackStartFn : assert . doesNotThrow
263
- } ) ;
241
+ if ( actual === undefined ) {
242
+ let details = '' ;
243
+ if ( error && error . name ) {
244
+ details += ` (${ error . name } )` ;
264
245
}
246
+ details += message ? `: ${ message } ` : '.' ;
247
+ innerFail ( {
248
+ actual,
249
+ expected : error ,
250
+ operator : 'throws' ,
251
+ message : `Missing expected exception${ details } ` ,
252
+ stackStartFn : throws
253
+ } ) ;
254
+ }
255
+ if ( error && expectedException ( actual , error ) === false ) {
265
256
throw actual ;
266
257
}
267
- }
268
-
269
- // Expected to throw an error.
270
- assert . throws = function throws ( block , error , message ) {
271
- innerThrows ( true , block , error , message ) ;
272
258
} ;
273
259
274
260
assert . doesNotThrow = function doesNotThrow ( block , error , message ) {
275
- innerThrows ( false , block , error , message ) ;
261
+ const actual = getActual ( block ) ;
262
+ if ( actual === undefined )
263
+ return ;
264
+
265
+ if ( typeof error === 'string' ) {
266
+ message = error ;
267
+ error = null ;
268
+ }
269
+
270
+ if ( ! error || expectedException ( actual , error ) ) {
271
+ const details = message ? `: ${ message } ` : '.' ;
272
+ innerFail ( {
273
+ actual,
274
+ expected : error ,
275
+ operator : 'doesNotThrow' ,
276
+ message : `Got unwanted exception${ details } \n${ actual . message } ` ,
277
+ stackStartFn : doesNotThrow
278
+ } ) ;
279
+ }
280
+ throw actual ;
276
281
} ;
277
282
278
283
assert . ifError = function ifError ( err ) { if ( err ) throw err ; } ;
0 commit comments