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.

Total assertion count expectations are inaccurate (with async expects)

See original GitHub issue

šŸ› Bug Report

expect.hasAssertions() and expect.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

test('hasAssertions should fail expects in promises', () => {
  expect.hasAssertions();
  // Note that the expect below does not "count" as a failed assertion for this
  // test, but hasAssertions() also does not fail as it should!
  Promise.resolve().then(() => expect(true).toBe(false));
});

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

āÆ npx envinfo --preset jest

  System:
    OS: macOS High Sierra 10.13.6
    CPU: (4) x64 Intel(R) Core(TM) i5-5257U CPU @ 2.70GHz
  Binaries:
    Node: 8.11.1 - ~/.nvm/versions/node/v8.11.1/bin/node
    Yarn: 1.15.0 - /usr/local/bin/yarn
    npm: 5.6.0 - ~/.nvm/versions/node/v8.11.1/bin/npm

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.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:9
  • Comments:9

github_iconTop GitHub Comments

2reactions
ballercatcommented, Apr 11, 2019

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!

1reaction
ballercatcommented, Feb 25, 2022

@SimenB Thanks for the response and I agree 100% RE: the problem of global expect not being bound to a single test.

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 test(string, fn) API with something like test.strict that would directly provide the expect Fn as a variable instead of relying on the global. Similar ideas have worked for other test runners (ava comes to mind).

Ex:

test.strict('this test will fail assertions post test completion', (expect) => {
   setTimeout(() => expect(true).toBe(true), 10000);
});
Read more comments on GitHub >

github_iconTop Results From Across the Web

Demystifying Jest Async Testing Patterns | by Liran Tal - Medium
Jest will then fail the test as it missed the expected assertions count. Explicit Expectations. I find assertions count somewhat non-elegant. Fortunately,Ā ...
Read more >
Expect - Jest
Async matchers return a Promise so you will need to await the returned value. Let's use an example matcher to illustrate the usage...
Read more >
API violation - multiple calls made to -[XCTestExpectation fulfill]
I got the same error when set expectation.expectedFulfillmentCount . Solution: expectation.assertForOverFulfill = false. If expectation.
Read more >
How to Test Asynchronous Code with Jest | Pluralsight
hasAssertions and expect.hasAssertionsNumber(N) in every test by counting how many assertions exist in the source code of the test function bodyĀ ...
Read more >
Jest Testing like a Pro - Tips and tricks
false positives, toThrow and expect.assertion ... you tell jest to count and make sure a certain number of expectations are run and passed....
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