question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

RNTL accidentally triggers `jest.useFakeTimers`

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:10 (10 by maintainers)

github_iconTop GitHub Comments

2reactions
Niryocommented, Oct 2, 2022

yep, i’ll do it

1reaction
Niryocommented, Sep 28, 2022

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Timer Mocks - Jest
useFakeTimers () . This is replacing the original implementation of setTimeout() and other timer functions. Timers can be restored to their ...
Read more >
Migration to 11.0 | React Native Testing Library - Open Source
If you use fake timers in any of your tests you should update your Jest dependencies to ... Up to version 10, RNTL...
Read more >
How do I set a mock date in Jest? - Stack Overflow
jest .useFakeTimers() .setSystemTime(new Date('2020-01-01')); ... to the original objects, which can be accidentally overwritten in some other code.
Read more >
How to use jest.useFakeTimers('modern') and jest ...
useFakeTimers ('modern') and jest.setSystemTime() but I don't know why it breaks my test. Does anyone know how to work with these two helpers?...
Read more >
Test Timing-Based JS Functions with Jest - DEV Community ‍ ‍
Tell Jest to use fake timers for this test file. Tell Jest to advance timers by enough for the setTimeout to be triggered....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found