RNTL accidentally triggers `jest.useFakeTimers`
Describe the bug
I am using sinon-fake-timers together with jest, because jest's exposes only minimal part of sinon timers and I need more advanced capabilities. The thing is that React native testing library mistakenly thinks that I am using "jest.useFakeTimers", because of this check:
if (
typeof globalObj.setTimeout.clock !== 'undefined' &&
// $FlowIgnore[prop-missing]
typeof jest.getRealSystemTime !== 'undefined'
)
The first statement is true because I am using sinon fake timers, and the second is true because I am using jest, so it assumes that I am using jest.useFakeTimers, and it automatically turn it on for me here:
if (fakeTimersType) {
jest.useFakeTimers(fakeTimersType);
}
This results in both custom sinon timer and jest's fake timers being installed, and it causes issues in my tests.
Expected behavior
jest.useFakeTimers should not be called if the user didn't call it explicitly.
Steps to Reproduce
use jest 27 and sinon-fake-timers
Versions
npmPackages: @testing-library/react-native: ^9.0.0 => 9.0.0 react-test-renderer: ^17.0.2 => 17.0.2
@Niryo can you check if the issue persists with current version of RNTL with Jest 29?
Yea, I'll check it later this week
@mdjastrzebski yep, it's still an issue
@Niryo thanks for checking it. BTW Jest recently (v27 or v28) added more features to its fake timers implementation. Does sinon-fake-timers still has some features that Jest does not?
@mdjastrzebski jest still exposes only a subset of sinon's options
@Niryo The recommended workaround for your specific use case is to you Patch Package locally in order to tweak RNTL behavior to your needs.
You use cases seems quite rare, and relevant only for advanced users that need more fake timer features. Such users should be able to make relevant changes using patch package. I am wary of altering the default behavior for majority of the users.
We source our Jest timer detection code from Dom Testing Library, so it seems that RTL/DTL would also have the same behavior.
Alternative option of introducing some new API for controlling fake timer behavior seems to add too much complexity for primary use cases. If you feel that you can come up with simple API that would solve your problem without affecting other users by default and without complicating our API too much feel free to submit a PR for such change.
I agree that it's a rare use case. what about introducing an env param? you already have this RNTL_SKIP_AUTO_CLEANUP, I can add another one like RNTL_SKIP_SETTING_TIME_MOCKS.
Sounds like a reasonable solution that would not complicate the API surface for regular users.
@Niryo would you be able to submit a PR for that?
yep, i'll do it
https://github.com/callstack/react-native-testing-library/pull/1169