Skip to content

Commit

Permalink
fix: cut duplicate error in negated toHaveBeenCalled (#2638)
Browse files Browse the repository at this point in the history
Previously the error message when failing a negated `toHaveBeenCalled`
or `toHaveBeenCalledWith` expectation was duplicated. This commit
removes the duplication.
  • Loading branch information
richardboehme committed Jan 10, 2023
1 parent a186a7e commit 09d6222
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
4 changes: 2 additions & 2 deletions packages/expect/src/jest-expect.ts
Expand Up @@ -418,7 +418,7 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => {
],
)
if (called && isNot)
msg += formatCalls(spy, msg)
msg = formatCalls(spy, msg)

if ((called && isNot) || (!called && !isNot)) {
const err = new Error(msg)
Expand All @@ -443,7 +443,7 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => {
)

if ((pass && isNot) || (!pass && !isNot)) {
msg += formatCalls(spy, msg, args)
msg = formatCalls(spy, msg, args)
const err = new Error(msg)
err.name = 'AssertionError'
throw err
Expand Down
26 changes: 26 additions & 0 deletions test/core/test/jest-expect.test.ts
Expand Up @@ -463,6 +463,32 @@ describe('toSatisfy()', () => {
})
})

describe('toHaveBeenCalled', () => {
describe('negated', () => {
it('fails if called', () => {
const mock = vi.fn()
mock()

expect(() => {
expect(mock).not.toHaveBeenCalled()
}).toThrow(/^expected "spy" to not be called at all[^e]/)
})
})
})

describe('toHaveBeenCalledWith', () => {
describe('negated', () => {
it('fails if called', () => {
const mock = vi.fn()
mock(3)

expect(() => {
expect(mock).not.toHaveBeenCalledWith(3)
}).toThrow(/^expected "spy" to not be called with arguments: \[ 3 \][^e]/)
})
})
})

describe('async expect', () => {
it('resolves', async () => {
await expect((async () => 'true')()).resolves.toBe('true')
Expand Down

0 comments on commit 09d6222

Please sign in to comment.