jest.fn().waitUntilCalledNTimes() Feature
See original GitHub issue🚀 Feature Proposal
Add some kind to feature to jest to wait until a mock function has been called n times.
Motivation
We have an API that recursively sends periodic notifications to another service, we need to check that this works, we currently have a setTimeout wrapped in a promise to make the test wait for a period thats deffinetly longer than needed.
Example
test('some test', async () => {
const someMock = jest.fn()
await someMock.waitUntilCalledNTimes(n) // on the nth call, continue.
expect(someOtherMock).hasBeenCalled()
})
Pitch
Why does this feature belong in the Jest core platform?
It would be an addition to the core jest mocking ability, allowing for dynamic tests ensuring that a function has to of been called n times before moving on to check for its asssertions
Im happy to give this ago myself, if its something thats wanted
Issue Analytics
- State:
- Created 5 years ago
- Reactions:1
- Comments:19 (5 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 >Mock Functions or Spies Demystified - How Does jest.fn() Work?
Mock functions, also known as spies, are special functions that allow us to track how a particular function is called by external code....
Read more >Why isn't my jest mock function implementation being called?
When I check lambda1/index.js in the debugger getVehicleMetaKeysFromDeviceId() is a jest object but when it is called it doesn't use my mock ...
Read more >Understanding Jest Mocks - Medium
Learn about the Jest Mock Function and the different strategies ... The simplest way to create a Mock Function instance is with jest.fn()...
Read more >Mocking functions and modules with Jest - pawelgrzybek.com
Let's have a look at them all. Function mock using jest.fn(); Function mock using jest.spyOn(); Module mock using jest.mock() ...
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
Yeah that makes sense @benjaminkay93
I think this makes more sense in userland, as you said, it’s pretty niche
Here’s an example of how you could implement, let me know what you think
https://www.npmjs.com/package/wait-for-expect
Is that enough?