Skip to content

Commit

Permalink
[readme] improve t.throws documentation
Browse files Browse the repository at this point in the history
* Added usage example for when expected parameter is a validation object.

See #540.
  • Loading branch information
cagross authored and ljharb committed Jan 26, 2021
1 parent ce0b1ad commit 94220ba
Showing 1 changed file with 40 additions and 1 deletion.
41 changes: 40 additions & 1 deletion readme.markdown
Expand Up @@ -286,7 +286,46 @@ Aliases: `t.notLooseEqual()`, `t.notLooseEquals()`

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

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.
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.
```
const err = new TypeError("Wrong value");
err.code = 404;
err.check = true;
// Passing test.
t.throws(
() => {
throw err;
},
{
code: 404,
check: true
},
"Test message."
);
```
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.
```
const err = new TypeError("Wrong value");
err.code = 404;
err.check = "true";
// Failing test.
t.throws(
() => {
throw err;
},
{
code: 404,
check: true // This is not deeply strictly equal to err.check.
},
"Test message."
);
```

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).

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.

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.

Expand Down

0 comments on commit 94220ba

Please sign in to comment.