

#JS BENCHMARK TESTING WITH SETTIMEOUT UPDATE#
If updating state that relies on the old state, you should instead use the state update callback instead, e.g.useEffect without a dependency array will cleanup and create a new effect every render, making the use of setInterval act more like setTimout (it'll only fire once before chatting cleaned up and a new interval is created).You don't need eFakeTimers() at all if you're using our async utils.Rerenders occur when the hook updates it's state, like setTimeOnApp in your example. The render it waits for is the render of the test component we render under the hood, so it does not matter whet the hook returns.Older versions of the library required a rerender to test for the value changes, but newer versions have interval checks now as well, i.e it tests the condition if the hook rerenders or at the next interval, whichever comes first.It checks I haven't looked closely at or ran your code, but a few things from just reading you comment: I'm fairly new to unit testing, so would appreciate any rectification surrounding my findings. nAllTimers() seems to do nothing in this case, instead initiate the use of faketimers and let your interval event inside the useEffect do the rest.The hook returns a number, not jsx, therefore this never fires and times out. the original poster was using await waitForNextUpdate() which i believe is looking for a rerender.It seems the key things to take away from this are: My next thought was that I could use one of the other async utils, waitForValueToChange to periodically test for to change and throw a nAllTimers()` in the callback to allow the timeout to fire in between checks, like so:Ĭonst ) Node.js supports the following Web Performance APIs: High Resolution Time. My initial reaction, was oh, that's easy, I'll just wait first for the promise first, then run the timers, but unfortunately this also doesn't work because there is not setState or other render trigger between awaiting the promise and setting the timeout, so again, the test times out waiting. This module provides an implementation of a subset of the W3C Web Performance APIs as well as additional APIs for Node.js-specific performance measurements. The deadlock occurs here because waitForNextUpdate does not resolve until the next render of the hook, and the set timeout wont fire until you call nAllTimers(), which has already been and gone because the promise causes it to miss a beat. nAllTimers() called (no-op - timer hasn't started).Your test follows the following sequence of events: boilerplate block (code will executed before every block and is part of the benchmark. javascript - Jest unit test: setTimeout not firing in async test - Stack Overflow Jest unit test: setTimeout not firing in async test Ask Question Asked 4 years, 9 months ago Modified 1 year, 1 month ago Viewed 52k times 15 I'm trying to understand how asynchronous testing works in Jest. it will be run before every test, and is not part of the benchmark.) 1. 1000), removing the fake timers and just letting the waitForNextUpdate do it's thing allows the test to pass (albeit after a second of waiting), so I'll work on the understanding that using a mocked timer is important. Setup block (useful for function initialization. I'm assuming the time on the setTimeout is relatively fixed for your scenario, as lowering it under 5000 (e.g. It basically boils down to when waitForNextUpdate resolves vs.
