@@ -395,6 +395,9 @@ is little benefit by catching a rejection and then rejecting it again. Instead,
395
395
consider adding a comment next to the specific code path that should not reject
396
396
and keep error messages as expressive as possible.
397
397
398
+ If specified, ` error ` can be a [ ` Class ` ] [ ] , [ ` RegExp ` ] [ ] or a validation
399
+ function. See [ ` assert.throws() ` ] [ ] for more details.
400
+
398
401
Besides the async nature to await the completion behaves identically to
399
402
[ ` assert.doesNotThrow() ` ] [ ] .
400
403
@@ -431,8 +434,7 @@ changes:
431
434
* ` error ` {RegExp|Function}
432
435
* ` message ` {any}
433
436
434
- Asserts that the function ` block ` does not throw an error. See
435
- [ ` assert.throws() ` ] [ ] for more details.
437
+ Asserts that the function ` block ` does not throw an error.
436
438
437
439
Please note: Using ` assert.doesNotThrow() ` is actually not useful because there
438
440
is no benefit by catching an error and then rethrowing it. Instead, consider
@@ -447,6 +449,9 @@ parameter, then an `AssertionError` is thrown. If the error is of a different
447
449
type, or if the ` error ` parameter is undefined, the error is propagated back
448
450
to the caller.
449
451
452
+ If specified, ` error ` can be a [ ` Class ` ] [ ] , [ ` RegExp ` ] [ ] or a validation
453
+ function. See [ ` assert.throws() ` ] [ ] for more details.
454
+
450
455
The following, for instance, will throw the [ ` TypeError ` ] [ ] because there is no
451
456
matching error type in the assertion:
452
457
@@ -483,7 +488,7 @@ assert.doesNotThrow(
483
488
() => {
484
489
throw new TypeError (' Wrong value' );
485
490
},
486
- TypeError ,
491
+ / Wrong value / ,
487
492
' Whoops'
488
493
);
489
494
// Throws: AssertionError: Got unwanted exception (TypeError). Whoops
@@ -916,7 +921,7 @@ assert(0);
916
921
added: REPLACEME
917
922
-->
918
923
* ` block ` {Function|Promise}
919
- * ` error ` {RegExp|Function|Object}
924
+ * ` error ` {RegExp|Function|Object|Error }
920
925
* ` message ` {any}
921
926
922
927
Awaits the ` block ` promise or, if ` block ` is a function, immediately calls the
@@ -930,8 +935,10 @@ checking the error handler.
930
935
Besides the async nature to await the completion behaves identically to
931
936
[ ` assert.throws() ` ] [ ] .
932
937
933
- If specified, ` error ` can be a constructor, [ ` RegExp ` ] [ ] , a validation
934
- function, or an object where each property will be tested for.
938
+ If specified, ` error ` can be a [ ` Class ` ] [ ] , [ ` RegExp ` ] [ ] , a validation function,
939
+ an object where each property will be tested for, or an instance of error where
940
+ each property will be tested for including the non-enumerable ` message ` and
941
+ ` name ` properties.
935
942
936
943
If specified, ` message ` will be the message provided by the ` AssertionError ` if
937
944
the block fails to reject.
@@ -1011,13 +1018,15 @@ changes:
1011
1018
description: The `error` parameter can now be an arrow function.
1012
1019
-->
1013
1020
* ` block ` {Function}
1014
- * ` error ` {RegExp|Function|Object}
1021
+ * ` error ` {RegExp|Function|Object|Error }
1015
1022
* ` message ` {any}
1016
1023
1017
1024
Expects the function ` block ` to throw an error.
1018
1025
1019
- If specified, ` error ` can be a constructor, [ ` RegExp ` ] [ ] , a validation
1020
- function, or an object where each property will be tested for.
1026
+ If specified, ` error ` can be a [ ` Class ` ] [ ] , [ ` RegExp ` ] [ ] , a validation function,
1027
+ an object where each property will be tested for, or an instance of error where
1028
+ each property will be tested for including the non-enumerable ` message ` and
1029
+ ` name ` properties.
1021
1030
1022
1031
If specified, ` message ` will be the message provided by the ` AssertionError ` if
1023
1032
the block fails to throw.
@@ -1066,10 +1075,11 @@ assert.throws(
1066
1075
Custom error object / error instance:
1067
1076
1068
1077
``` js
1078
+ const err = new TypeError (' Wrong value' );
1079
+ err .code = 404 ;
1080
+
1069
1081
assert .throws (
1070
1082
() => {
1071
- const err = new TypeError (' Wrong value' );
1072
- err .code = 404 ;
1073
1083
throw err;
1074
1084
},
1075
1085
{
@@ -1078,6 +1088,16 @@ assert.throws(
1078
1088
// Note that only properties on the error object will be tested!
1079
1089
}
1080
1090
);
1091
+
1092
+ // Fails due to the different `message` and `name` properties:
1093
+ assert .throws (
1094
+ () => {
1095
+ const otherErr = new Error (' Not found' );
1096
+ otherErr .code = 404 ;
1097
+ throw otherErr;
1098
+ },
1099
+ err // This tests for `message`, `name` and `code`.
1100
+ );
1081
1101
```
1082
1102
1083
1103
Note that ` error ` cannot be a string. If a string is provided as the second
@@ -1118,6 +1138,7 @@ assert.throws(throwingFirst, /Second$/);
1118
1138
Due to the confusing notation, it is recommended not to use a string as the
1119
1139
second argument. This might lead to difficult-to-spot errors.
1120
1140
1141
+ [ `Class` ] : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes
1121
1142
[ `Error.captureStackTrace` ] : errors.html#errors_error_capturestacktrace_targetobject_constructoropt
1122
1143
[ `Error` ] : errors.html#errors_class_error
1123
1144
[ `Map` ] : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map
0 commit comments