Skip to content

RNTL accidentally triggers jest.useFakeTimers #886

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

Closed
Niryo opened this issue Dec 29, 2021 · 10 comments
Closed

RNTL accidentally triggers jest.useFakeTimers #886

Niryo opened this issue Dec 29, 2021 · 10 comments

Comments

@Niryo
Copy link
Contributor

Niryo commented Dec 29, 2021

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 Niryo changed the title Bad check for jest fake timers RNTL accidentally triggers jest.useFakeTimers Dec 29, 2021
@mdjastrzebski
Copy link
Member

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

@Niryo
Copy link
Contributor Author

Niryo commented Sep 26, 2022

Yea, I'll check it later this week

@Niryo
Copy link
Contributor Author

Niryo commented Sep 28, 2022

@mdjastrzebski yep, it's still an issue

@mdjastrzebski
Copy link
Member

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

@Niryo
Copy link
Contributor Author

Niryo commented Sep 28, 2022

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

@mdjastrzebski
Copy link
Member

@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 mdjastrzebski closed this as not planned Won't fix, can't repro, duplicate, stale Sep 30, 2022
@Niryo
Copy link
Contributor Author

Niryo commented Oct 2, 2022

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.

@mdjastrzebski
Copy link
Member

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?

@Niryo
Copy link
Contributor Author

Niryo commented Oct 2, 2022

yep, i'll do it

@Niryo
Copy link
Contributor Author

Niryo commented Oct 11, 2022

#1169

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants