vitest-dev / vitest-dev/vitest

`expect.hasAssertions()` fails to mark the test as failed when using async function and callback

Open
#4,750 15 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement p2-nice-to-have
Dominant language
TypeScript
Stars
17.1k
Forks
2k
Avg merge
1d 22h
Merged PRs (30d)
94

Description

Describe the bug

You are using expect.hasAssertions() and an async function that accepts a callback.
In that callback you make some assertions. If one of those assertions fails, the test is marked as successful and an error is reported.

I see two possibilities :

  1. The assertions have been run and the test should fail because one of the assertion has failed.
  2. The assertions have not been run yet and the test should fail because of expect.hasAssertions().
Reproduction

https://stackblitz.com/edit/vitest-dev-vitest-a4gsaq?file=test%2Fexpect.hasAssertions-async.test.ts

System Info
System:
    OS: macOS 14.2
    CPU: (12) arm64 Apple M2 Max
    Memory: 19.44 GB / 64.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 18.16.1 - ~/.nvm/versions/node/v18.16.1/bin/node
    Yarn: 1.22.19 - ~/.nvm/versions/node/v18.16.1/bin/yarn
    npm: 9.5.1 - ~/.nvm/versions/node/v18.16.1/bin/npm
    pnpm: 7.29.1 - ~/.nvm/versions/node/v18.16.1/bin/pnpm
    bun: 1.0.14 - ~/.bun/bin/bun
  Browsers:
    Chrome: 120.0.6099.109
    Edge: 120.0.2210.61
    Safari: 17.2
  npmPackages:
    vitest: ^1.0.4 => 1.0.4
Used Package Manager

npm

Validations

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the linked StackBlitz reproduction and confirm how the async callback's assertion failure is reported. Trace Vitest's handling of assertion counts and errors for async tests with callbacks; done means the test is reliably marked failed, either for the assertion failure or for an unmet expect.hasAssertions() check.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
testing
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.