-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature: allow to assert time intervals with .toHaveBeenCalledTimes #5363
Comments
This seems like a perfect fit for a custom matcher. @mattphillips thoughts? |
Hey @SimenB this could certainly be a custom matcher in jest-extended. It sounds similar in implementation to the toHaveBeenCalledBefore matcher. We currently have an issue with recording the timestamp of when one or more mocks are invoked (potentially multiple times) during the same tick of the event loop they can arrive at the same timestamp value see. @acostalima this matcher can however be achieved still with the above issue, you would just have to make sure that the mock is actually asynchronous and await it to get differing timestamps. Do you have an example of how you would like the API to look? Off the top of my head would it be something like the following? const timeout = time => () => new Promise(res => setTimeout(res, time));
it('calls mock1 twice with no more than 50ms in between invocations', async () => {
const mock1 = jest.fn(timeout(10));
await mock1();
await mock1();
expect(mock1).toHaveBeenCalledBy([50]);
}); Feel free to open an issue for this in |
Closing as per above comment. Thanks @mattphillips! |
@SimenB @mattphillips |
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Do you want to request a feature or report a bug?
Feature
What is the current behavior?
.toHaveBeenCalledTimes
could be further enhanced to allow to expect successive calls to the same function to occur in given instants. This feature particularly useful in a scenario where one wants to ensure exponential back-off is working properly.What is the potential behavior?
.toHaveBeenCalledTimes
fails when the specified timing constraints are not fulfilled.Implementation
Possibly allow to pass in a second (optional) argument to
.toHaveBeenCalledTimes
that would be an array specifying, in milliseconds, the minimum time between each consecutive call to a mocked function. It should be fairly straight forward to implement this as it should be only necessary to record the timestamp for each call to the mocked function, and then compare the differences between the timestamps with the received array.The text was updated successfully, but these errors were encountered: