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

Rename toHaveBeenCalledOnceWith (closes #529) #574

Merged
merged 1 commit into from
Apr 14, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/matchers/index.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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>"
`;
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,9 @@ interface CustomMatchers<R> {
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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