Skip to content

Conversation

@ronnakamoto
Copy link

@ronnakamoto ronnakamoto commented Oct 27, 2025

Description

This PR adds Chai-style assertion names for spy matchers, enabling users migrating from Mocha+Chai+Sinon to use familiar assertion syntax without rewriting their tests.

Resolves #8819

Users migrating from Mocha/Chai/Sinon to Vitest currently need to rewrite all their spy assertions from Chai-style (e.g., expect(spy).to.have.been.called()) to Jest-style (e.g., expect(spy).toHaveBeenCalled()). This creates friction during migration and requires significant test refactoring.

This implementation provides Chai-style assertion names that delegate to existing Jest-style implementations, allowing both styles to coexist.

Added the following assertions that delegate to existing Jest-style implementations:

Call Assertions

Chai-style Delegates to
called() toHaveBeenCalled()
callCount(n) toHaveBeenCalledTimes(n)
calledWith(...args) toHaveBeenCalledWith(...args)
calledOnce() toHaveBeenCalledOnce()
calledOnceWith(...args) toHaveBeenCalledExactlyOnceWith(...args)
calledTwice() toHaveBeenCalledTimes(2)
calledThrice() toHaveBeenCalledTimes(3)
lastCalledWith(...args) toHaveBeenLastCalledWith(...args)
nthCalledWith(n, ...args) toHaveBeenNthCalledWith(n, ...args)

Return Assertions

Chai-style Delegates to
returned() toHaveReturned()
returnedWith(value) toHaveReturnedWith(value)
returnedTimes(n) toHaveReturnedTimes(n)
lastReturnedWith(value) toHaveLastReturnedWith(value)
nthReturnedWith(n, value) toHaveNthReturnedWith(n, value)

Ordering Assertions

Chai-style Delegates to
calledBefore(spy) toHaveBeenCalledBefore(spy)
calledAfter(spy) toHaveBeenCalledAfter(spy)

Tests

  • Run the tests with pnpm test:ci.

Documentation

  • Updated documentation

@netlify
Copy link

netlify bot commented Oct 27, 2025

Deploy Preview for vitest-dev ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 9f4cf1b
🔍 Latest deploy log https://app.netlify.com/projects/vitest-dev/deploys/68ff9f244d302b000851d09a
😎 Deploy Preview https://deploy-preview-8842--vitest-dev.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Enhance Chai assertions

1 participant