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.

Suggestion: Expose mocked jest function

See original GitHub issue

Case: I want to known what parameter passed to mongoose function (E.X. find, countDocuments…etc.)

Example code: query.js

// Let use User Model in __tests__/User.js
import user from "User"
export queryUserByName = (name) => user.find({
   name
})

My Test

import mockingoose from "mockingoose"
import {queryUserByName} from "./query"
import User from "./User"

test("Test find user by name", async () => {
  mockingoose.User.toReturn([{name: "David", email: "david.ng.dev@gmail.com"}], "find")
  const result = queryUserByName("David")
  const callParams = User.Query.prototype["find"].mock.calls[1][0]
  expect(callParams).toEqual({name: David})
})

Although this work but this one is undocumented way.

May be direct expose jest fn or add it to README.md ?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:15 (9 by maintainers)

github_iconTop GitHub Comments

2reactions
andreialecucommented, Mar 20, 2019

One thing to keep in mind is that the approach in the top level post is wrong in a way, which #50 addresses correctly.

You can have a query like: queryUserByName = (name) => user.find().where('name').equals(name);

In this case the mocked find() call will not have the right parameters of the query, so the OP’s code to check for them via mocks won’t work. The conditions will be undefined, as the find mock is called at the wrong time.

The PR considers this scenario and properly exposes the Query object directly, which will be populated properly at the point that the query is (mockingly) sent to mongo.

1reaction
stiegcommented, Mar 8, 2019

Hey @alonronin I have posted https://github.com/alonronin/mockingoose/pull/49. Have a look when you have time. Cheers.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Mock Functions - Jest
Mock functions allow you to test the links between code by erasing the actual implementation of a function, capturing calls to the function...
Read more >
Mocking Function with jest.mock in jest - typescript
Inside your mock you can use jest.fn() to create mocked functions. You can then use these functions to mock the results you need...
Read more >
Jest Testing Tutorial: 5 Easy Steps - Testim Blog
We explain further in this post how Jest mocking works ... place the test value in the expect keyword and call the exposed...
Read more >
Jest Testing like a Pro - Tips and tricks
We recently switch to Jest framework when writing Unit and ... If you are a 100% Mock Advocate, I really suggest reading Mocking...
Read more >
cannot spy the default property because it is not a function
For lazily evaluated spied functions it's easier to expose them as variables: const mockLogAppOpen = jest.fn(); jest.mock('@react-native-firebase/analytics' ...
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