Skip to content

Commit 09d6222

Browse files
authoredJan 10, 2023
fix: cut duplicate error in negated toHaveBeenCalled (#2638)
Previously the error message when failing a negated `toHaveBeenCalled` or `toHaveBeenCalledWith` expectation was duplicated. This commit removes the duplication.
1 parent a186a7e commit 09d6222

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed
 

‎packages/expect/src/jest-expect.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => {
418418
],
419419
)
420420
if (called && isNot)
421-
msg += formatCalls(spy, msg)
421+
msg = formatCalls(spy, msg)
422422

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

445445
if ((pass && isNot) || (!pass && !isNot)) {
446-
msg += formatCalls(spy, msg, args)
446+
msg = formatCalls(spy, msg, args)
447447
const err = new Error(msg)
448448
err.name = 'AssertionError'
449449
throw err

‎test/core/test/jest-expect.test.ts

+26
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,32 @@ describe('toSatisfy()', () => {
463463
})
464464
})
465465

466+
describe('toHaveBeenCalled', () => {
467+
describe('negated', () => {
468+
it('fails if called', () => {
469+
const mock = vi.fn()
470+
mock()
471+
472+
expect(() => {
473+
expect(mock).not.toHaveBeenCalled()
474+
}).toThrow(/^expected "spy" to not be called at all[^e]/)
475+
})
476+
})
477+
})
478+
479+
describe('toHaveBeenCalledWith', () => {
480+
describe('negated', () => {
481+
it('fails if called', () => {
482+
const mock = vi.fn()
483+
mock(3)
484+
485+
expect(() => {
486+
expect(mock).not.toHaveBeenCalledWith(3)
487+
}).toThrow(/^expected "spy" to not be called with arguments: \[ 3 \][^e]/)
488+
})
489+
})
490+
})
491+
466492
describe('async expect', () => {
467493
it('resolves', async () => {
468494
await expect((async () => 'true')()).resolves.toBe('true')

0 commit comments

Comments
 (0)
Please sign in to comment.