Skip to content

Instantly share code, notes, and snippets.

@rob-gordon
Last active September 1, 2022 16:04
Show Gist options
  • Save rob-gordon/7efd4a57757b0b2920afee70a315b743 to your computer and use it in GitHub Desktop.
Save rob-gordon/7efd4a57757b0b2920afee70a315b743 to your computer and use it in GitHub Desktop.
act() Testing Utils
// 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