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.

Async testing fails with async/await

See original GitHub issue

🐛 Bug Report

Asynchronous testing does not support async functions.

To Reproduce

await expect(async () => {
   throw new Error('Test');
}).rejects.toThrow('Test');

Yields:

    expect(received).rejects.toThrow()

    Matcher error: received value must be a promise

    Received has type:  function
    Received has value: [Function anonymous]

      22 |       await expect(async () => {
      23 |          throw new Error('Test');
    > 24 |       }).rejects.toThrow('Test');
         |                  ^
      25 |    });
      26 | 
      27 | 

Expected behavior

The test should treat the function as a promise. At the moment, the fix is to chain the function to a Promise, for example:

await expect( Promise.resolve().then(async () => {
   throw new Error('Test');
})).rejects.toThrow('Test');

The solution is quite easy, instead of expecting only an object, expect should check if the constructor is an AsyncFunction. For example :

if (value.constructor.name === 'Promise` || value.constructor.name === 'AsyncFunction') {
    ...
}

envinfo

  System:
    OS: Linux 5.3 Ubuntu 19.10 (Eoan Ermine)
    CPU: (16) x64 AMD Ryzen 7 PRO 2700 Eight-Core Processor
  Binaries:
    Node: 12.10.0 - ~/.nvm/versions/node/v12.10.0/bin/node
    Yarn: 1.19.1 - ~/.nvm/versions/node/v12.10.0/bin/yarn
    npm: 6.11.3 - ~/.nvm/versions/node/v12.10.0/bin/npm
  npmPackages:
    jest: ^24.9.0 => 24.9.0 

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:3
  • Comments:10 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
Patrick-Remycommented, Apr 20, 2020

Still exists for resolves.

Current behavior

await expect(async () => new Promise(resolve => setTimeout(resolve, 1000))).resolves.toBeUndefined()

// Matcher error: received value must be a promise

Workaround

await expect((async () => new Promise(resolve => setTimeout(resolve, 1000)))()).resolves.toBeUndefined()
2reactions
yacinehmitocommented, Dec 1, 2019

Hello 👋

Your suggestion would indeed make the API more practical for the use case you present, but it would not work consistently. Relying on the name of the function being passed is error-prone. Indeed, it is not necessarily an arrow-function and it could be a named async function.

I also don’t believe it is a bug. The documentation makes no claim to support functions; it knowingly only supports promises.

To work around this, you can simply call the function. This is similar to your suggestion, but you don’t really need the Promise.resolve. The code below will work just as well:

await expect((async () => {
   throw new Error('Test');
})()).rejects.toThrow('Test');

Not only does this work fine, but most of the time it is not even needed. Indeed, people tend to test one function call. Say I already have an async function called myfunction. This is how I would test it:

await expect(myfunction()).rejects.toThrow('Test'));

It doesn’t look too bad.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Testing Asynchronous Code - Jest
Promises​. Return a promise from your test, and Jest will wait for that promise to resolve. If the promise is rejected, the test...
Read more >
Why isn't this unit test catching an error from this async/await ...
In this case, we call the fetchItem() function with an argument that is not a string (which our database query will expect). It's...
Read more >
Testing async/await exceptions - Artur Gruchała
We have to test our new code. The testing happy path is pretty straightforward. We can mark the test method as async ,...
Read more >
Async await test cases failed using jest javascript testing library
I am using Jest testing Library for some simple async/await functions. But it's failing again and again as I am very new to...
Read more >
Asynchronous work - Jasmine Documentation
Failing with async / await​​ async / await functions can indicate failure by either returning a rejected promise or by throwing an error....
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