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.

t.throws and t.doesNotThrow should be able to accept a promise

See original GitHub issue

At the moment, t.throws and t.doesNotThrow only accepts a function. It should also be able to accept a promise, like AVA.

I want to be able to test that Koa middleware throws a particular error:

(ctx, next) => {
  t.throws(next(), MyError)
}

It would be great if the function return value and promise resolved value would be returned. Here is an AVA example:

const promise = Promise.reject(new TypeError('🦄'));

test('rejects', async t => {
  const error = await t.throws(promise);
  t.is(error.message, '🦄');
})

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
lorenzofox3commented, Jun 25, 2018

I think you are mislead by console.log. As tests run in parallel, the logging sequence (at least through console.log) is not relevant.

However you should be able to test promise error easily

const throwing = async (message) => {
  throw new Error(message);
}

test('test 1',async t => {
  try {
    await throwing('oh no!');
  } catch (e){
    t.equal(e.message,'oh no!', 'should test the error');
  }
  
  t.ok(true,'shoud run after');
});

or in action

0reactions
mindplay-dkcommented, Feb 18, 2022

⚠ warning: the example above will always pass - even if throwing does not throw!

✔ The correct solution was offered in this thread.

I do think we have a problem with this feature… I fall into this trap myself every time - the issue has been posted twice now, and the track record for someone providing the correct workaround is 1:2. 😉

How about the following solution?

  1. If the callback passed to throws returns a Promise, it should throw an error and abort the test: this will help folks realize that throws is not intended for use with async callbacks - currently this quietly fails, so this will fix that.

  2. Add a another assertion rejected specifically accepting only a Promise, which must be rejected rather than resolved: this is more explicit and makes the distinction more clear.

When failing in throws for passing a Promise, the error message could explain how to correctly switch to rejects, e.g.:

await t.rejected(async () => { .... })

We should tighten the type-hints in TS as well - for throws we can use a param type T extends Promise<unknown> ? never : T, as covered in this article, and for rejected of course this would accept solely a Promise<T>.

(The only remaining pitfall then is people may forget to use await with t.rejected - but that’s just Javascript, I’m afraid.)

What do you think, would you consider reopening this issue?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Jasmine: How to expect promise handler to not throw exception
Let reload return the promise it creates. In your test case, attach a catch handler to it which triggers a test failure:
Read more >
Reject Vs Throw Promises in JavaScript - GeeksforGeeks
reject (): It is an inbuilt function in Javascript that returns a Promise object which has been rejected for a particular given reason....
Read more >
How to Throw Errors From Async Functions in JavaScript?
Async functions and async methods always return a Promise, either resolved or rejected. You must attach then() and catch() , no matter what....
Read more >
Top 15 C++ Exception handling mistakes and how to avoid ...
So here's the bottom-line, don't just dismiss using C++ exceptions ... a function can specify that it does not throw exceptions by providing ......
Read more >
Node.js v19.3.0 Documentation
Lines that don't start with $ or > character show the output of the previous ... import assert from 'node:assert'; // WARNING: This...
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