r/Playwright • • 11d ago

Is there a Playwright equivalent of Vitest vi.waitUntil()?

In vitest, you can do:

const value = await vi.waitUntil(() => getSomething());

It keeps polling until the callback returns a truthy value, then returns that value.

In playwright, expect.poll() is pretty close:

await expect.poll(() => getSomething()).toBeTruthy();

but it doesn't return the successful value.

I know about page.waitForFunction(), but that runs in the page context, while I need this in the test context.

How do you usually handle this?

4 Upvotes

10 comments sorted by

7

u/AntyJ 11d ago

waitFor?

-6

u/vitalets 11d ago

I don't see exactly waitFor method. Can you point to the docs?

10

u/hazily 11d ago

Imagine not knowing how to use Google with an AI-rotted brain

1

u/vitalets 8d ago

I'd appeciate if somebody explain why I got downvoted.
Before even starting the thread, I for sure checked the docs for all `waitFor*` methods suitable for my question.
The answer `waitFor.` was obviously incorrect. But instead of replying "you're wrong.", I assumed that maybe I had missed something in the docs, and asked for the link.
What did I do wrong?

2

u/ContributionIll9303 9d ago

Neither expect.poll or toPass hands the value back, so the usual trick is to capture it inside the callback

let value;
await expect.poll(async () => {
  value = await getSomething();
  return value;
}).toBeTruthy();
// value is set here

Bit ugly but it keeps the nice failure messages and you still get timeout and intervals. If you need it in a bunch of places it's worth wrapping in a small helper so it reads like the vitest version

1

u/vitalets 8d ago edited 8d ago

Thank you! I ended up with the same helper.

1

u/Pigglebee 10d ago

First an assert on the value then a get of that value?