Burak Tokak
Burak Tokak

Reputation: 1860

Using setTimeout() in React and Testing it

I realized that I have been using setTimeout() in my project -maybe- more than I should, I did a quick research and scroll through some open source react projects on Github and decided I should ask about it.

I use setTimeout mostly for changing the classname or adding styles to animate the DOM objects also using CSS transition, one point I thought it will be hard to write unit testing with this many setTimeout.

Since I came from using AngularJS, there is a $timeout service which has the same functionality with setTimeout, but in testing you can flush the timeout like $timeout.flush() and the timeout ends instantly.

My questions;

Keep in mind I'm fairly new at reactjs and haven't write any unit test on it yet.

Upvotes: 5

Views: 19536

Answers (1)

MariuszJasinski
MariuszJasinski

Reputation: 504

It depends what you are using. There's a sinonjs fake timers (http://sinonjs.org/) and Jest also has this functionality (with an example): https://facebook.github.io/jest/docs/timer-mocks.html

Cheers, Mariusz

Upvotes: 4

Related Questions