Skip to content

Commit

Permalink
chore(docs): add assert.throws example for asyncs (#172)
Browse files Browse the repository at this point in the history
* Update api.assert.md

Adding a reference on the idiom of how to assert an async function throws an error: 
#35 (comment)

* Apply suggestions from code review

* Update docs/api.assert.md

Co-authored-by: Luke Edwards <luke.edwards05@gmail.com>
  • Loading branch information
nohea and lukeed authored Dec 19, 2021
1 parent 4baf4fe commit 957fa25
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions docs/api.assert.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,19 @@ assert.throws(() => OOPS(), /Cannot read property/);
assert.throws(() => OOPS(), err => err instanceof TypeError);
```

If you are trying to assert that an `async` function throws an Error, the following approach [is recommended](https://github.com/lukeed/uvu/issues/35#issuecomment-896270152):

```js
try {
await asyncFnThatThrows();
assert.unreachable('should have thrown');
} catch (err) {
assert.instance(err, Error);
assert.match(err.message, 'something specific');
assert.is(err.code, 'ERROR123');
}
```

### unreachable(msg?: Message)
Assert that a line should never be reached.

Expand Down

0 comments on commit 957fa25

Please sign in to comment.