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.

Unhandled rejection error on "t.throws()" test of trivial async function

See original GitHub issue

Description

Unhandled rejection error on test of async function that throws

Test Source

import test from 'ava';

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

test(async t => {

    const error = await t.throws(async () => {
        return await f();
    });

    t.is(error.message, '');
});

Error Message & Stack Trace

1 failed [08:28:15]
  1 rejection

  test › [anonymous]
  /Users/scotty/workspace/fetch-nice/test/test.js:9

   8:
   9:     const error = await t.throws(async () => {
   10:         return await f();

  Test.<anonymous> (test/test.js:9:27)
  step (test/test.js:9:191)
  Test.<anonymous> (test/test.js:9:99)
  Test.__dirname [as fn] (test/test.js:7:1)

  Unhandled Rejection
  Error
    test/test.js:4:11
    Generator.next (<anonymous>)
    step (test/test.js:9:191)
    test/test.js:9:437
    test/test.js:9:99
    f (test/test.js:3:7)
    test/test.js:10:22
    Generator.next (<anonymous>)
    step (test/test.js:9:191)
    test/test.js:9:437
    test/test.js:9:99
    _tryBlock (node_modules/core-assert/index.js:311:5)
    _throws (node_modules/core-assert/index.js:330:12)
    Function.assert.throws (node_modules/core-assert/index.js:360:3)
    Test.<anonymous> (test/test.js:9:27)
    Generator.next (<anonymous>)
    step (test/test.js:9:191)
    test/test.js:9:437
    Test.<anonymous> (test/test.js:9:99)
    Test.__dirname [as fn] (test/test.js:7:1)

Config

{
  "ava": {
  "require": [
      "babel-register",
      "babel-polyfill"
    ]
  }
}

Command-Line Arguments

ava -w

Environment

Node.js v7.6.0 darwin 15.6.0 ava 0.19.1 npm 4.1.2

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

6reactions
sindresorhuscommented, Apr 27, 2017

That said I think we should extend t.throws() and t.notThrows() so that if the function returns a promise without throwing, the assertion is applied to that promise instead.

👍

1reaction
novemberborncommented, Feb 12, 2018

This was fixed in #1650.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How can I use ava's t.throws to test the error message of a ...
Try calling the functions and passing whatever is returned to t.throws() . eg. : test('throws works with functions that return promises (this fails)',...
Read more >
JavaScript Promises: All you need to know - Medium
A promise is an object that represents eventual completion or failure of an asynchronous operation and its subsequent result value. Dive deeper ...
Read more >
Unit testing with Jest - Naftali Murgor
A quick intro to unit testing Nodejs applications. ... calls with try...catch if using async/await to avoid unhandled promise rejection.
Read more >
How to Interact With a Database Using Promises in Node.js
If an error is thrown and goes unhandled, then the promise will be rejected and the error will be passed to the next...
Read more >
JavaScript: Don't throw exceptions from async functions
When your function returns a promise ("async function"), try not throw synchronous exceptions when encountering fatal errors. So avoid 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