Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add toHaveBeenCalledOnceWith matcher
- Loading branch information
1 parent
4869f6b
commit fdf3e18
Showing
6 changed files
with
172 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { isJestMockOrSpy } from '../utils'; | ||
|
||
export function toHaveBeenCalledOnceWith(received, expected) { | ||
const { printReceived, printExpected, printWithType, matcherHint } = this.utils; | ||
|
||
if (!isJestMockOrSpy(received)) { | ||
return { | ||
pass: false, | ||
message: () => | ||
matcherHint('.toHaveBeenCalledOnceWith') + | ||
'\n\n' + | ||
`Matcher error: ${printReceived('received')} must be a mock or spy function` + | ||
'\n\n' + | ||
printWithType('Received', received, printReceived), | ||
}; | ||
} | ||
|
||
const passMessage = | ||
matcherHint('.not.toHaveBeenCalledOnceWith') + | ||
'\n\n' + | ||
`Expected mock function to have been called any amount of times but one with ${printExpected( | ||
expected, | ||
)}, but it was called exactly once with ${printExpected(expected)}.`; | ||
|
||
const failOnceMessage = | ||
matcherHint('.toHaveBeenCalledOnceWith') + | ||
'\n\n' + | ||
'Expected mock function to have been called exactly once, but it was called:\n' + | ||
` ${printReceived(received.mock.calls.length)} times`; | ||
|
||
const failExpectedMessage = | ||
matcherHint('.toHaveBeenCalledOnceWith') + | ||
'\n\n' + | ||
`Expected mock function to have been called exactly once with ${printReceived( | ||
expected, | ||
)}, but it was called with:\n` + | ||
` ${printReceived(received.mock.calls[0]?.[0])}`; | ||
|
||
const passOnce = received.mock.calls.length === 1; | ||
const pass = passOnce && this.equals(expected, received.mock.calls[0][0]); | ||
|
||
return { | ||
pass, | ||
message: () => (pass ? passMessage : !passOnce ? failOnceMessage : failExpectedMessage), | ||
actual: received, | ||
}; | ||
} |
37 changes: 37 additions & 0 deletions
37
test/matchers/__snapshots__/toHaveBeenCalledOnceWith.test.js.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`.not.toHaveBeenCalledOnceWith fails if mock was invoked exactly once with the expected value 1`] = ` | ||
"<dim>expect(</><red>received</><dim>).not.toHaveBeenCalledOnceWith(</><green>expected</><dim>)</> | ||
Expected mock function to have been called any amount of times but one with <green>\\"hello\\"</>, but it was called exactly once with <green>\\"hello\\"</>." | ||
`; | ||
exports[`.toHaveBeenCalledOnceWith fails if mock was invoked more than once, indicating how many times it was invoked 1`] = ` | ||
"<dim>expect(</><red>received</><dim>).toHaveBeenCalledOnceWith(</><green>expected</><dim>)</> | ||
Expected mock function to have been called exactly once, but it was called: | ||
<red>17</> times" | ||
`; | ||
exports[`.toHaveBeenCalledOnceWith fails if mock was never invoked indicating that it was invoked 0 times 1`] = ` | ||
"<dim>expect(</><red>received</><dim>).toHaveBeenCalledOnceWith(</><green>expected</><dim>)</> | ||
Expected mock function to have been called exactly once, but it was called: | ||
<red>0</> times" | ||
`; | ||
exports[`.toHaveBeenCalledOnceWith fails when given value is not a jest spy or mock 1`] = ` | ||
"<dim>expect(</><red>received</><dim>).toHaveBeenCalledOnceWith(</><green>expected</><dim>)</> | ||
Matcher error: <red>\\"received\\"</> must be a mock or spy function | ||
Received has type: function | ||
Received has value: <red>[Function mock1]</>" | ||
`; | ||
exports[`.toHaveBeenCalledOnceWith fails when given value is not the expected one 1`] = ` | ||
"<dim>expect(</><red>received</><dim>).toHaveBeenCalledOnceWith(</><green>expected</><dim>)</> | ||
Expected mock function to have been called exactly once with <red>\\"hello\\"</>, but it was called with: | ||
<red>\\"not hello\\"</>" | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import * as matcher from 'src/matchers/toHaveBeenCalledOnceWith'; | ||
|
||
expect.extend(matcher); | ||
|
||
describe('.toHaveBeenCalledOnceWith', () => { | ||
let mock; | ||
beforeEach(() => { | ||
mock = jest.fn(); | ||
}); | ||
|
||
test('passes if mock was invoked exactly once with the expected value', () => { | ||
mock('hello'); | ||
expect(mock).toHaveBeenCalledOnceWith('hello'); | ||
}); | ||
|
||
test('fails if mock was never invoked indicating that it was invoked 0 times', () => { | ||
expect(() => expect(mock).toHaveBeenCalledOnceWith('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(); | ||
}); | ||
|
||
test('fails when given value is not a jest spy or mock', () => { | ||
const mock1 = () => {}; | ||
expect(() => expect(mock1).toHaveBeenCalledOnceWith('hello')).toThrowErrorMatchingSnapshot(); | ||
}); | ||
|
||
test('fails when given value is not the expected one', () => { | ||
mock('not hello'); | ||
expect(() => expect(mock).toHaveBeenCalledOnceWith('hello')).toThrowErrorMatchingSnapshot(); | ||
}); | ||
}); | ||
|
||
describe('.not.toHaveBeenCalledOnceWith', () => { | ||
let mock; | ||
beforeEach(() => { | ||
mock = jest.fn(); | ||
}); | ||
|
||
test('passes if mock was never invoked', () => { | ||
expect(mock).not.toHaveBeenCalledOnceWith('hello'); | ||
}); | ||
|
||
test('passes if mock was invoked more than once', () => { | ||
mock('hello'); | ||
mock('hello'); | ||
expect(mock).not.toHaveBeenCalledOnceWith('hello'); | ||
}); | ||
|
||
test('fails if mock was invoked exactly once with the expected value', () => { | ||
mock('hello'); | ||
expect(() => expect(mock).not.toHaveBeenCalledOnceWith('hello')).toThrowErrorMatchingSnapshot(); | ||
}); | ||
|
||
test('passes if mock was invoked exactly once without the expected value', () => { | ||
mock('not hello'); | ||
expect(mock).not.toHaveBeenCalledOnceWith('hello'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters