FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

docs for assert.throws · tmpvar/node@c5c1dc5 · GitHub

Commit c5c1dc5

Browse files
authored andcommitted
docs for assert.throws
1 parent e681abe commit c5c1dc5

1 file changed

Lines changed: 35 additions & 2 deletions

File tree

‎doc/api/assert.markdown‎

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,44 @@ Tests strict non-equality, as determined by the strict not equal operator ( `!==
3737

3838
### assert.throws(block, [error], [message])
3939

40-
Expects `block` to throw an error.
40+
Expects `block` to throw an error. `error` can be constructor, regexp or
41+
validation function.
42+
43+
Validate instanceof using constructor:
44+
45+
assert.throws(
46+
function() {
47+
throw new Error("Wrong value");
48+
},
49+
Error
50+
);
51+
52+
Validate error message using RegExp:
53+
54+
assert.throws(
55+
function() {
56+
throw new Error("Wrong value");
57+
},
58+
/value/
59+
);
60+
61+
Custom error validation:
62+
63+
assert.throws(
64+
function() {
65+
throw new Error("Wrong value");
66+
},
67+
function(err) {
68+
if ( !(err instanceof Error) || !/value/.test(err) ) {
69+
return false;
70+
}
71+
},
72+
"unexpected error"
73+
);
4174

4275
### assert.doesNotThrow(block, [error], [message])
4376

44-
Expects `block` not to throw an error.
77+
Expects `block` not to throw an error, see assert.throws for details.
4578

4679
### assert.ifError(value)
4780

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL