access to jest.fn()'s [enhancement]
See original GitHub issueIt would be nice if the mocked jest.fn()'s were easily available so that toHaveBeenCalledWith() can used.
For example, when testing a getXXX function, I don’t really need to test that the value I just mocked is being returned as much as I need to make sure that the findOne function was actually called on the model with the proper parameters.
In a couple contrived jest tests, I’d see this working as such:
it('should call findOne with the _id', async () => {
await getMyDocument('some_id');
expect(mockingoose.MyDocument.findOne).toHaveBeenCalledWith({_id: 'some_id'});
});
it('should call findOne with the _id and isActive flag', async () => {
await getMyDocument('some_id?isActive=true');
expect(mockingoose.MyDocument.findOne).toHaveBeenCalledWith({_id: 'some_id', isActive: true});
});
Issue Analytics
- State:
- Created 6 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Mock Functions - Jest
All mock functions have this special .mock property, which is where ... will be displayed instead of 'jest.fn()' in the test error output....
Read more >Enhance Jest for Better Feature Flag Testing in JavaScript - Split
This class is responsible for abstracting over the details of how we access user profiles so that the rest of our codebase doesn't...
Read more >Unit Testing with Jest | CS4530/5500, Spring 2021
log() for our tests, we can do so using a mock. A mock implementation can be substituted for a spy or a jest.fn()....
Read more >Using a mock function in Jest - Learn React with TypeScript 3 ...
Let's change this to a Jest mock function: const handleSubmit = jest.fn();. Our test will run correctly, as it did before, but this...
Read more >Testing React Applications with Jest - Auth0
In this article, we'll look at how to test a React application using the Jest testing framework. Jest is a JavaScript test runner...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@alonronin oh cool,
jest.clearAllMocks()
definitely will help reduce mistakes.I checked
MyDocument.Query.prototype.exec.mock.calls
and all I get is[ [] ]
. So that’s useful forexpect(MyDocument.Query.prototype.exec).toHaveBeenCalled();
but unless I’m doing something wrong I don’t think it’ll allow me to check the query being executedah I see; I guess in case other people find their way here, this seems to be a valid way of doing it:
fyi, if you forget the second parameter then you’ll get a blank failure message, pays to remember that while the second parameter to
findOne
is optional, jest’sexpect
doesn’t care and expects you to declare that it will beundefined
.But the real gotcha in this is that the
jest.fn
never clears, so you must change yourbeforeEach
to be as such:Otherwise one test’s calls will carry over into another test’s expectation and you will get false-positives.