Skip to content

Commit 957fa25

Browse files
nohealukeed
andauthored
chore(docs): add assert.throws example for asyncs (#172)
* 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>
1 parent 4baf4fe commit 957fa25

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

docs/api.assert.md

+13
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,19 @@ assert.throws(() => OOPS(), /Cannot read property/);
131131
assert.throws(() => OOPS(), err => err instanceof TypeError);
132132
```
133133

134+
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):
135+
136+
```js
137+
try {
138+
await asyncFnThatThrows();
139+
assert.unreachable('should have thrown');
140+
} catch (err) {
141+
assert.instance(err, Error);
142+
assert.match(err.message, 'something specific');
143+
assert.is(err.code, 'ERROR123');
144+
}
145+
```
146+
134147
### unreachable(msg?: Message)
135148
Assert that a line should never be reached.
136149

0 commit comments

Comments
 (0)