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.

Allow fake timers other than jest

See original GitHub issue

Describe the feature you’d like:

This is an follow-on to https://github.com/testing-library/dom-testing-library/issues/939. The request is to support fake timers other than jest. For instance, jest uses @sinonjs/fake-timers under the hood, and yet it’s not possible to use @sinonjs/fake-timers directly with testing-library without jest. I would like to be able to use testing-library with @sinonjs/fake-timers without jest.

Suggested implementation:

@eps1lon had a good suggestion in https://github.com/testing-library/dom-testing-library/issues/939#issuecomment-830771708 to decouple testing-library from jest timers, and @MatanBobi suggested doing so in a way that can be set up in the configure step instead of requiring wrappers.

Describe alternatives you’ve considered:

I have tried to manually advance the clock with clock.tick(), and it works but causes act warnings.

Teachability, Documentation, Adoption, Migration Strategy:

I’m not certain what, if anything, will need to be done in user land to make this work. Ideally, it could be handled under-the-hood within this library, but I’m not sure how that could be done in a robust way.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:16 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
jwaltoncommented, Aug 3, 2021

According to this comment Jest fake timers are the only one supported.

I’m using jest + sinon timers, and everything was working fine for me up until the latest release.

This particular combination is a problem. dom-testing-library does some custom checking to see if jest timers are enabled (which will come back true in this case, even though jest timers are not enabled, because sinon will create a setTimeout.clock - any other fake timer library that creates a setTimeout.clock would have this same problem). This library will then make calls into jest fake timers, which will raise lots of warnings from jest since fake timers are not enabled.

0reactions
KamiKillertOcommented, Oct 6, 2022

@ChristopherChudzicki Thanks. I’ve tested with vi.useFakeTimers({ shouldAdvanceTime: true }); and it’s working perfectly.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Timer Mocks - Jest
In the following example we enable fake timers by calling jest.useFakeTimers() . This is replacing the original implementation of setTimeout() ...
Read more >
Unit Testing Beginners Guide - Part 2 - Spying and fake timers
You can create a mock function with jest.fn() . Simply put, a spy is another function that has built-in the ability to record...
Read more >
Testing Recipes - React
You can use fake timers only in some tests. Above, we enabled them by calling jest.useFakeTimers() . The main advantage they provide is...
Read more >
Using Fake Timers | Testing Library
In some cases, when your code uses timers ( setTimeout , setInterval , clearTimeout , clearInterval ), your tests may become unpredictable, slow ......
Read more >
Fake timers don't trigger setTimeout call correctly in Jest
Calling setTimeout properly on global window object fixed the issue. It didn't matter in actual app, but it's important for jest's fake ......
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