question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

access to jest.fn()'s [enhancement]

See original GitHub issue

It 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:closed
  • Created 6 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
ironforwardcommented, Feb 24, 2018

@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 for expect(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 executed

2reactions
ironforwardcommented, Feb 24, 2018

ah I see; I guess in case other people find their way here, this seems to be a valid way of doing it:

expect(MyDocument.Query.prototype.findOne).toHaveBeenCalledWith({ _id: 'some_id' }, undefined);

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’s expect doesn’t care and expects you to declare that it will be undefined.

But the real gotcha in this is that the jest.fn never clears, so you must change your beforeEach to be as such:

beforeEach(() => {
  mockingoose.resetAll();
  MyDocument.Query.prototype.findOne.mockClear();
});

Otherwise one test’s calls will carry over into another test’s expectation and you will get false-positives.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found