-
-
Notifications
You must be signed in to change notification settings - Fork 225
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add matcher .toHaveBeenCalledOnce (#274)
Co-authored-by: Igor Bykov <[email protected]>
- Loading branch information
1 parent
d5dd8f1
commit 405f1e7
Showing
8 changed files
with
180 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
30 changes: 30 additions & 0 deletions
30
src/matchers/toHaveBeenCalledOnce/__snapshots__/index.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,30 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`.not.toHaveBeenCalledOnce fails if mock was invoked exactly once 1`] = ` | ||
"<dim>expect(</><red>received</><dim>).not.toHaveBeenCalledOnce(</><green>expected</><dim>)</> | ||
Expected mock function to have been called any amount of times but one, but it was called exactly once." | ||
`; | ||
exports[`.toHaveBeenCalledOnce fails if mock was invoked more than once, indicating how many times it was invoked 1`] = ` | ||
"<dim>expect(</><red>received</><dim>).toHaveBeenCalledOnce(</><green>expected</><dim>)</> | ||
Expected mock function to have been called exactly once, but it was called: | ||
<red>17</> times" | ||
`; | ||
exports[`.toHaveBeenCalledOnce fails if mock was never invoked indicating that it was invoked 0 times 1`] = ` | ||
"<dim>expect(</><red>received</><dim>).toHaveBeenCalledOnce(</><green>expected</><dim>)</> | ||
Expected mock function to have been called exactly once, but it was called: | ||
<red>0</> times" | ||
`; | ||
exports[`.toHaveBeenCalledOnce fails when given value is not a jest spy or mock 1`] = ` | ||
"<dim>expect(</><red>received</><dim>).toHaveBeenCalledAfter(</><green>expected</><dim>)</> | ||
Matcher error: <red>\\"received\\"</> must be a mock or spy function | ||
Received has type: function | ||
Received has value: <red>[Function mock1]</>" | ||
`; |
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,45 @@ | ||
import { matcherHint, printReceived, printWithType } from 'jest-matcher-utils'; | ||
|
||
import { isJestMockOrSpy } from '../../utils'; | ||
|
||
import predicate from './predicate'; | ||
|
||
const passMessage = () => () => | ||
matcherHint('.not.toHaveBeenCalledOnce') + | ||
'\n\n' + | ||
'Expected mock function to have been called any amount of times but one, but it was called exactly once.'; | ||
|
||
const failMessage = mockFn => () => { | ||
return ( | ||
matcherHint('.toHaveBeenCalledOnce') + | ||
'\n\n' + | ||
'Expected mock function to have been called exactly once, but it was called:\n' + | ||
` ${printReceived(mockFn.mock.calls.length)} times` | ||
); | ||
}; | ||
|
||
const mockCheckFailMessage = value => () => { | ||
return ( | ||
matcherHint('.toHaveBeenCalledAfter') + | ||
'\n\n' + | ||
`Matcher error: ${printReceived('received')} must be a mock or spy function` + | ||
'\n\n' + | ||
printWithType('Received', value, printReceived) | ||
); | ||
}; | ||
|
||
export default { | ||
toHaveBeenCalledOnce: received => { | ||
if (!isJestMockOrSpy(received)) { | ||
return { pass: false, message: mockCheckFailMessage(received) }; | ||
} | ||
|
||
const pass = predicate(received); | ||
|
||
return { | ||
pass, | ||
message: pass ? passMessage(received) : failMessage(received), | ||
actual: received, | ||
}; | ||
}, | ||
}; |
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,52 @@ | ||
import matcher from './'; | ||
|
||
expect.extend(matcher); | ||
|
||
describe('.toHaveBeenCalledOnce', () => { | ||
let mock; | ||
beforeEach(() => { | ||
mock = jest.fn(); | ||
}); | ||
|
||
test('passes if mock was invoked exactly once', () => { | ||
mock(); | ||
expect(mock).toHaveBeenCalledOnce(); | ||
}); | ||
|
||
test('fails if mock was never invoked indicating that it was invoked 0 times', () => { | ||
expect(() => expect(mock).toHaveBeenCalledOnce()).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).toHaveBeenCalledOnce()).toThrowErrorMatchingSnapshot(); | ||
}); | ||
|
||
test('fails when given value is not a jest spy or mock', () => { | ||
const mock1 = () => {}; | ||
expect(() => expect(mock1).toHaveBeenCalledOnce()).toThrowErrorMatchingSnapshot(); | ||
}); | ||
}); | ||
|
||
describe('.not.toHaveBeenCalledOnce', () => { | ||
let mock; | ||
beforeEach(() => { | ||
mock = jest.fn(); | ||
}); | ||
|
||
test('passes if mock was never invoked', () => { | ||
expect(mock).not.toHaveBeenCalledOnce(); | ||
}); | ||
|
||
test('passes if mock was invoked more than once', () => { | ||
mock(); | ||
mock(); | ||
expect(mock).not.toHaveBeenCalledOnce(); | ||
}); | ||
|
||
test('fails if mock was invoked exactly once', () => { | ||
mock(); | ||
expect(() => expect(mock).not.toHaveBeenCalledOnce()).toThrowErrorMatchingSnapshot(); | ||
}); | ||
}); |
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 @@ | ||
export default mockFn => mockFn.mock.calls.length === 1; |
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,25 @@ | ||
import predicate from './predicate'; | ||
|
||
describe('.toHaveBeenCalledOnce predicate', () => { | ||
let mock; | ||
beforeEach(() => { | ||
// Refresh on each test | ||
mock = jest.fn(); | ||
}); | ||
|
||
test('returns true if mock was invoked exactly once', () => { | ||
mock(); | ||
expect(predicate(mock)).toBe(true); | ||
}); | ||
|
||
test('returns true if mock was invoked any amount of times but one', () => { | ||
expect(predicate(mock)).toBe(false); | ||
|
||
mock(); | ||
mock(); | ||
expect(predicate(mock)).toBe(false); | ||
|
||
new Array(20).fill(mock).forEach(e => e()); | ||
expect(predicate(mock)).toBe(false); | ||
}); | ||
}); |
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