`assert.throws()` with async argument
See original GitHub issueHi 👋,
Is assert.throws()
currently supposed to work with async functions?
For example, this fails:
assert.throws(async () => {
await asyncFnThatThrows();
});
While this passes:
assert.throws(() => {
syncFnThatThrows();
});
What is the recommended way to test that an async function throws?
Thought of something like this, but there’s no .pass()
or .fail()
:
try {
await asyncFnThatThrows();
test.fail('did not throw');
} catch (err) {
test.pass();
}
Edit: currently using this hack:
async function throwsAsync(
asyncFn,
failMessage = 'did not throw',
passMesage = 'did throw',
) {
try {
await asyncFn();
assert.equal(false, true, failMessage);
} catch (err) {
assert.equal(true, true, passMesage);
}
}
Issue Analytics
- State:
- Created 3 years ago
- Comments:9 (4 by maintainers)
Top Results From Across the Web
Assert.ThrowsAsync - NUnit Docs
The Assert.ThrowsAsync is the async equivalent to Assert.Throws for asynchronous code. See Assert.Throws for more information. Exception Assert.ThrowsAsync(Type ...
Read more >c# - NUnit3: Assert.Throws with async Task - Stack Overflow
Throws appears to take a TestDelegate, defined as: public delegate void TestDelegate();. hence the ArgumentException. What is the best way ...
Read more >Unit Testing: Assert Throw Vs ThrowAsync - TheCodeBuzz
ThrowsAsync (and await the result) when testing async code.' Or. Do not use Assert.Throws() to check for asynchronously thrown exceptions.
Read more >assert.async() - QUnit API
assert.async() returns a callback function and pauses test processing until the callback function is called. The callback will throw an Error if it...
Read more >Throws Async | JustMock Documentation - Telerik
public class Foo { public async Task AsyncExecute() { await Task. ... Like Throws method, ThrowsAsync gives an option to pass arguments to...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@aral I’m saying that if you only have this:
then yes, this could lead to misleading results since the
Assertion
thatuvu/assert
throws extendsError
, thus satisfying theassert.instance
check.I’m saying that this snippet shouldn’t stand on its own in most cases. Instead, the recommendation is to always assert against properties within the
err
that gets thrown. So something more like this:In this case, if/when
assert.unreachable
does throw, it doesnt match the other conditions. And you still get the benefit of ensuring thatasyncFnThatThrows
function needs to throw.I see your PR, thanks! Will review shortly.
Thanks for the consideration. Your findings make sense, and I haven’t seen any issues in practice using the
try/catch
+assert.unreachable
pattern myself.