Last active
September 1, 2022 16:04
-
-
Save rob-gordon/7efd4a57757b0b2920afee70a315b743 to your computer and use it in GitHub Desktop.
act() Testing Utils
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// From https://twitter.com/diegohaz/status/1560525455383461888 | |
// by Diego Haz @diegohaz | |
// Wait for all queueMicrotask() callbacks | |
export function flushMicrotasks() { | |
return act(() => Promise.resolve()); | |
} | |
// Wait for all requestAnimationFrame() callbacks | |
export function nextFrame() { | |
return act( | |
() => new Promise((resolve) => requestAnimationFrame(() => resolve())) | |
); | |
} | |
// Wait for setTimeout() callbacks | |
export async function sleep(ms: number) { | |
await act(() => new Promise((resolve) => setTimeout(resolve, ms))); | |
await nextFrame(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment