Skip to content

Commit

Permalink
Rename toHaveBeenCalledOnceWith to toHaveBeenCalledExactlyOnceWith (c…
Browse files Browse the repository at this point in the history
…loses #529) (#574)
  • Loading branch information
keeganwitt committed Apr 14, 2023
1 parent 18680e8 commit 2adf683
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 40 deletions.
2 changes: 1 addition & 1 deletion src/matchers/index.js
Expand Up @@ -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';
Expand Down
@@ -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' +
Expand All @@ -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 ' +
Expand Down
@@ -1,34 +1,34 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`.not.toHaveBeenCalledOnceWith fails if mock was invoked exactly once with the expected value 1`] = `
"<dim>expect(</intensity><red>received</color><dim>).not.toHaveBeenCalledOnceWith()</intensity>
exports[`.not.toHaveBeenCalledExactlyOnceWith fails if mock was invoked exactly once with the expected value 1`] = `
"<dim>expect(</intensity><red>received</color><dim>).not.toHaveBeenCalledExactlyOnceWith()</intensity>
Expected mock to be invoked some number of times other than once or once with arguments other than <green>["hello"]</color>, but was invoked <red>1</color> times with <red>"hello"</color>"
`;

exports[`.toHaveBeenCalledOnceWith fails if mock was invoked more than once, indicating how many times it was invoked 1`] = `
"<dim>expect(</intensity><red>received</color><dim>).toHaveBeenCalledOnceWith(</intensity><green>expected</color><dim>)</intensity>
exports[`.toHaveBeenCalledExactlyOnceWith fails if mock was invoked more than once, indicating how many times it was invoked 1`] = `
"<dim>expect(</intensity><red>received</color><dim>).toHaveBeenCalledExactlyOnceWith(</intensity><green>expected</color><dim>)</intensity>
Expected mock function to have been called exactly once, but it was called <red>17</color> times"
`;

exports[`.toHaveBeenCalledOnceWith fails if mock was never invoked indicating that it was invoked 0 times 1`] = `
"<dim>expect(</intensity><red>received</color><dim>).toHaveBeenCalledOnceWith(</intensity><green>expected</color><dim>)</intensity>
exports[`.toHaveBeenCalledExactlyOnceWith fails if mock was never invoked indicating that it was invoked 0 times 1`] = `
"<dim>expect(</intensity><red>received</color><dim>).toHaveBeenCalledExactlyOnceWith(</intensity><green>expected</color><dim>)</intensity>
Expected mock function to have been called exactly once, but it was called <red>0</color> times"
`;

exports[`.toHaveBeenCalledOnceWith fails when given value is not a jest spy or mock 1`] = `
"<dim>expect(</intensity><red>received</color><dim>).toHaveBeenCalledOnceWith()</intensity>
exports[`.toHaveBeenCalledExactlyOnceWith fails when given value is not a jest spy or mock 1`] = `
"<dim>expect(</intensity><red>received</color><dim>).toHaveBeenCalledExactlyOnceWith()</intensity>
Matcher error: <red>"received"</color> must be a mock or spy function
Received has type: function
Received has value: <red>[Function mock1]</color>"
`;

exports[`.toHaveBeenCalledOnceWith fails when given value is not the expected one 1`] = `
"<dim>expect(</intensity><red>received</color><dim>).toHaveBeenCalledOnceWith(</intensity><green>expected</color><dim>)</intensity>
exports[`.toHaveBeenCalledExactlyOnceWith fails when given value is not the expected one 1`] = `
"<dim>expect(</intensity><red>received</color><dim>).toHaveBeenCalledExactlyOnceWith(</intensity><green>expected</color><dim>)</intensity>
Expected mock function to have been called exactly once with <green>["hello"]</color>, but it was called with <red>"not hello"</color>"
`;
@@ -1,82 +1,82 @@
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();
});

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');
});
});
8 changes: 4 additions & 4 deletions types/index.d.ts
Expand Up @@ -164,9 +164,9 @@ interface CustomMatchers<R> extends Record<string, any> {
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`.
Expand Down Expand Up @@ -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`.
Expand Down
8 changes: 4 additions & 4 deletions website/docs/matchers/Mock.mdx
Expand Up @@ -48,16 +48,16 @@ Use `.toHaveBeenCalledOnce` to check if a `Mock` was called exactly one time.

</TestFile>

### .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.

<TestFile name="toHaveBeenCalledOnceWith">
<TestFile name="toHaveBeenCalledExactlyOnceWith">
{`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');
});`}

</TestFile>
2 changes: 1 addition & 1 deletion website/docs/matchers/index.md
Expand Up @@ -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)

Expand Down

0 comments on commit 2adf683

Please sign in to comment.