From 2adf68380ddf31a26c69287a4137524301a1e4fb Mon Sep 17 00:00:00 2001 From: Keegan Witt Date: Fri, 14 Apr 2023 11:21:46 -0400 Subject: [PATCH] Rename toHaveBeenCalledOnceWith to toHaveBeenCalledExactlyOnceWith (closes #529) (#574) --- src/matchers/index.js | 2 +- ....js => toHaveBeenCalledExactlyOnceWith.js} | 8 ++--- ...aveBeenCalledExactlyOnceWith.test.js.snap} | 20 ++++++------ ...> toHaveBeenCalledExactlyOnceWith.test.js} | 32 +++++++++---------- types/index.d.ts | 8 ++--- website/docs/matchers/Mock.mdx | 8 ++--- website/docs/matchers/index.md | 2 +- 7 files changed, 40 insertions(+), 40 deletions(-) rename src/matchers/{toHaveBeenCalledOnceWith.js => toHaveBeenCalledExactlyOnceWith.js} (82%) rename test/matchers/__snapshots__/{toHaveBeenCalledOnceWith.test.js.snap => toHaveBeenCalledExactlyOnceWith.test.js.snap} (55%) rename test/matchers/{toHaveBeenCalledOnceWith.test.js => toHaveBeenCalledExactlyOnceWith.test.js} (56%) diff --git a/src/matchers/index.js b/src/matchers/index.js index 88d9fa2b..514b2b03 100644 --- a/src/matchers/index.js +++ b/src/matchers/index.js @@ -51,7 +51,7 @@ export { toEqualCaseInsensitive } from './toEqualCaseInsensitive'; export { toHaveBeenCalledAfter } from './toHaveBeenCalledAfter'; export { toHaveBeenCalledBefore } from './toHaveBeenCalledBefore'; export { toHaveBeenCalledOnce } from './toHaveBeenCalledOnce'; -export { toHaveBeenCalledOnceWith } from './toHaveBeenCalledOnceWith'; +export { toHaveBeenCalledExactlyOnceWith } from './toHaveBeenCalledExactlyOnceWith'; export { toInclude } from './toInclude'; export { toIncludeAllMembers } from './toIncludeAllMembers'; export { toIncludeAllPartialMembers } from './toIncludeAllPartialMembers'; diff --git a/src/matchers/toHaveBeenCalledOnceWith.js b/src/matchers/toHaveBeenCalledExactlyOnceWith.js similarity index 82% rename from src/matchers/toHaveBeenCalledOnceWith.js rename to src/matchers/toHaveBeenCalledExactlyOnceWith.js index e868433e..f72229d4 100644 --- a/src/matchers/toHaveBeenCalledOnceWith.js +++ b/src/matchers/toHaveBeenCalledExactlyOnceWith.js @@ -1,13 +1,13 @@ import { isJestMockOrSpy } from '../utils'; -export function toHaveBeenCalledOnceWith(received, ...expected) { +export function toHaveBeenCalledExactlyOnceWith(received, ...expected) { const { printReceived, printExpected, printWithType, matcherHint } = this.utils; if (!isJestMockOrSpy(received)) { return { pass: false, message: () => - matcherHint('.toHaveBeenCalledOnceWith', 'received', '') + + matcherHint('.toHaveBeenCalledExactlyOnceWith', 'received', '') + '\n\n' + `Matcher error: ${printReceived('received')} must be a mock or spy function` + '\n\n' + @@ -23,12 +23,12 @@ export function toHaveBeenCalledOnceWith(received, ...expected) { pass, message: () => { return pass - ? matcherHint('.not.toHaveBeenCalledOnceWith', 'received', '') + + ? matcherHint('.not.toHaveBeenCalledExactlyOnceWith', 'received', '') + '\n\n' + 'Expected mock to be invoked some number of times other than once or once with ' + `arguments other than ${printExpected(expected)}, but was invoked ` + `${printReceived(received.mock.calls.length)} times with ${printReceived(...actual)}` - : matcherHint('.toHaveBeenCalledOnceWith') + + : matcherHint('.toHaveBeenCalledExactlyOnceWith') + '\n\n' + (invokedOnce ? 'Expected mock function to have been called exactly once with ' + diff --git a/test/matchers/__snapshots__/toHaveBeenCalledOnceWith.test.js.snap b/test/matchers/__snapshots__/toHaveBeenCalledExactlyOnceWith.test.js.snap similarity index 55% rename from test/matchers/__snapshots__/toHaveBeenCalledOnceWith.test.js.snap rename to test/matchers/__snapshots__/toHaveBeenCalledExactlyOnceWith.test.js.snap index d7d96712..06da0606 100644 --- a/test/matchers/__snapshots__/toHaveBeenCalledOnceWith.test.js.snap +++ b/test/matchers/__snapshots__/toHaveBeenCalledExactlyOnceWith.test.js.snap @@ -1,25 +1,25 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`.not.toHaveBeenCalledOnceWith fails if mock was invoked exactly once with the expected value 1`] = ` -"expect(received).not.toHaveBeenCalledOnceWith() +exports[`.not.toHaveBeenCalledExactlyOnceWith fails if mock was invoked exactly once with the expected value 1`] = ` +"expect(received).not.toHaveBeenCalledExactlyOnceWith() Expected mock to be invoked some number of times other than once or once with arguments other than ["hello"], but was invoked 1 times with "hello"" `; -exports[`.toHaveBeenCalledOnceWith fails if mock was invoked more than once, indicating how many times it was invoked 1`] = ` -"expect(received).toHaveBeenCalledOnceWith(expected) +exports[`.toHaveBeenCalledExactlyOnceWith fails if mock was invoked more than once, indicating how many times it was invoked 1`] = ` +"expect(received).toHaveBeenCalledExactlyOnceWith(expected) Expected mock function to have been called exactly once, but it was called 17 times" `; -exports[`.toHaveBeenCalledOnceWith fails if mock was never invoked indicating that it was invoked 0 times 1`] = ` -"expect(received).toHaveBeenCalledOnceWith(expected) +exports[`.toHaveBeenCalledExactlyOnceWith fails if mock was never invoked indicating that it was invoked 0 times 1`] = ` +"expect(received).toHaveBeenCalledExactlyOnceWith(expected) Expected mock function to have been called exactly once, but it was called 0 times" `; -exports[`.toHaveBeenCalledOnceWith fails when given value is not a jest spy or mock 1`] = ` -"expect(received).toHaveBeenCalledOnceWith() +exports[`.toHaveBeenCalledExactlyOnceWith fails when given value is not a jest spy or mock 1`] = ` +"expect(received).toHaveBeenCalledExactlyOnceWith() Matcher error: "received" must be a mock or spy function @@ -27,8 +27,8 @@ Received has type: function Received has value: [Function mock1]" `; -exports[`.toHaveBeenCalledOnceWith fails when given value is not the expected one 1`] = ` -"expect(received).toHaveBeenCalledOnceWith(expected) +exports[`.toHaveBeenCalledExactlyOnceWith fails when given value is not the expected one 1`] = ` +"expect(received).toHaveBeenCalledExactlyOnceWith(expected) Expected mock function to have been called exactly once with ["hello"], but it was called with "not hello"" `; diff --git a/test/matchers/toHaveBeenCalledOnceWith.test.js b/test/matchers/toHaveBeenCalledExactlyOnceWith.test.js similarity index 56% rename from test/matchers/toHaveBeenCalledOnceWith.test.js rename to test/matchers/toHaveBeenCalledExactlyOnceWith.test.js index 3e1e8741..8e041fea 100644 --- a/test/matchers/toHaveBeenCalledOnceWith.test.js +++ b/test/matchers/toHaveBeenCalledExactlyOnceWith.test.js @@ -1,8 +1,8 @@ -import * as matcher from 'src/matchers/toHaveBeenCalledOnceWith'; +import * as matcher from 'src/matchers/toHaveBeenCalledExactlyOnceWith'; expect.extend(matcher); -describe('.toHaveBeenCalledOnceWith', () => { +describe('.toHaveBeenCalledExactlyOnceWith', () => { let mock; beforeEach(() => { mock = jest.fn(); @@ -10,73 +10,73 @@ describe('.toHaveBeenCalledOnceWith', () => { test('passes if mock was invoked exactly once with the expected value', () => { mock('hello'); - expect(mock).toHaveBeenCalledOnceWith('hello'); + expect(mock).toHaveBeenCalledExactlyOnceWith('hello'); }); test('passes if mock was invoked exactly once with the expected values in an array', () => { mock(['hello', 'there']); - expect(mock).toHaveBeenCalledOnceWith(['hello', 'there']); + expect(mock).toHaveBeenCalledExactlyOnceWith(['hello', 'there']); }); test('passes if mock was invoked exactly once with the expected values', () => { mock('hello', 'there'); - expect(mock).toHaveBeenCalledOnceWith('hello', 'there'); + expect(mock).toHaveBeenCalledExactlyOnceWith('hello', 'there'); }); test('fails if mock was never invoked indicating that it was invoked 0 times', () => { - expect(() => expect(mock).toHaveBeenCalledOnceWith('hello')).toThrowErrorMatchingSnapshot(); + expect(() => expect(mock).toHaveBeenCalledExactlyOnceWith('hello')).toThrowErrorMatchingSnapshot(); }); test('fails if mock was invoked more than once, indicating how many times it was invoked', () => { // Invoke mock 17 times new Array(17).fill(mock).forEach(e => e(Math.random())); - expect(() => expect(mock).toHaveBeenCalledOnceWith('hello')).toThrowErrorMatchingSnapshot(); + expect(() => expect(mock).toHaveBeenCalledExactlyOnceWith('hello')).toThrowErrorMatchingSnapshot(); }); test('fails when given value is not a jest spy or mock', () => { const mock1 = () => {}; - expect(() => expect(mock1).toHaveBeenCalledOnceWith('hello')).toThrowErrorMatchingSnapshot(); + expect(() => expect(mock1).toHaveBeenCalledExactlyOnceWith('hello')).toThrowErrorMatchingSnapshot(); }); test('fails when given value is not the expected one', () => { mock('not hello'); - expect(() => expect(mock).toHaveBeenCalledOnceWith('hello')).toThrowErrorMatchingSnapshot(); + expect(() => expect(mock).toHaveBeenCalledExactlyOnceWith('hello')).toThrowErrorMatchingSnapshot(); }); }); -describe('.not.toHaveBeenCalledOnceWith', () => { +describe('.not.toHaveBeenCalledExactlyOnceWith', () => { let mock; beforeEach(() => { mock = jest.fn(); }); test('passes if mock was never invoked', () => { - expect(mock).not.toHaveBeenCalledOnceWith('hello'); + expect(mock).not.toHaveBeenCalledExactlyOnceWith('hello'); }); test('passes if mock was invoked more than once', () => { mock('hello'); mock('hello'); - expect(mock).not.toHaveBeenCalledOnceWith('hello'); + expect(mock).not.toHaveBeenCalledExactlyOnceWith('hello'); }); test('fails if mock was invoked exactly once with the expected value', () => { mock('hello'); - expect(() => expect(mock).not.toHaveBeenCalledOnceWith('hello')).toThrowErrorMatchingSnapshot(); + expect(() => expect(mock).not.toHaveBeenCalledExactlyOnceWith('hello')).toThrowErrorMatchingSnapshot(); }); test('passes if mock was invoked exactly once without the expected value', () => { mock('not hello'); - expect(mock).not.toHaveBeenCalledOnceWith('hello'); + expect(mock).not.toHaveBeenCalledExactlyOnceWith('hello'); }); test('passes if mock was invoked exactly once without both expected values in an array', () => { mock(['hello', 'there']); - expect(mock).not.toHaveBeenCalledOnceWith(['hello', 'not there']); + expect(mock).not.toHaveBeenCalledExactlyOnceWith(['hello', 'not there']); }); test('passes if mock was invoked exactly once without both expected values', () => { mock('hello', 'there'); - expect(mock).not.toHaveBeenCalledOnceWith('hello', 'not there'); + expect(mock).not.toHaveBeenCalledExactlyOnceWith('hello', 'not there'); }); }); diff --git a/types/index.d.ts b/types/index.d.ts index a420baf5..af5fd0ee 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -164,9 +164,9 @@ interface CustomMatchers extends Record { toHaveBeenCalledOnce(): R; /** - * Use `.toHaveBeenCalledOnceWith` to check if a `Mock` was called exactly one time with the expected value. + * Use `.toHaveBeenCalledExactlyOnceWith` to check if a `Mock` was called exactly one time with the expected value. */ - toHaveBeenCalledOnceWith(): R; + toHaveBeenCalledExactlyOnceWith(): R; /** * Use `.toBeNumber` when checking if a value is a `Number`. @@ -598,9 +598,9 @@ declare namespace jest { toHaveBeenCalledOnce(): R; /** - * Use `.toHaveBeenCalledOnceWith` to check if a `Mock` was called exactly one time with the expected value. + * Use `.toHaveBeenCalledExactlyOnceWith` to check if a `Mock` was called exactly one time with the expected value. */ - toHaveBeenCalledOnceWith(...args: unknown[]): R; + toHaveBeenCalledExactlyOnceWith(...args: unknown[]): R; /** * Use `.toBeNumber` when checking if a value is a `Number`. diff --git a/website/docs/matchers/Mock.mdx b/website/docs/matchers/Mock.mdx index 0ba58910..4c749067 100644 --- a/website/docs/matchers/Mock.mdx +++ b/website/docs/matchers/Mock.mdx @@ -48,16 +48,16 @@ Use `.toHaveBeenCalledOnce` to check if a `Mock` was called exactly one time. -### .toHaveBeenCalledOnceWith() +### .toHaveBeenCalledExactlyOnceWith() -Use `.toHaveBeenCalledOnceWith` to check if a `Mock` was called exactly one time and with the expected values. +Use `.toHaveBeenCalledExactlyOnceWith` to check if a `Mock` was called exactly one time and with the expected values. - + {`test('passes only if mock was called exactly once with the expected value', () => { const mock = jest.fn();\n expect(mock).not.toHaveBeenCalled(); mock('hello'); - expect(mock).toHaveBeenCalledOnceWith('hello'); + expect(mock).toHaveBeenCalledExactlyOnceWith('hello'); });`} diff --git a/website/docs/matchers/index.md b/website/docs/matchers/index.md index f44e50eb..7674400e 100644 --- a/website/docs/matchers/index.md +++ b/website/docs/matchers/index.md @@ -50,7 +50,7 @@ sidebar_position: 1 - [.toHaveBeenCalledBefore()](/docs/matchers/mock/#tohavebeencalledbefore) - [.toHaveBeenCalledAfter()](/docs/matchers/mock/#tohavebeencalledafter) - [.toHaveBeenCalledOnce()](/docs/matchers/mock/#tohavebeencalledonce) -- [.toHaveBeenCalledOnceWith()](/docs/matchers/mock/#tohavebeencalledoncewith) +- [.toHaveBeenCalledExactlyOnceWith()](/docs/matchers/mock/#tohavebeencalledexactlyoncewith) ## [Number](/docs/matchers/number)