Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: Jest reports a wrong test to have failed due to an exception #11869

Closed
darioackermann opened this issue Sep 13, 2021 · 4 comments
Closed

Comments

@darioackermann
Copy link

darioackermann commented Sep 13, 2021

Version

27.2.0

Steps to reproduce

  1. Clone repo at https://github.com/darioackermann/jest-async-network-wrong-failing-attribution
  2. npm i
  3. Run npm test with network connection: they will all pass
  4. Run npm test without network connection.

Expected behavior

Jest should report that the test with the name should do a request which fails when no network failed, or if not backtraceable, an async test has failed at some point

Actual behavior

Jest reports that should do something else failed

Additional context

No response

Environment

System:
    OS: Windows 10 10.0.19043
    CPU: (12) x64 Intel(R) Core(TM) i7-10750H CPU @ 2.60GHz
  Binaries:
    Node: 14.17.0 - C:\Program Files\nodejs\node.EXE
    npm: 6.14.13 - C:\Program Files\nodejs\npm.CMD
  npmPackages:
    jest: ^27.2.0 => 27.2.0
@SimenB
Copy link
Member

SimenB commented Sep 13, 2021

You need to tell Jest to wait for the async work, otherwise it has no way of knowing which test is running when it fails.

diff --git i/network.test.js w/network.test.js
index 2b0c1a4..908166b 100644
--- i/network.test.js
+++ w/network.test.js
@@ -4,7 +4,7 @@ const sleep = require("sleep-promise");
 const Test =
 {
     doRequest() {
-        axios.get('http://example.com')
+        return axios.get('http://example.com')
     },
 }
 
@@ -12,7 +12,7 @@ describe('a failing test', function () {
 
     it('should do a request which fails when no network', async function () {
         let requestSpy = jest.spyOn(Test, "doRequest")
-        Test.doRequest();
+        await Test.doRequest();
         // passes in any case
         expect(requestSpy).toBeCalledTimes(1)
     });

image

@SimenB SimenB closed this as completed Sep 13, 2021
@darioackermann
Copy link
Author

darioackermann commented Sep 13, 2021

I am afraid this example was not the best to bring up the problem.
What if waiting is not possible because the thing we're waiting for is not directly returned?
I will try to provide a more accurate example of the problem we were experiencing.

@SimenB
Copy link
Member

SimenB commented Sep 13, 2021

You need to tell jest to wait some way or the other from within the test, otherwise the test will complete and your code keep running. You might need to restructure your code if that's not possible

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 14, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants