-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
Reinstate timestamps for mock calls #6672
Comments
Not sure about this one. What if people want a timestamp for when it returned instead of invoked? Or if it's a promise, when it settled? Checking the order of invocations makes sense to me (error handler invoked before logger, or whatever). Not sure about how close to mocks were called. We don't want a too big of an API surface. If you really want that, you could in theory implement it as you |
@thymikee @rickhanlonii thoughts? Also @UselessPickles since you've been working on the mocks lately 🙂 |
It would be fairly simple to update the mock code to record 2 timestamps per call:
There's already a convenient structure containing the "result" data where we could also store the time of completion. But the current structure of the rest of the "mock" object would require that we add yet another parallel array to contain invocation/call timestamps. It would be nice to combine all of the parallel arrays into a single array of "mock call" objects some day, but that's a big breaking change. So my thoughts are that it's simple and low risk to implement, so it might be a worthwhile inclusion if it helps others write custom matchers that analyze the relationships between different method calls. |
...but the real question is how we obtain the the "now" timestamp. I haven't fully researched this, but I'm vaguely aware of some options:
Also note that performance.now() and the NodeJS process timestamp are NOT real date-time timestamps. They start at zero at the beginning of execution and count how long the process has been running. Is that a problem? Will anyone expect these timestamps to be absolute timestamps, or is it OK for them to be relative to the beginning of the process? |
|
@UselessPickles @SimenB Yep, that's it. At least for the use case I presented, timestamps relative to the beginning of the process will do. The package convert-hrtime may prove itself useful for this. |
@UselessPickles @SimenB what's the way forward? |
Ping @UselessPickles @SimenB |
I think we need this PR merged first, because it includes changes to related code/structures: #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 |
I wonder if we should have type MockInvocation = {
call: { args: Array<any> },
result: { type: 'return' | 'throw', value: any },
// Drop it, maybe? #7070
instance: any
}; Then we could in theory add some more stuff @thymikee @rickhanlonii thoughts? |
I don't see any reason to nest Also, a slight correction on the type of
|
I like it! Do we want a real timestamp, or just something relative to process startup? Depends on if we want higher precision than milliseconds |
Looks like this is stalled (sorry, I've been afk) Can we wrap it up for Jest 24? What do we need? |
You suggested using And there seemed to be agreement previously that time relative to process startup is sufficient.
Updating the mock code itself to use the structures suggested in my previous comment would be easy. The harder parts are:
I could probably update the mock code and structures itself to help get it started, but I don't think I'll have time to deal with all the other "harder parts" I listed. My free time this winter is consumed by a project car. |
SGTM.
Can mock whatever module we use for timing and increment with |
This issue is stale because it has been open for 1 year with no activity. Remove stale label or comment or this will be closed in 14 days. |
Oh.. That is beautiful. Let’s not stale yet. I have noticed one funny issue with return types of |
This issue is stale because it has been open for 1 year with no activity. Remove stale label or comment or this will be closed in 30 days. |
🚀 Feature Proposal
Record the timestamp of individual mock calls.
Motivation
I am well aware timestamps were removed in favor of
invocationCallOrder
(#5867). However, to implement a new matcher proposed in jest-community/jest-extended#112, timestamps are necessary. I am proposing to restore the implementation alongsideinvocationCallOrder
and, at the same time, address the concerns raised previously regarding the precision issues (jest-community/jest-extended#98 (comment), #4402 (comment)). @brigonzalez was already headed towards a possible solution for this resorting to process.hrtime() instead ofDate
.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.
The text was updated successfully, but these errors were encountered: