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

Verify mocks called #15

Merged
merged 2 commits into from
Apr 5, 2019
Merged

Conversation

roarosa
Copy link
Contributor

@roarosa roarosa commented Mar 30, 2019

Add functionality for confirming that no extra unused mocks were created.

@codecov-io
Copy link

Codecov Report

Merging #15 into master will not change coverage.
The diff coverage is 100%.

Impacted file tree graph

@@          Coverage Diff          @@
##           master    #15   +/-   ##
=====================================
  Coverage     100%   100%           
=====================================
  Files           2      2           
  Lines          73     79    +6     
=====================================
+ Hits           73     79    +6
Impacted Files Coverage Δ
src/when.js 100% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update d05d7be...c37b70a. Read the comment docs.

expect({
...rest,
name: fn.getMockName()
}).toEqual(expect.objectContaining({ hasBeenCalled: true }))
Copy link
Owner

@timkindberg timkindberg Mar 31, 2019

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?

Copy link
Contributor Author

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.

Copy link
Owner

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?

Copy link
Contributor Author

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.

Copy link
Contributor Author

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?

@timkindberg
Copy link
Owner

@roaclark Please see #16

@timkindberg timkindberg merged commit c37b70a into timkindberg:master Apr 5, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants