Suggestion: Expose mocked jest function
See original GitHub issueCase: 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:
- Created 5 years ago
- Comments:15 (9 by maintainers)
Top 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 >
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
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 thefind
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.Hey @alonronin I have posted https://github.com/alonronin/mockingoose/pull/49. Have a look when you have time. Cheers.