react-native-testing-library icon indicating copy to clipboard operation
react-native-testing-library copied to clipboard

RNTL accidentally triggers `jest.useFakeTimers`

Open Niryo opened this issue 4 years ago • 10 comments

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 avatar Dec 29 '21 10:12 Niryo

@Niryo can you check if the issue persists with current version of RNTL with Jest 29?

mdjastrzebski avatar Sep 26 '22 12:09 mdjastrzebski

Yea, I'll check it later this week

Niryo avatar Sep 26 '22 13:09 Niryo

@mdjastrzebski yep, it's still an issue

Niryo avatar Sep 28 '22 08:09 Niryo

@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 avatar Sep 28 '22 09:09 mdjastrzebski

@mdjastrzebski jest still exposes only a subset of sinon's options

Niryo avatar Sep 28 '22 09:09 Niryo

@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.

mdjastrzebski avatar Sep 30 '22 12:09 mdjastrzebski

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.

Niryo avatar Oct 02 '22 07:10 Niryo

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?

mdjastrzebski avatar Oct 02 '22 08:10 mdjastrzebski

yep, i'll do it

Niryo avatar Oct 02 '22 09:10 Niryo

https://github.com/callstack/react-native-testing-library/pull/1169

Niryo avatar Oct 11 '22 12:10 Niryo