question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`assert.throws()` with async argument

See original GitHub issue

Hi 👋,

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:closed
  • Created 3 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
lukeedcommented, Aug 10, 2021

@aral I’m saying that if you only have this:

try {
  await asyncFnThatThrows();
  assert.unreachable('should have thrown');
} catch (err) {
  assert.instance(err, Error);
}

then yes, this could lead to misleading results since the Assertion that uvu/assert throws extends Error, thus satisfying the assert.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:

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');
}

In this case, if/when assert.unreachable does throw, it doesnt match the other conditions. And you still get the benefit of ensuring that asyncFnThatThrows function needs to throw.


I see your PR, thanks! Will review shortly.

1reaction
JosephusPayecommented, Aug 8, 2020

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found