Skip to content
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

fix(expect): check for no 'throw' type in toHaveReturned #2850

Merged
merged 4 commits into from
Feb 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/expect/src/jest-expect.ts
Expand Up @@ -549,7 +549,7 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => {
def(['toHaveReturned', 'toReturn'], function () {
const spy = getSpy(this)
const spyName = spy.getMockName()
const calledAndNotThrew = spy.mock.calls.length > 0 && !spy.mock.results.some(({ type }) => type === 'throw')
const calledAndNotThrew = spy.mock.calls.length > 0 && spy.mock.results.some(({ type }) => type !== 'throw')
this.assert(
calledAndNotThrew,
`expected "${spyName}" to be successfully called at least once`,
Expand Down
14 changes: 9 additions & 5 deletions test/core/test/fn.test.ts
Expand Up @@ -60,29 +60,33 @@ describe('mock', () => {
let i = 0

const fn = vitest.fn(() => {
if (i === 1) {
if (i === 0) {
++i
throw new Error('error')
}

return String(++i)
})

fn()
try {
fn()
}
catch {}
expect(fn).not.toHaveReturned()

fn()
expect(fn).toHaveReturned()

fn()

try {
expect(fn).toHaveNthReturnedWith(2, '2')
assert.fail('expect should throw, since 2nd call is thrown')
expect(fn).toHaveNthReturnedWith(1, '1')
assert.fail('expect should throw, since 1st call is thrown')
}
catch {}

// not throws
expect(fn).not.toHaveNthReturnedWith(2, '2')
expect(fn).not.toHaveNthReturnedWith(1, '1')

expect(fn).toHaveReturnedTimes(2)
expect(fn).toHaveNthReturnedWith(3, '3')
Expand Down