Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions docs/guide/migration.md
Original file line number Diff line number Diff line change
Expand Up @@ -742,8 +742,7 @@ Vitest supports all common sinon-chai assertions:
| `spy.callCount(n)` | `callCount(n)` | Spy was called n times |
| `spy.calledWith(...)` | `calledWith(...)` | Spy was called with specific args |
| `spy.calledOnceWith(...)` | `calledOnceWith(...)` | Spy was called once with specific args |
| `spy.returned` | `returned` | Spy returned successfully |
| `spy.returnedWith(value)` | `returnedWith(value)` | Spy returned specific value |
Comment on lines -745 to -746

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like this was oversight or hallucination. Probably no way back to remove returnedWith, so now returnedWith and returned are essentially same.

| `spy.returned(value)` | `returned` | Spy returned specific value |

See the [Chai-Style Spy Assertions](/api/expect#chai-style-spy-assertions) documentation for the complete list.

Expand Down
7 changes: 4 additions & 3 deletions packages/expect/src/chai-style-assertions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export const ChaiStyleAssertions: ChaiPlugin = (chai, utils) => {
name: keyof Assertion,
delegateTo: keyof Assertion,
) {
utils.addChainableMethod(
utils.addMethod(

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Chai's addChainableMethod uses Object.defineProperty({ get }) and addMethod uses simple assignment. Thus addMethod allows later addMethod to take over, but addChainableMethod breaks.

Not sure what was the purpose but ours aren't chainable anyways, so just switched to addMethod.

chai.Assertion.prototype,
name,
function (this: Chai.AssertionStatic & Assertion, ...args: any[]) {
Expand All @@ -56,13 +56,13 @@ export const ChaiStyleAssertions: ChaiPlugin = (chai, utils) => {
}
return jestMethod.call(this, ...args)
},
() => {},
)
}

// API to (somewhat) mirror sinon-chai
// https://github.com/chaijs/sinon-chai
defProperty('called', 'toHaveBeenCalled')
defProperty('calledOnce', 'toHaveBeenCalledOnce')
defProperty('returned', 'toHaveReturned')

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

defProperty uses defineProperty(get), so later call of addMethod('returned') by sinon-chai breaks.

defPropertyWithArgs('calledTwice', 'toHaveBeenCalledTimes', 2)
defPropertyWithArgs('calledThrice', 'toHaveBeenCalledTimes', 3)

Expand All @@ -71,6 +71,7 @@ export const ChaiStyleAssertions: ChaiPlugin = (chai, utils) => {
defMethod('calledOnceWith', 'toHaveBeenCalledExactlyOnceWith')
defMethod('lastCalledWith', 'toHaveBeenLastCalledWith')
defMethod('nthCalledWith', 'toHaveBeenNthCalledWith')
defMethod('returned', 'toHaveReturned')
defMethod('returnedWith', 'toHaveReturnedWith')
defMethod('returnedTimes', 'toHaveReturnedTimes')
defMethod('lastReturnedWith', 'toHaveLastReturnedWith')
Expand Down
8 changes: 4 additions & 4 deletions packages/expect/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -836,13 +836,13 @@ export interface ChaiMockAssertion {
nthCalledWith: <E extends any[]>(n: number, ...args: E) => void

/**
* Checks that a spy returned successfully at least once.
* Chai-style equivalent of `toHaveReturned`.
* Checks that a spy returned a specific value at least once.
* Chai-style equivalent of `toHaveReturnedWith`.
*
* @example
* expect(spy).to.have.returned
* expect(spy).to.have.returned('value')
*/
readonly returned: Assertion
returned: <E>(value: E) => void

/**
* Checks that a spy returned a specific value at least once.
Expand Down
4 changes: 2 additions & 2 deletions test/core/test/chai-style-assertions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ describe('Chai-style assertions', () => {
it('passes when spy returned successfully', () => {
const spy = vi.fn(() => 'value')
spy()
expect(spy).to.have.returned
expect(spy).to.have.returned('value')
})

it('fails when spy threw an error', () => {
Expand All @@ -218,7 +218,7 @@ describe('Chai-style assertions', () => {
}
catch {}
expect(() => {
expect(spy).to.have.returned
expect(spy).to.have.returned('value')
}).toThrow(/expected "testSpy" to be successfully called at least once/)
})

Expand Down
Loading