Skip to content

Commit b28f6a5

Browse files
foliveiraFishrock123
authored andcommitted
docs: Clarify assert.doesNotThrow behavior
The documentation for assert.doesNotThrow now reflects all the inputs the function accepts, as well as the errors thrown for each combination of parameter types. PR-URL: #2807 Reviewed-By: Rich Trott <[email protected]>
1 parent 9e9bfa4 commit b28f6a5

File tree

1 file changed

+28
-4
lines changed

1 file changed

+28
-4
lines changed

doc/api/assert.markdown

+28-4
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ access it with `require('assert')`.
77

88
## assert.fail(actual, expected, message, operator)
99

10-
Throws an exception that displays the values for `actual` and `expected` separated by the provided operator.
10+
Throws an exception that displays the values for `actual` and `expected`
11+
separated by the provided operator.
1112

1213
## assert(value[, message]), assert.ok(value[, message])
1314

14-
Tests if value is truthy. It is equivalent to `assert.equal(true, !!value, message)`.
15+
Tests if value is truthy. It is equivalent to
16+
`assert.equal(true, !!value, message)`.
1517

1618
## assert.equal(actual, expected[, message])
1719

@@ -86,9 +88,31 @@ Custom error validation:
8688
"unexpected error"
8789
);
8890

89-
## assert.doesNotThrow(block[, message])
91+
## assert.doesNotThrow(block[, error][, message])
9092

91-
Expects `block` not to throw an error. See `assert.throws()` for details.
93+
Expects `block` not to throw an error. See [assert.throws()](#assert_assert_throws_block_error_message) for more details.
94+
95+
If `block` throws an error and if it is of a different type from `error`, the
96+
thrown error will get propagated back to the caller. The following call will
97+
throw the `TypeError`, since we're not matching the error types in the
98+
assertion.
99+
100+
assert.doesNotThrow(
101+
function() {
102+
throw new TypeError("Wrong value");
103+
},
104+
SyntaxError
105+
);
106+
107+
In case `error` matches with the error thrown by `block`, an `AssertionError`
108+
is thrown instead.
109+
110+
assert.doesNotThrow(
111+
function() {
112+
throw new TypeError("Wrong value");
113+
},
114+
TypeError
115+
);
92116

93117
## assert.ifError(value)
94118

0 commit comments

Comments
 (0)