![]() ![]() As a convention, hook functions should start with use.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. I've never done any testing with jasmine, but I think I understand your problem.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 ) 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. The benchmarks creates a large table with randomized entries and measures the time for various operations including rendering duration. 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. This is a simple benchmark for several javascript frameworks. nAllTimers() called (no-op - timer hasn't started).Your test follows the following sequence of events: 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. Unfortunately we need to take some extra steps to actually get some results back Benchmark. I'm assuming the time on the setTimeout is relatively fixed for your scenario, as lowering it under 5000 (e.g. node bench.js Do this, and you’ll see the script takes some time to run and then returns nothing. It basically boils down to when waitForNextUpdate resolves vs. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |