Skip to content
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

Closed
acostalima opened this issue Jan 22, 2018 · 5 comments
Closed

Comments

@acostalima
Copy link

acostalima commented Jan 22, 2018

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.

@SimenB
Copy link
Member

SimenB commented Jan 22, 2018

This seems like a perfect fit for a custom matcher. @mattphillips thoughts?

@mattphillips
Copy link
Contributor

mattphillips commented Jan 22, 2018

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 jest-extended with the API you'd like and we can work on getting this available for you 😄

@SimenB
Copy link
Member

SimenB commented Jan 22, 2018

Closing as per above comment. Thanks @mattphillips!

@SimenB SimenB closed this as completed Jan 22, 2018
@acostalima
Copy link
Author

acostalima commented Jan 23, 2018

@SimenB @mattphillips
Thanks for the quick reply!
The above proposal is looking good. I will open an issue in jest-extended so we can proceed this discussion. 😄

@github-actions
Copy link

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.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 13, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants