Skip to content

Commit 06931b1

Browse files
authored
test: use expect.poll instead of untilUpdated (#607)
1 parent 5de85f6 commit 06931b1

File tree

8 files changed

+160
-168
lines changed

8 files changed

+160
-168
lines changed

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

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import {
77
editFile,
88
getColor,
99
isBuild,
10+
isServe,
1011
page,
1112
untilBrowserLogAfter,
12-
untilUpdated,
1313
viteServer,
1414
} from '~utils'
1515

@@ -125,8 +125,8 @@ test('css', async () => {
125125
} else {
126126
// During dev, the CSS is loaded from async chunk and we may have to wait
127127
// when the test runs concurrently.
128-
await untilUpdated(() => getColor('h1'), 'green')
129-
await untilUpdated(() => getColor('.jsx'), 'blue')
128+
await expect.poll(() => getColor('h1')).toMatch('green')
129+
await expect.poll(() => getColor('.jsx')).toMatch('blue')
130130
}
131131
})
132132

@@ -165,25 +165,30 @@ test('hydration', async () => {
165165
expect(await page.textContent('button')).toMatch('1')
166166
})
167167

168-
test('hmr', { retry: 3 }, async () => {
168+
test.runIf(isServe)('hmr', { retry: 3 }, async () => {
169169
// This is test is flaky in Mac CI, but can't be reproduced locally. Wait until
170170
// network idle to avoid the issue. TODO: This may be caused by a bug when
171171
// modifying a file while loading, we should remove this guard
172172
await page.goto(url, { waitUntil: 'networkidle' })
173173
editFile('src/pages/Home.vue', (code) => code.replace('Home', 'changed'))
174-
await untilUpdated(() => page.textContent('h1'), 'changed')
174+
await expect.poll(() => page.textContent('h1')).toMatch('changed')
175175
})
176176

177177
test('client navigation', async () => {
178178
await untilBrowserLogAfter(() => page.goto(url), 'hydrated')
179179

180-
await untilUpdated(() => page.textContent('a[href="/test/about"]'), 'About')
180+
await expect
181+
.poll(() => page.textContent('a[href="/test/about"]'))
182+
.toMatch('About')
181183
await page.click('a[href="/test/about"]')
182-
await untilUpdated(() => page.textContent('h1'), 'About')
184+
await expect.poll(() => page.textContent('h1')).toMatch('About')
185+
186+
if (isBuild) return
187+
183188
editFile('src/pages/About.vue', (code) => code.replace('About', 'changed'))
184-
await untilUpdated(() => page.textContent('h1'), 'changed')
189+
await expect.poll(() => page.textContent('h1')).toMatch('changed')
185190
await page.click('a[href="/test/"]')
186-
await untilUpdated(() => page.textContent('a[href="/test/"]'), 'Home')
191+
await expect.poll(() => page.textContent('a[href="/test/"]')).toMatch('Home')
187192
})
188193

189194
test('import.meta.url', async () => {

playground/tailwind-v3/__tests__/tailwind.spec.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import {
55
isServe,
66
page,
77
untilBrowserLogAfter,
8-
untilUpdated,
98
} from '~utils'
109

1110
test.runIf(isServe)('regenerate CSS and HMR (pug template)', async () => {
@@ -23,5 +22,5 @@ test.runIf(isServe)('regenerate CSS and HMR (pug template)', async () => {
2322
],
2423
false,
2524
)
26-
await untilUpdated(() => getBgColor(el), 'rgb(220, 38, 38)')
25+
await expect.poll(() => getBgColor(el)).toMatch('rgb(220, 38, 38)')
2726
})

playground/tailwind/__tests__/tailwind.spec.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import {
55
isServe,
66
page,
77
untilBrowserLogAfter,
8-
untilUpdated,
98
} from '~utils'
109

1110
test.runIf(isServe)('regenerate CSS and HMR (pug template)', async () => {
@@ -23,5 +22,5 @@ test.runIf(isServe)('regenerate CSS and HMR (pug template)', async () => {
2322
],
2423
false,
2524
)
26-
await untilUpdated(() => getBgColor(el), 'oklch(0.577 0.245 27.325)')
25+
await expect.poll(() => getBgColor(el)).toMatch('oklch(0.577 0.245 27.325)')
2726
})

playground/test-utils.ts

Lines changed: 1 addition & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { normalizePath } from 'vite'
1010
import { fromComment } from 'convert-source-map'
1111
import { expect } from 'vitest'
1212
import type { ResultPromise as ExecaResultPromise } from 'execa'
13-
import { isBuild, isWindows, page, testDir } from './vitestSetup'
13+
import { isWindows, page, testDir } from './vitestSetup'
1414

1515
export * from './vitestSetup'
1616

@@ -67,8 +67,6 @@ function rgbToHex(rgb: string): string | undefined {
6767
return undefined
6868
}
6969

70-
const timeout = (n: number) => new Promise((r) => setTimeout(r, n))
71-
7270
async function toEl(el: string | ElementHandle): Promise<ElementHandle> {
7371
if (typeof el === 'string') {
7472
return await page.$(el)
@@ -99,9 +97,7 @@ export function readFile(filename: string): string {
9997
export function editFile(
10098
filename: string,
10199
replacer: (str: string) => string,
102-
runInBuild: boolean = false,
103100
): void {
104-
if (isBuild && !runInBuild) return
105101
filename = path.resolve(testDir, filename)
106102
const content = fs.readFileSync(filename, 'utf-8')
107103
const modified = replacer(content)
@@ -148,46 +144,6 @@ export function readManifest(base = ''): Manifest {
148144
)
149145
}
150146

151-
/**
152-
* Poll a getter until the value it returns includes the expected value.
153-
*/
154-
export async function untilUpdated(
155-
poll: () => string | Promise<string>,
156-
expected: string,
157-
runInBuild = false,
158-
): Promise<void> {
159-
if (isBuild && !runInBuild) return
160-
const maxTries = process.env.CI ? 200 : 50
161-
for (let tries = 0; tries < maxTries; tries++) {
162-
const actual = (await poll()) ?? ''
163-
if (actual.indexOf(expected) > -1 || tries === maxTries - 1) {
164-
expect(actual).toMatch(expected)
165-
break
166-
} else {
167-
await timeout(50)
168-
}
169-
}
170-
}
171-
172-
/**
173-
* Retry `func` until it does not throw error.
174-
*/
175-
export async function withRetry(
176-
func: () => Promise<void>,
177-
runInBuild = false,
178-
): Promise<void> {
179-
if (isBuild && !runInBuild) return
180-
const maxTries = process.env.CI ? 200 : 50
181-
for (let tries = 0; tries < maxTries; tries++) {
182-
try {
183-
await func()
184-
return
185-
} catch {}
186-
await timeout(50)
187-
}
188-
await func()
189-
}
190-
191147
type UntilBrowserLogAfterCallback = (logs: string[]) => PromiseLike<void> | void
192148

193149
export async function untilBrowserLogAfter(

playground/vue-jsx/__tests__/vue-jsx.spec.ts

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, 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('.named')).toMatch('0')
@@ -38,7 +38,9 @@ describe.runIf(isServe)('vue-jsx server', () => {
3838
editFile('Comps.jsx', (code) =>
3939
code.replace('named {count', 'named updated {count'),
4040
)
41-
await untilUpdated(() => page.textContent('.named'), 'named updated 0')
41+
await expect
42+
.poll(() => page.textContent('.named'))
43+
.toMatch('named updated 0')
4244

4345
// affect all components in same file
4446
expect(await page.textContent('.named-specifier')).toMatch('1')
@@ -51,10 +53,9 @@ describe.runIf(isServe)('vue-jsx server', () => {
5153
editFile('Comps.jsx', (code) =>
5254
code.replace('named specifier {count', 'named specifier updated {count'),
5355
)
54-
await untilUpdated(
55-
() => page.textContent('.named-specifier'),
56-
'named specifier updated 1',
57-
)
56+
await expect
57+
.poll(() => page.textContent('.named-specifier'))
58+
.toMatch('named specifier updated 1')
5859

5960
// affect all components in same file
6061
expect(await page.textContent('.default')).toMatch('2')
@@ -66,7 +67,9 @@ describe.runIf(isServe)('vue-jsx server', () => {
6667
editFile('Comps.jsx', (code) =>
6768
code.replace('default {count', 'default updated {count'),
6869
)
69-
await untilUpdated(() => page.textContent('.default'), 'default updated 2')
70+
await expect
71+
.poll(() => page.textContent('.default'))
72+
.toMatch('default updated 2')
7073

7174
// should not affect other components on the page
7275
expect(await page.textContent('.default-tsx')).toMatch('4')
@@ -80,10 +83,9 @@ describe.runIf(isServe)('vue-jsx server', () => {
8083
editFile('Comp.tsx', (code) =>
8184
code.replace('default tsx {count', 'default tsx updated {count'),
8285
)
83-
await untilUpdated(
84-
() => page.textContent('.default-tsx'),
85-
'default tsx updated 3',
86-
)
86+
await expect
87+
.poll(() => page.textContent('.default-tsx'))
88+
.toMatch('default tsx updated 3')
8789

8890
// should not affect other components on the page
8991
expect(await page.textContent('.named')).toMatch('1')
@@ -93,7 +95,9 @@ describe.runIf(isServe)('vue-jsx server', () => {
9395
editFile('Script.vue', (code) =>
9496
code.replace('script {count', 'script updated {count'),
9597
)
96-
await untilUpdated(() => page.textContent('.script'), 'script updated 4')
98+
await expect
99+
.poll(() => page.textContent('.script'))
100+
.toMatch('script updated 4')
97101

98102
expect(await page.textContent('.src-import')).toMatch('6')
99103
})
@@ -103,10 +107,9 @@ describe.runIf(isServe)('vue-jsx server', () => {
103107
editFile('SrcImport.jsx', (code) =>
104108
code.replace('src import {count', 'src import updated {count'),
105109
)
106-
await untilUpdated(
107-
() => page.textContent('.src-import'),
108-
'src import updated 5',
109-
)
110+
await expect
111+
.poll(() => page.textContent('.src-import'))
112+
.toMatch('src import updated 5')
110113

111114
expect(await page.textContent('.script')).toMatch('5')
112115
})
@@ -115,6 +118,6 @@ describe.runIf(isServe)('vue-jsx server', () => {
115118
editFile('setup-syntax-jsx.vue', (code) =>
116119
code.replace('let count = ref(100)', 'let count = ref(1000)'),
117120
)
118-
await untilUpdated(() => page.textContent('.setup-jsx'), '1000')
121+
await expect.poll(() => page.textContent('.setup-jsx')).toMatch('1000')
119122
})
120123
})
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { test } from 'vitest'
2-
import { getBg, untilUpdated } from '~utils'
1+
import { expect, test } from 'vitest'
2+
import { getBg } from '~utils'
33

44
test('vue legacy assets', async () => {
5-
await untilUpdated(() => getBg('.main'), 'assets/asset', true)
5+
await expect.poll(() => getBg('.main')).toMatch('assets/asset')
66
})
77

88
test('async vue legacy assets', async () => {
9-
await untilUpdated(() => getBg('.module'), 'assets/asset', true)
9+
await expect.poll(() => getBg('.module')).toMatch('assets/asset')
1010
})

0 commit comments

Comments
 (0)