Skip to content

Commit 94220ba

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 ce0b1ad commit 94220ba

File tree

1 file changed

+40
-1
lines changed

1 file changed

+40
-1
lines changed

Diff for: readme.markdown

+40-1
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,46 @@ Aliases: `t.notLooseEqual()`, `t.notLooseEquals()`
286286

287287
## t.throws(fn, expected, msg)
288288

289-
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. 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 an example). 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.
289+
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.
290+
```
291+
const err = new TypeError("Wrong value");
292+
err.code = 404;
293+
err.check = true;
294+
295+
// Passing test.
296+
t.throws(
297+
() => {
298+
throw err;
299+
},
300+
{
301+
code: 404,
302+
check: true
303+
},
304+
"Test message."
305+
);
306+
```
307+
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.
308+
```
309+
const err = new TypeError("Wrong value");
310+
err.code = 404;
311+
err.check = "true";
312+
313+
// Failing test.
314+
t.throws(
315+
() => {
316+
throw err;
317+
},
318+
{
319+
code: 404,
320+
check: true // This is not deeply strictly equal to err.check.
321+
},
322+
"Test message."
323+
);
324+
```
325+
326+
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).
327+
328+
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.
290329

291330
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.
292331

0 commit comments

Comments
 (0)