Skip to content

Commit 18e89ce

Browse files
authored
test: use expect.poll instead of untilUpdated (#511)
1 parent dd5bd78 commit 18e89ce

File tree

11 files changed

+48
-82
lines changed

11 files changed

+48
-82
lines changed
Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
import { expect, test } from 'vitest'
2-
import {
3-
editFile,
4-
isServe,
5-
page,
6-
untilBrowserLogAfter,
7-
untilUpdated,
8-
} from '~utils'
2+
import { editFile, isServe, page, untilBrowserLogAfter } from '~utils'
93

104
test('should render', async () => {
115
expect(await page.textContent('span')).toMatch('Hello World')
@@ -18,13 +12,17 @@ if (isServe) {
1812
() => page.textContent('span'),
1913
'[vite] hot updated: /src/App.tsx',
2014
)
21-
await untilUpdated(() => page.textContent('span'), 'Hello class components')
15+
await expect
16+
.poll(() => page.textContent('span'))
17+
.toMatch('Hello class components')
2218

2319
editFile('src/utils.tsx', (code) => code.replace('Hello', 'Hi'))
2420
await untilBrowserLogAfter(
2521
() => page.textContent('span'),
2622
'[vite] hot updated: /src/App.tsx',
2723
)
28-
await untilUpdated(() => page.textContent('span'), 'Hi class components')
24+
await expect
25+
.poll(() => page.textContent('span'))
26+
.toMatch('Hi class components')
2927
})
3028
}

playground/compiler-react-18/__tests__/compiler-react-18.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { expect, test } from 'vitest'
2-
import { editFile, isServe, page, untilUpdated } from '~utils'
2+
import { editFile, isServe, page } from '~utils'
33

44
test('should render', async () => {
55
expect(await page.textContent('button')).toMatch('count is 0')
@@ -11,5 +11,5 @@ test.runIf(isServe)('should hmr', async () => {
1111
editFile('src/App.tsx', (code) =>
1212
code.replace('count is {count}', 'count is {count}!'),
1313
)
14-
await untilUpdated(() => page.textContent('button'), 'count is 1!')
14+
await expect.poll(() => page.textContent('button')).toMatch('count is 1!')
1515
})

playground/compiler/__tests__/compiler.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { expect, test } from 'vitest'
2-
import { editFile, isServe, page, untilUpdated } from '~utils'
2+
import { editFile, isServe, page } from '~utils'
33

44
test('should render', async () => {
55
expect(await page.textContent('button')).toMatch('count is 0')
@@ -11,5 +11,5 @@ test.runIf(isServe)('should hmr', async () => {
1111
editFile('src/App.tsx', (code) =>
1212
code.replace('count is {count}', 'count is {count}!'),
1313
)
14-
await untilUpdated(() => page.textContent('button'), 'count is 1!')
14+
await expect.poll(() => page.textContent('button')).toMatch('count is 1!')
1515
})

playground/hook-with-jsx/__tests__/hook-with-jsx.spec.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
import { expect, test } from 'vitest'
2-
import {
3-
editFile,
4-
isServe,
5-
page,
6-
untilBrowserLogAfter,
7-
untilUpdated,
8-
} from '~utils'
2+
import { editFile, isServe, page, untilBrowserLogAfter } from '~utils'
93

104
test('should render', async () => {
115
expect(await page.textContent('button')).toMatch('count is 0')
@@ -22,6 +16,6 @@ if (isServe) {
2216
() => page.textContent('button'),
2317
'[vite] hot updated: /src/App.tsx',
2418
)
25-
await untilUpdated(() => page.textContent('button'), 'count is 1!')
19+
await expect.poll(() => page.textContent('button')).toMatch('count is 1!')
2620
})
2721
}

playground/mdx/__tests__/mdx.spec.ts

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
import { expect, test } from 'vitest'
2-
import {
3-
editFile,
4-
isServe,
5-
page,
6-
untilBrowserLogAfter,
7-
untilUpdated,
8-
} from '~utils'
2+
import { editFile, isServe, page, untilBrowserLogAfter } from '~utils'
93

104
test('should render', async () => {
115
expect(await page.textContent('h1')).toMatch('Vite + MDX')
@@ -24,7 +18,7 @@ if (isServe) {
2418
() => page.textContent('h1'),
2519
'[vite] hot updated: /src/demo.mdx',
2620
)
27-
await untilUpdated(() => page.textContent('h1'), 'Updated')
21+
await expect.poll(() => page.textContent('h1')).toMatch('Updated')
2822
})
2923

3024
test('should hmr with .md extension', async () => {
@@ -35,9 +29,8 @@ if (isServe) {
3529
),
3630
'[vite] hot updated: /src/demo2.md',
3731
)
38-
await untilUpdated(
39-
() => page.getByText('.md extension hmr works.').textContent(),
40-
'.md extension hmr works. This is bold text.',
41-
)
32+
await expect
33+
.poll(() => page.getByText('.md extension hmr works.').textContent())
34+
.toMatch('.md extension hmr works. This is bold text.')
4235
})
4336
}

playground/react-classic/__tests__/react.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { expect, test } from 'vitest'
2-
import { editFile, isServe, page, untilUpdated, viteTestUrl } from '~utils'
2+
import { editFile, isServe, page, viteTestUrl } from '~utils'
33

44
test('should render', async () => {
55
expect(await page.textContent('h1')).toMatch('Hello Vite + React')
@@ -13,7 +13,7 @@ test('should update', async () => {
1313

1414
test.runIf(isServe)('should hmr', async () => {
1515
editFile('App.jsx', (code) => code.replace('Vite + React', 'Updated'))
16-
await untilUpdated(() => page.textContent('h1'), 'Hello Updated')
16+
await expect.poll(() => page.textContent('h1')).toMatch('Hello Updated')
1717
// preserve state
1818
expect(await page.textContent('button')).toMatch('count is: 1')
1919
})

playground/react-emotion/__tests__/react.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { expect, test } from 'vitest'
2-
import { editFile, getColor, isServe, page, untilUpdated } from '~utils'
2+
import { editFile, getColor, isServe, page } from '~utils'
33

44
test('should render', async () => {
55
expect(await page.textContent('h1')).toMatch(
@@ -17,13 +17,13 @@ test.runIf(isServe)('should hmr', async () => {
1717
editFile('App.jsx', (code) =>
1818
code.replace('Vite + React + @emotion/react', 'Updated'),
1919
)
20-
await untilUpdated(() => page.textContent('h1'), 'Hello Updated')
20+
await expect.poll(() => page.textContent('h1')).toMatch('Hello Updated')
2121

2222
editFile('Counter.jsx', (code) =>
2323
code.replace('color: #646cff;', 'color: #d26ac2;'),
2424
)
2525

26-
await untilUpdated(() => getColor('code'), '#d26ac2')
26+
await expect.poll(() => getColor('code')).toMatch('#d26ac2')
2727

2828
// preserve state
2929
expect(await page.textContent('button')).toMatch('count is: 1')
@@ -47,7 +47,7 @@ test('should update button style', async () => {
4747
code.replace('border: 2px solid #000', 'border: 4px solid red'),
4848
)
4949

50-
await untilUpdated(getButtonBorderStyle, '4px solid rgb(255, 0, 0)')
50+
await expect.poll(getButtonBorderStyle).toMatch('4px solid rgb(255, 0, 0)')
5151

5252
// preserve state
5353
expect(await page.textContent('button')).toMatch('count is: 1')

playground/react/__tests__/react.spec.ts

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {
66
isServe,
77
page,
88
untilBrowserLogAfter,
9-
untilUpdated,
109
viteTestUrl,
1110
} from '~utils'
1211

@@ -24,25 +23,26 @@ test.runIf(isServe)('should hmr', async () => {
2423
editFile('App.jsx', (code) =>
2524
code.replace('Vite + React', 'Vite + React Updated'),
2625
)
27-
await untilUpdated(() => page.textContent('h1'), 'Hello Vite + React Updated')
26+
await expect
27+
.poll(() => page.textContent('h1'))
28+
.toMatch('Hello Vite + React Updated')
2829
// preserve state
2930
expect(await page.textContent('#state-button')).toMatch('count is: 1')
3031

3132
editFile('App.jsx', (code) =>
3233
code.replace('Vite + React Updated', 'Vite + React'),
3334
)
34-
await untilUpdated(() => page.textContent('h1'), 'Hello Vite + React')
35+
await expect.poll(() => page.textContent('h1')).toMatch('Hello Vite + React')
3536
})
3637

3738
test.runIf(isServe)('should not invalidate when code is invalid', async () => {
3839
editFile('App.jsx', (code) =>
3940
code.replace('<div className="App">', '<div className="App"}>'),
4041
)
4142

42-
await untilUpdated(
43-
() => page.textContent('vite-error-overlay .message-body'),
44-
'Unexpected token',
45-
)
43+
await expect
44+
.poll(() => page.textContent('vite-error-overlay .message-body'))
45+
.toMatch('Unexpected token')
4646
// if import.meta.invalidate happened, the old page won't be shown because the page is reloaded
4747
expect(await page.textContent('h1')).toMatch('Hello Vite + React')
4848

@@ -90,7 +90,7 @@ if (!isBuild) {
9090
'Parent rendered',
9191
],
9292
)
93-
await untilUpdated(() => page.textContent('#parent'), 'Updated')
93+
await expect.poll(() => page.textContent('#parent')).toMatch('Updated')
9494
})
9595

9696
// #3301
@@ -122,10 +122,9 @@ if (!isBuild) {
122122
'Parent rendered',
123123
],
124124
)
125-
await untilUpdated(
126-
() => page.textContent('#context-provider'),
127-
'context provider updated',
128-
)
125+
await expect
126+
.poll(() => page.textContent('#context-provider'))
127+
.toMatch('context provider updated')
129128
})
130129

131130
test('should hmr files with "react/jsx-runtime"', async () => {
@@ -143,10 +142,9 @@ if (!isBuild) {
143142
),
144143
['[vite] hot updated: /hmr/jsx-import-runtime.js'],
145144
)
146-
await untilUpdated(
147-
() => page.textContent('#jsx-import-runtime'),
148-
'JSX import runtime updated',
149-
)
145+
await expect
146+
.poll(() => page.textContent('#jsx-import-runtime'))
147+
.toMatch('JSX import runtime updated')
150148

151149
expect(await page.textContent('#state-button')).toMatch('count is: 1')
152150
})

playground/ssr-react/__tests__/ssr-react.spec.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {
66
isBuild,
77
page,
88
untilBrowserLogAfter,
9-
untilUpdated,
109
viteTestUrl as url,
1110
} from '~utils'
1211

@@ -51,11 +50,11 @@ test('/', async () => {
5150
test.skipIf(isBuild)('hmr', async () => {
5251
await untilBrowserLogAfter(() => page.goto(url), 'hydrated')
5352

54-
await untilUpdated(() => page.textContent('h1'), 'Home')
53+
await expect.poll(() => page.textContent('h1')).toMatch('Home')
5554
editFile('src/pages/Home.jsx', (code) =>
5655
code.replace('<h1>Home', '<h1>changed'),
5756
)
58-
await untilUpdated(() => page.textContent('h1'), 'changed')
57+
await expect.poll(() => page.textContent('h1')).toMatch('changed')
5958

6059
// verify the change also affects next SSR
6160
const res = await page.reload()
@@ -65,14 +64,14 @@ test.skipIf(isBuild)('hmr', async () => {
6564
test('client navigation', async () => {
6665
await untilBrowserLogAfter(() => page.goto(url), 'hydrated')
6766

68-
await untilUpdated(() => page.textContent('a[href="/about"]'), 'About')
67+
await expect.poll(() => page.textContent('a[href="/about"]')).toMatch('About')
6968
await page.click('a[href="/about"]')
70-
await untilUpdated(() => page.textContent('h1'), 'About')
69+
await expect.poll(() => page.textContent('h1')).toMatch('About')
7170

7271
if (!isBuild) {
7372
editFile('src/pages/About.jsx', (code) =>
7473
code.replace('<h1>About', '<h1>changed'),
7574
)
76-
await untilUpdated(() => page.textContent('h1'), 'changed')
75+
await expect.poll(() => page.textContent('h1')).toMatch('changed')
7776
}
7877
})

playground/test-utils.ts

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ function rgbToHex(rgb: string): string {
3737
}
3838
}
3939

40-
const timeout = (n: number) => new Promise((r) => setTimeout(r, n))
41-
4240
async function toEl(el: string | ElementHandle): Promise<ElementHandle> {
4341
if (typeof el === 'string') {
4442
return await page.$(el)
@@ -84,25 +82,6 @@ export function removeFile(filename: string): void {
8482
fs.unlinkSync(path.resolve(testDir, filename))
8583
}
8684

87-
/**
88-
* Poll a getter until the value it returns includes the expected value.
89-
*/
90-
export async function untilUpdated(
91-
poll: () => string | Promise<string>,
92-
expected: string,
93-
): Promise<void> {
94-
const maxTries = process.env.CI ? 100 : 50
95-
for (let tries = 0; tries < maxTries; tries++) {
96-
const actual = (await poll()) ?? ''
97-
if (actual.indexOf(expected) > -1 || tries === maxTries - 1) {
98-
expect(actual).toMatch(expected)
99-
break
100-
} else {
101-
await timeout(50)
102-
}
103-
}
104-
}
105-
10685
type UntilBrowserLogAfterCallback = (logs: string[]) => PromiseLike<void> | void
10786

10887
export async function untilBrowserLogAfter(

0 commit comments

Comments
 (0)