-
Notifications
You must be signed in to change notification settings - Fork 39
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
Verify mocks called #15
Conversation
Codecov Report
@@ Coverage Diff @@
## master #15 +/- ##
=====================================
Coverage 100% 100%
=====================================
Files 2 2
Lines 73 79 +6
=====================================
+ Hits 73 79 +6
Continue to review full report at Codecov.
|
expect({ | ||
...rest, | ||
name: fn.getMockName() | ||
}).toEqual(expect.objectContaining({ hasBeenCalled: true })) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't quite understand this. Can you just check that callMock.hasBeenCalled == true
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I started with expect(hasBeenCalled).toBe(true)
, but the error message received was not very helpful. It was just "Expected true but received false", with no indication of which function or which mocked call was actually failing.
That being said, this doesn't feel like a particularly elegant solution and I would appreciate better alternatives. Jest's solution for a custom error message is a custom matcher, but that seemed even more heavy-handed (jestjs/jest#3293). I would also be fine going back to the simple boolean comparison if you prefer.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok that makes sense. I wonder if there would be a way to construct an expect(callMock).toBeCalledWith(...args)
assertion per fn.__whenMock__.callMocks
mock. What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like that idea, but I'm struggling with it due to the ordering. I don't think toBeCalledWith is sufficient because it doesn't cover having multiple "once" statements.
when(fn).calledWith(1).mockReturnValueOnce(2).mockReturnValueOnce(2)
fn(1)
when.verifyAllWhenMocksCalled() // toBeCalledWith(1) would return true, but this should still fail
Perhaps something with toHaveBeenNthCalledWith would work if whenMock was on the latest version of Jest, but it would just be tricky due to the rearranging of callMocks with once.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Two other possibilities that surface a bit more information:
Call an expect on the matchers if not called. Still not the most obvious code, but gives more information about what the function should have been called with.
fn.__whenMock__.callMocks.forEach(({ hasBeenCalled, matchers }) => {
if (!hasBeenCalled) {
expect([]).toEqual(matchers)
}
})
Filter out the called mocks and assert that the remaining list is empty. I think this is much cleaner code, but it also exposes more whenMock internals.
const uncalledMocks = fn.__whenMock__.callMocks.filter(mock => !mock.hasBeenCalled)
expect(uncalledMocks).toHaveLength(0)
Do either of those sound good to you?
@roaclark Please see #16 |
Add functionality for confirming that no extra unused mocks were created.