Skip to content

Commit b36f816

Browse files
cagrossljharb
authored andcommitted
[readme] improve t.throws documentation
* Added usage example for when expected parameter is a validation object. See #540.
1 parent 283f537 commit b36f816

File tree

1 file changed

+42
-1
lines changed

1 file changed

+42
-1
lines changed

readme.markdown

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,48 @@ Aliases: `t.notLooseEqual()`, `t.notLooseEquals()`
270270

271271
## t.throws(fn, expected, msg)
272272

273-
Assert that the function call `fn()` throws an exception. `expected`, if present, must be a `RegExp` or `Function`. The `RegExp` matches the string representation of the exception, as generated by `err.toString()`. The `Function` is the exception thrown (e.g. `Error`). `msg` is an optional description of the assertion.
273+
Assert that the function call `fn()` throws an exception. `expected`, if present, must be a `RegExp`, `Function`, or `Object`. The `RegExp` matches the string representation of the exception, as generated by `err.toString()`. For example, if you set `expected` to `/user/`, the test will pass only if the string representation of the exception contains the word `user`. Any other exception will result in a failed test. The `Function` is the exception thrown (e.g. `Error`). `Object` in this case corresponds to a so-called validation object, in which each property is tested for strict deep equality. As an example, see the following two tests--each passes a validation object to `t.throws()` as the second parameter. The first test will pass, because all property values in the actual error object are deeply strictly equal to the property values in the validation object.
274+
```
275+
const err = new TypeError("Wrong value");
276+
err.code = 404;
277+
err.check = true;
278+
279+
// Passing test.
280+
t.throws(
281+
() => {
282+
throw err;
283+
},
284+
{
285+
code: 404,
286+
check: true
287+
},
288+
"Test message."
289+
);
290+
```
291+
This next test will fail, because all property values in the actual error object are _not_ deeply strictly equal to the property values in the validation object.
292+
```
293+
const err = new TypeError("Wrong value");
294+
err.code = 404;
295+
err.check = "true";
296+
297+
// Failing test.
298+
t.throws(
299+
() => {
300+
throw err;
301+
},
302+
{
303+
code: 404,
304+
check: true // This is not deeply strictly equal to err.check.
305+
},
306+
"Test message."
307+
);
308+
```
309+
310+
This is very similar to how Node's `assert.throws()` method tests validation objects (please see the [Node _assert.throws()_ documentation](https://nodejs.org/api/assert.html#assert_assert_throws_fn_error_message) for more information).
311+
312+
If `expected` is not of type `RegExp`, `Function`, or `Object`, or omitted entirely, any exception will result in a passed test. `msg` is an optional description of the assertion.
313+
314+
Please note that the second parameter, `expected`, cannot be of type `string`. If a value of type `string` is provided for `expected`, then `t.throws(fn, expected, msg)` will execute, but the value of `expected` will be set to `undefined`, and the specified string will be set as the value for the `msg` parameter (regardless of what _actually_ passed as the third parameter). This can cause unexpected results, so please be mindful.
274315

275316
## t.doesNotThrow(fn, expected, msg)
276317

0 commit comments

Comments
 (0)