-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
Total assertion count expectations are inaccurate (with async expects) #8297
Comments
This also has a negative effect on consecutive tests which use things like Example: test.only('assertions after done() callback - 1', done => {
setTimeout(() => {
done();
setTimeout(() => {
expect(1 + 2).toBe(2);
});
});
});
test.only('assertions after done() callback - 2', done => {
expect.hasAssertions();
setTimeout(() => {
done();
});
}); This test setup leads to these results after tests complete: FAIL src/__tests__/index.js
✓ assertions after done() callback - 1 (31ms)
✕ assertions after done() callback - 2 (6ms)
● assertions after done() callback - 2
expect(received).toBe(expected) // Object.is equality
Expected: 2
Received: 3
28 | done();
29 | setTimeout(() => {
> 30 | expect(1 + 2).toBe(2);
| ^
31 | });
32 | });
33 | });
at Timeout.toBe [as _onTimeout] (src/__tests__/index.js:30:21) Note that the second spec did not fail due to missing assertions, but instead due to a broken assertion enqueued from spec #1. I get what's going on and I get that it's due to the global nature of the |
Can reproduce this. IIRC at least for |
- Fail on missed assertions - Workaround jestjs/jest#8297 with zone.js in the plugin
Ooh, the zone.js library is very cool. How I stumbled upon this is trying to write a "plugin" for jest which would catch these type of runtime issues. I have incorporated zone.js library into it as a PoC, and it seems to be working rather well. I'm going to throw this at a few thousand tests I have available and report back. Thanks! |
I have simillar issue. My test: test('should work', () => {
expect.assertions(1);
return getCoinInfo()
.then((coinInfo) => {
expect(coinInfo.id).toBeGreaterThan(0);
})
.catch((error) => {
logError(error);
});
}); It should fail but it doesn't |
Running into this as well. |
I've just stumbled across this issue due to my own misunderstanding of the behaviour of AFAICT, jest signals expectation failure by throwing, so when I run the initial example in my own code base, it fails, but the following test passes:
(ack. it's highly likely things have changed since the issue was raised) I'm unclear on why this works at all without returning or awaiting the promise 😄
https://jestjs.io/docs/tutorial-async (emphasis mine) |
Essentially same underlying issue here the solution I suggest in #9881 (comment) - |
@SimenB Thanks for the response and I agree 100% RE: the problem of global It's probably a bit too late in the lifecycle of Jest to expand/change it's API in such a way but I for one would love to see an addition to the Ex: test.strict('this test will fail assertions post test completion', (expect) => {
setTimeout(() => expect(true).toBe(true), 10000);
}); |
Yeah, I wanna add that type of API. Unfortunate we used the FWIW I'd me 100% open to a PR adding support for |
FYI: A fix for this is to use
|
You are correct in that there are ways to write good tests which do not leak assertions. The examples provided in this issue are intentionally stripped down to reproducible example of the behavior. They are not intended as an example of a "good" test that still breaks. As a side-note, the example above is perfectly valid with just |
Unfortunately @EllaMolnar-FOX 's fix won't work for me because I'm using TypeScript for my tests. I can't use both
|
This issue is stale because it has been open for 1 year with no activity. Remove stale label or comment or this will be closed in 30 days. |
This issue was closed because it has been stalled for 30 days with no activity. Please open a new issue if the issue is still relevant, linking to this one. |
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. |
🐛 Bug Report
expect.hasAssertions()
andexpect.assertions()
incorrectly count assertions as part of a test when they should not be.To Reproduce
The most basic example I could create to demonstrate
Expected behavior
The test above should fail, the async assertion should be ignored towards the assertion count. The promise chain, where the expect is executing is not part of the test. We can tell that because the test is not marked as failing even with a blatantly broken assertion. When the expect runs the test is "complete" and should not be counted as part of
assertionCount
in Jest.Link to repl or repo (highly encouraged)
I forked the repo and wrote an integration test to demonstrate the problem. Branch
Permalink to the spec itself
https://repl.it/repls/DramaticCuteAssignments
Run
npx envinfo --preset jest
Notes
I do realize that the test is bad, it's not very well written. But tests like these do happen IRL and assertion count matchers is what you would want to use to catch them. Currently they do not work for this scenario.
This was pretty interesting and I dug into it a bunch, trying to see if there is an easy fix. But I was unable to locate an obvious mistake. It seems like the
assertionCount
logic (expect
) is separate from the logic marking tests as failures (jest-jasmine2
?) so there isn't a simple way to marry the two.The text was updated successfully, but these errors were encountered: