-
Notifications
You must be signed in to change notification settings - Fork 467
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
Opt-out of Jest FakeTimers #939
Comments
I think the problem is also a bit larger than using fake timers. |
I'm more and more convinced that we should remove all the jest timer handling and replace with a timer API that people configure. Assuming jest is just not appropriate anymore with our reach and it causes us too often problems. Something like // test.js
beforeEach(() => {
domTestingLibrary.useFakeTimers(jest)
})
test(...);
afterEach(() => {
domTestingLibrary.useRealTimers()
})
// @testing-library/dom
interface TimersAdapter {
// whatever methods we currently use from jest
}
export function useFakeTimers(newTimersAdapter: TimersAdapter) {
timersAdapter = newTimersAdapter;
// This call is for convenience
timersAdapter.useFakeTimers();
}
export function useRealTimers() {
timersAdapter = noopTimers;
timersAdapter.useRealTimers();
} And then different timer libraries would implement their own We can then think about warning if we detect that timers were mocked but no call to |
I like the suggestion @eps1lon as it decouples DTL from jest. |
I agree that decoupling DTL from jest is a good idea, I'd prefer configuring the the test environment in the |
@timdeschryver - I filed #961, and I agree that the issue is essentially a dup of this one. I also like the suggestion made by @eps1lon, but if I am reading it correctly, it essentially outsources the problem to a family of new libraries - e.g. @testing-library/jest-modern-fake-timers-adapter, but I am curious whether these new adapters would still belong in the testing-library ecosystem (I suspect most people won't want to write their own). My main point is - if the intent is to split the logic off to separate adapters, would it make sense to harden the logic & fix things like #961 to the extent possible first & then do the split? If we make some more assumptions about the underlying timer implementation & essentially implement separate solutions for jest legacy & jest modern timers, I think #961 would not be that difficult to fix & it would likely solve the main issue raised here. (It just seems like it's going to become high priority to fix the issue, because Jest 27 is out & defaults to modern timers, but it might take a while to fully implement the solution @eps1lon is proposing) |
Am I correct in thinking that it's not possible to use @sinonjs/fake-timers directly, because testing-library only has special handling for jest? Jest is now using sinon's fake-timers, so it seems like it should work, but I'm having a very hard time getting |
@IanVS Dom Testing Library v8 removes the coupling with Jest (and also makes it compatible with Jest v27 and the modern timers). Closed by #966 |
Hi, @timdeschryver, thanks for the note. I see that the referenced PR did do some cleanup, but as far as I can tell it is still coupled to jest. See dom-testing-library/src/wait-for.js Line 52 in d347302
dom-testing-library/src/wait-for.js Line 74 in d347302
I tried version 8, but without success so far. |
@IanVS ah sorry, I've misunderstood the question/problem. Since it might have a big impact, I would suggest you to create a new issue for it. |
Hmmm, why does that need a new issue? Isn't it the whole point of this issue? It seems like a mistake to lose the context and subscribers from this current issue... I think this one was marked as closed incorrectly, as the PR you referenced did not actually solve the issue here. |
This issue was about jest fake timers. We never support any other fake timers so a new issue is warranted. |
OK thanks, I've opened #987. |
Describe the feature you'd like:
I had to pin the DTL version to 7.29.4 in ATL because some tests that were using
waitFor
andwaitForElementToBeRemoved
were failing (I think this was also the root cause for #901).This issue still persists to this date (I just tried to upgrade the DTL to its latest version).
After some time debugging, I think that the problem is that DTL falsely assumes that the Angular tests are using jest's fake timers. This is because Angular patches the timers API.
Suggested implementation:
I would like to add an option to the config to opt-out of this behavior since I don't see a better way to check if fake timers are used.
Describe alternatives you've considered:
Could we patch the
useFakeTimers
method, and use that to know if fake timers are used? This would replace the existing check.Teachability, Documentation, Adoption, Migration Strategy:
/
The text was updated successfully, but these errors were encountered: