-
-
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
Use Lolex as implementation for fake timers #5165
Comments
Looked a tiny bit more (still no code written).
*beyond the fact it's a breaking change |
In any case, we can help from the lolex side :) |
You learn something every day :D (it's correct in Jest's docs, I just skimmed it a bit too quick when trying to wrap my head around how we mock timers). |
I’d love this, especially since it will also allow mocking for |
This sounds exciting. I can provide data about FB in about 10 days but not earlier. It’s important we retain all useful functionality. FakeTimers also exposes some legacy globals which I’d be happy to kill completely and codemod internally at FB. At FB, we overwrite native Promise with the promise npm module, which uses asap, which uses setImmediate. This allows us to drive Promises using fake timers, which we used to do in the days before awesome async support in Jest. We need to make sure that a new fake timers implementation will retain the possibility to do this as we are unable to codemod all these calls automatically and probably have a couple hundred test suites depend on this behavior. Shall we move forward with a PR to see how far we can get and what kind of changes would be required to lolex? The most important invariants are that it doesn’t regress performance at all and that the size of dependencies/modules stays mostly the same. |
I have some done locally that I can push, but "from all of us to all of you" started on the tv, so I got distracted :D |
Hey, thanks @cpojer for weighing in:
FWIW, you can overwrite native We might be able to add support for such scheduling in core (Node.js) - it sounds like a pretty compelling use case (controlling the order of things firing) which might be a better long term solution for async/await. |
My branch with a single WIP commit https://github.com/SimenB/jest/tree/lolex |
Another difference is in how I think I prefer Lolex' behavior, as it more closely simulates what happens in a real environment, but it's a difference nonetheless. |
Opened up a PR with my branch, as I've pushed some more, and commenting on a branch is pretty weird: #5171. Not sure if it makes sense to keep this issue open or move the discussion to that PR. |
You can alternatively overwrite native asap may decide not to use I also intend to bring this use case up at the upcoming Node.js collaborator summit if that's OK for something I'd like native promises to let you do, would that be fine with you? |
What is blocking this PR currently? |
2.5 years later... 🙌 |
@SimenB thank you! I was waiting for this for quite a while now! Quick question (as the docs are a bit new and I can't find this information anywhere): I know that sinon fake timers offers you the chance to choose what you want to mock (for example, mocking just Date, but not This was very useful as some third party libraries like Would something like that be possible with jest fake timers? |
@Tinadim there's no official way provided by Jest, but you can still use the API directly for advanced use cases I'd think https://github.com/sinonjs/fake-timers/tree/cb4c2f6e2eaddfd2c1be49f14e7f6d51f6633280#var-clock--faketimersinstallconfig |
Do you want to request a feature or report a bug?
Feature, I suppose.
What is the current behavior?
We currently implement fake timers ourselves. See docs and implementation.
If the current behavior is a bug, please provide the steps to reproduce and
either a repl.it demo through https://repl.it/languages/jest or a minimal
repository on GitHub that we can
yarn install
andyarn test
.N/A
What is the expected behavior?
Our current implementation has a couple of holes (see #3465 and #5147) both of which are covered by Lolex.
I think we would benefit from basically proxying Lolex through Jest's API to keep the "batteries included" feature.
I hit one issue when I spent some minutes looking into it to see if it was feasible while still keeping Jest's API mostly the same, sinonjs/fake-timers#146. Might be other issues, I haven't dug too deeply into it.
Please provide your exact Jest configuration and mention your Jest, node,
yarn/npm version and operating system.
N/A
The text was updated successfully, but these errors were encountered: