Reinstate timestamps for mock calls
See original GitHub issue🚀 Feature Proposal
Record the timestamp of individual mock calls.
Motivation
I am well aware timestamps were removed in favor of invocationCallOrder
(https://github.com/facebook/jest/pull/5867). However, to implement a new matcher proposed in https://github.com/jest-community/jest-extended/issues/112, timestamps are necessary. I am proposing to restore the implementation alongside invocationCallOrder
and, at the same time, address the concerns raised previously regarding the precision issues (https://github.com/jest-community/jest-extended/issues/98#issuecomment-355440156, https://github.com/facebook/jest/issues/4402#issuecomment-375939427). @brigonzalez was already headed towards a possible solution for this resorting to process.hrtime() instead of Date
.
Example
One potential scenario for this feature is, by making use of the aforementioned matcher, to ensure a function is called in compliance with a given exponential back-off strategy.
Pitch
I believe this feature supports a legitimate use case and other may follow.
Issue Analytics
- State:
- Created 5 years ago
- Reactions:1
- Comments:18 (8 by maintainers)
Top GitHub Comments
I think we need this PR merged first, because it includes changes to related code/structures: https://github.com/facebook/jest/pull/6381
Once that is merged, I could easily create a PR to store invocation and completion timestamps, but it might be time to seriously think about restructuring the parallel arrays of information about mock calls. Without any restructuring, it would require adding yet another parallel array to store the invocation timestamps (which may be “good enough” for now to avoid serious breaking changes). I would like feedback from the Jest team on this before I put any work into it.
No matter what approach we take, it will technically be a breaking change to the mock serializer.
The completion timestamp would simply be a new property on the MockFunctionResult structure that would be undefined while the
type
property isincomplete
, then updated to a timestamp value when the function either returns or throws.process.hrtime
is that one 😃 we can use https://www.npmjs.com/package/browser-process-hrtime which should work in all envs we care about