Loading
Current section: Assertions 7 exercises
Problem

Retryable Assertions

Use Vitest’s expect.poll() for retryable assertions, replacing vi.waitFor() to test eventual asynchronous states more cleanly.

Loading exercise

Transcript

00:00 When testing asynchronous code, your application arrives at the expected state eventually, and you have to take that eventuality into account to have reliable tests. So when you're working with promise-based APIs like Fetch, it's really great to use async and await syntax to make sure that you wait for this request to complete.

00:17 You can also utilize the built-in resolves and rejects chaining in Vita's expect function to await certain promises, like here the response promise. By chaining it this way instead of awaiting this promise directly, you make that promise resolution a part of your expectation, and it will produce much nicer error messages as well.

00:36 But what if your tested system doesn't give you any means, any promises to await? If you just express your intention and tests immediately, well, this is a sure recipe for flakiness because there is no guarantee that from this point and to this point, your application actually transitions into the right state.

00:53 This is where you usually reach out to utilities like v.waitfor to have some sort of predictability in your test suite, because calling v.waitfor will return you a promise that will resolve once the given callback here resolves. Using waitfor and waituntil are great approaches

01:09 to tackling asynchronicity, but there's actually one more API you can use, and I find it to be so elegant you're going to love it. In this exercise, your task will be to refactor an existing test suite from the waitfor utility to something called retriable assertions. And of course, make sure that the tests are passing at the end. So give it your best and see you once you're done.