vitest-dev / vitest-dev/vitest
`expect.hasAssertions()` fails to mark the test as failed when using async function and callback
Nobody has claimed this yet.
- 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 :
- The assertions have been run and the test should fail because one of the assertion has failed.
- 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
- Follow our Code of Conduct
- Read the Contributing Guidelines.
- Read the docs.
- Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
- Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.
- The provided reproduction is a minimal reproducible example of the bug.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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