DEV Community

Discussion on: Testing setTimeout/setInterval

Collapse
 
cellog profile image
Gregory Beaver

The mocking of timings provided natively by Jest is fantastic, by calling jest.useFakeTimers the timeout functions become mocks and you can advance "time" by a fake number of milliseconds or run all pending timers or all timers to check that behavior works as expected. Of course, there are hidden gotchas, as you'll want to reset all mocks after each test runs but mostly it is easier than custom rolled timeout handling. Not to take away from your work - what you designed is basically what jest provides internally

Collapse
 
rfornal profile image
bob.ts

Awesome! Good to know.

There’s a similar “time” pattern in Jasmine, I believe. The issue here was a client with an existing code-base, including thousands of tests already in Jasmine. In fact, they prefer to only upgrade 3rd party code when it’s clearly broken ... which explains the odd pattern we worked out in this article.

Thanks again for the information. I may jump in and experiment with Jest one of these days!