Skip to content

Commit dbcd5a9

Browse files
authored
feat(preferred): add wait-for-expect replacements (#1109)
* feat(preferred): add wait-for-expect replacements * docs: clarify wait-for-expect migration guidance * docs: remove redundant wait-for-expect migration text --------- Co-authored-by: Gabriel Pereira Rodrigues <173825442+Creatixpy@users.noreply.github.com>
1 parent 6b92e31 commit dbcd5a9

2 files changed

Lines changed: 57 additions & 0 deletions

File tree

‎docs/modules/wait-for-expect.md‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
---
2+
description: Replace wait-for-expect with built-in assertion retries in Vitest or node:test
3+
---
4+
5+
# Replacements for `wait-for-expect`
6+
7+
Popular test runners such as Vitest and `node:test` provide built-in assertion retries, so projects using them can remove `wait-for-expect`.
8+
9+
## `vitest`
10+
11+
[`vi.waitFor()`](https://vitest.dev/api/vi.html#vi-waitfor) is available since Vitest 0.34.5 and accepts synchronous or asynchronous assertion callbacks.
12+
13+
It retries callbacks that throw or reject until they succeed or time out. Keep the assertion inside the callback and await the result; returning `false` alone does not trigger a retry.
14+
15+
```ts
16+
import waitForExpect from 'wait-for-expect' // [!code --]
17+
import { expect, test } from 'vitest' // [!code --]
18+
import { expect, test, vi } from 'vitest' // [!code ++]
19+
20+
test('service becomes ready', async () => {
21+
const checkReady = async () => {
22+
expect(await service.isReady()).toBe(true)
23+
}
24+
25+
await waitForExpect(checkReady, 4500, 50) // [!code --]
26+
await vi.waitFor(checkReady, { timeout: 4500, interval: 50 }) // [!code ++]
27+
})
28+
```
29+
30+
If you use `vi.useFakeTimers()`, `vi.waitFor()` automatically advances fake timers by the polling interval on each check. Review tests that previously advanced timers manually.
31+
32+
## `node:test`
33+
34+
[`t.waitFor()`](https://nodejs.org/api/test.html#contextwaitforcondition-options) is available on the test context since Node.js 22.14.0 and 23.7.0 and accepts synchronous or asynchronous assertion callbacks.
35+
36+
It retries callbacks that throw or reject until they succeed or time out. Keep the assertion inside the callback and await the result; returning `false` alone does not trigger a retry.
37+
38+
```ts
39+
import waitForExpect from 'wait-for-expect' // [!code --]
40+
import { test } from 'node:test'
41+
import assert from 'node:assert/strict'
42+
43+
test('service becomes ready', async (t) => {
44+
const checkReady = async () => {
45+
assert.equal(await service.isReady(), true)
46+
}
47+
48+
await waitForExpect(checkReady, 4500, 50) // [!code --]
49+
await t.waitFor(checkReady, { timeout: 4500, interval: 50 }) // [!code ++]
50+
})
51+
```

‎manifests/preferred.json‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2826,6 +2826,12 @@
28262826
"replacements": ["vue-markdown-render"],
28272827
"url": {"type": "e18e", "id": "vue3-markdown-it"}
28282828
},
2829+
"wait-for-expect": {
2830+
"type": "module",
2831+
"moduleName": "wait-for-expect",
2832+
"replacements": ["node:test", "vitest"],
2833+
"url": {"type": "e18e", "id": "wait-for-expect"}
2834+
},
28292835
"wellknown": {
28302836
"type": "module",
28312837
"moduleName": "wellknown",

0 commit comments

Comments
 (0)