-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
feat: added chai style assertions #8842
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
feat: added chai style assertions #8842
Conversation
✅ Deploy Preview for vitest-dev ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
docs/api/expect.md
Outdated
| }) | ||
| ``` | ||
|
|
||
| ## Chai-Style Spy Assertions |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a guide, not an API.
For an API we need all methods to be described similar to other to* assertions
The current doc can be moved to guides
docs/guide/migration.md
Outdated
| ``` | ||
|
|
||
| ::: tip | ||
| Vitest supports both `before`/`after` (Mocha-style) and `beforeAll`/`afterAll` (Jest-style) - they're aliases! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Vitest does not support before/after
docs/guide/migration.md
Outdated
|
|
||
| ### Assertions | ||
|
|
||
| Vitest includes Chai assertions by default, so most Chai assertions work without changes: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's remove the "most" part, all of them work the same way
docs/guide/migration.md
Outdated
| ``` | ||
|
|
||
| ::: tip | ||
| Notice the only difference: Chai-style assertions in Vitest use method calls `called()` instead of properties `called`. This is because Vitest implements these as methods that delegate to Jest-style matchers. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we still want them to be defined in the same way that sinon-chai does:
expect(spy).to.have.been.called
expect(spy).to.have.been.calledOnce
expect(spy).to.have.been.calledWith('arg1', 'arg2')There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Refactored to that format
docs/guide/migration.md
Outdated
| vi.useRealTimers() | ||
| ``` | ||
|
|
||
| ### Async Testing |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this can be removed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
|
|
||
| // Chai-style assertion: called | ||
| // Delegates to: toHaveBeenCalled | ||
| def('called', 'toHaveBeenCalled') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this should be a property
| } | ||
| } | ||
|
|
||
| // Chai-style assertion: called |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All of the comments can be removed, they do not provide any value
|
|
||
| // Chai-style assertion: calledTwice | ||
| // Wrapper that calls toHaveBeenCalledTimes(2) | ||
| utils.addMethod( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why can't we use def for this? Maybe enhance it to support a property with specified arguments
| * Chai-style equivalent of `toHaveBeenCalledTimes(2)`. | ||
| * | ||
| * @example | ||
| * expect(spy).to.have.been.calledTwice |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All the examples show it as a property...
packages/expect/src/types.ts
Outdated
| * Chai-style assertions for spy/mock testing. | ||
| * These provide sinon-chai compatible assertion names that delegate to Jest-style implementations. | ||
| */ | ||
| export interface ChaiStyleAssertion { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's name it something like ChaiMockAssertion or something
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:
Property Assertions (no parentheses):
called->toHaveBeenCalledcalledOnce->toHaveBeenCalledOncecalledTwice->toHaveBeenCalledTimes(2)calledThrice->toHaveBeenCalledTimes(3)returned->toHaveReturnedMethod Assertions (with parentheses):
callCount(n)->toHaveBeenCalledTimes(n)calledWith(...args)->toHaveBeenCalledWith(...args)calledOnceWith(...args)->toHaveBeenCalledExactlyOnceWith(...args)lastCalledWith(...args)->toHaveBeenLastCalledWith(...args)nthCalledWith(n, ...args)->toHaveBeenNthCalledWith(n, ...args)returnedWith(value)->toHaveReturnedWith(value)returnedTimes(n)->toHaveReturnedTimes(n)lastReturnedWith(value)->toHaveLastReturnedWith(value)nthReturnedWith(n, value)->toHaveNthReturnedWith(n, value)calledBefore(spy)->toHaveBeenCalledBefore(spy)calledAfter(spy)->toHaveBeenCalledAfter(spy)Tests
pnpm test:ci.Documentation