Skip to content

Commit 6994d17

Browse files
authored
test: use expect.poll instead of withRetry/expectWithRetry/untilUpdated (vitejs#20206)
1 parent b13b7f5 commit 6994d17

File tree

41 files changed

+873
-1021
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+873
-1021
lines changed

playground/alias/__tests__/alias.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, getColor, isBuild, page, untilUpdated } from '~utils'
2+
import { editFile, getColor, isBuild, page } from '~utils'
33

44
test('fs', async () => {
55
expect(await page.textContent('.fs')).toMatch('[success] alias to fs path')
@@ -31,7 +31,7 @@ test('css via link', async () => {
3131
expect(await getColor('body')).toBe('grey')
3232
if (isBuild) return
3333
editFile('dir/test.css', (code) => code.replace('grey', 'red'))
34-
await untilUpdated(() => getColor('body'), 'red')
34+
await expect.poll(() => getColor('body')).toBe('red')
3535
})
3636

3737
test('optimized dep', async () => {

playground/assets/__tests__/assets.spec.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import {
1414
readFile,
1515
readManifest,
1616
serverLogs,
17-
untilUpdated,
1817
viteTestUrl,
1918
watcher,
2019
} from '~utils'
@@ -342,7 +341,7 @@ describe('css url() references', () => {
342341
editFile('nested/fragment-bg-hmr.svg', (code) =>
343342
code.replace('fill="blue"', 'fill="red"'),
344343
)
345-
await untilUpdated(() => getBg('.css-url-svg'), 'red')
344+
await expect.poll(() => getBg('.css-url-svg')).toMatch('red')
346345
}
347346
})
348347

@@ -360,7 +359,7 @@ describe('css url() references', () => {
360359
editFile('nested/fragment-bg-hmr2.svg', (code) =>
361360
code.replace('fill="blue"', 'fill="red"'),
362361
)
363-
await untilUpdated(() => getBg('.css-url-svg'), 'red')
362+
await expect.poll(() => getBg('.css-url-svg')).toMatch('red')
364363
}
365364
})
366365
})
@@ -663,11 +662,11 @@ test('inline style test', async () => {
663662

664663
if (!isBuild) {
665664
test('@import in html style tag hmr', async () => {
666-
await untilUpdated(() => getColor('.import-css'), 'rgb(0, 136, 255)')
665+
await expect.poll(() => getColor('.import-css')).toBe('rgb(0, 136, 255)')
667666
const loadPromise = page.waitForEvent('load')
668667
editFile('./css/import.css', (code) => code.replace('#0088ff', '#00ff88'))
669668
await loadPromise
670-
await untilUpdated(() => getColor('.import-css'), 'rgb(0, 255, 136)')
669+
await expect.poll(() => getColor('.import-css')).toBe('rgb(0, 255, 136)')
671670
})
672671
}
673672

playground/backend-integration/__tests__/backend-integration.spec.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import {
1212
readManifest,
1313
serverLogs,
1414
untilBrowserLogAfter,
15-
untilUpdated,
1615
} from '~utils'
1716

1817
test('should have no 404s', () => {
@@ -107,11 +106,11 @@ describe.runIf(isServe)('serve', () => {
107106
})
108107

109108
test('preserve the base in CSS HMR', async () => {
110-
await untilUpdated(() => getColor('body'), 'black') // sanity check
109+
await expect.poll(() => getColor('body')).toBe('black') // sanity check
111110
editFile('frontend/entrypoints/global.css', (code) =>
112111
code.replace('black', 'red'),
113112
)
114-
await untilUpdated(() => getColor('body'), 'red') // successful HMR
113+
await expect.poll(() => getColor('body')).toBe('red') // successful HMR
115114

116115
// Verify that the base (/dev/) was added during the css-update
117116
const link = await page.$('link[rel="stylesheet"]:last-of-type')
@@ -127,6 +126,6 @@ describe.runIf(isServe)('serve', () => {
127126
),
128127
'[vite] css hot updated: /global.css',
129128
)
130-
await untilUpdated(() => getColor(el), 'rgb(204, 0, 0)')
129+
await expect.poll(() => getColor(el)).toBe('rgb(204, 0, 0)')
131130
})
132131
})

playground/cli/__tests__/cli.spec.ts

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { expect, test } from 'vitest'
22
import { port, streams } from './serve'
3-
import { editFile, isServe, page, withRetry } from '~utils'
3+
import { editFile, isServe, page } from '~utils'
44

55
test('cli should work', async () => {
66
// this test uses a custom serve implementation, so regular helpers for browserLogs and goto don't work
@@ -23,17 +23,19 @@ test('cli should work', async () => {
2323
test.runIf(isServe)('should restart', async () => {
2424
const logsLengthBeforeEdit = streams.server.out.length
2525
editFile('./vite.config.js', (content) => content)
26-
await withRetry(async () => {
27-
const logs = streams.server.out.slice(logsLengthBeforeEdit)
28-
expect(logs).toEqual(
29-
expect.arrayContaining([expect.stringMatching('server restarted')]),
30-
)
31-
// Don't reprint the server URLs as they are the same
32-
expect(logs).not.toEqual(
33-
expect.arrayContaining([expect.stringMatching('http://localhost')]),
34-
)
35-
expect(logs).not.toEqual(
36-
expect.arrayContaining([expect.stringMatching('error')]),
37-
)
38-
})
26+
await expect
27+
.poll(() => {
28+
const logs = streams.server.out.slice(logsLengthBeforeEdit)
29+
expect(logs).toEqual(
30+
expect.arrayContaining([expect.stringMatching('server restarted')]),
31+
)
32+
// Don't reprint the server URLs as they are the same
33+
expect(logs).not.toEqual(
34+
expect.arrayContaining([expect.stringMatching('http://localhost')]),
35+
)
36+
expect(logs).not.toEqual(
37+
expect.arrayContaining([expect.stringMatching('error')]),
38+
)
39+
})
40+
.toSatisfy(() => true)
3941
})

playground/csp/__tests__/csp.spec.ts

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

44
test('linked css', async () => {
55
expect(await getColor('.linked')).toBe('blue')
@@ -18,27 +18,25 @@ test('dynamic css', async () => {
1818
})
1919

2020
test('script tag', async () => {
21-
await expectWithRetry(() => page.textContent('.js')).toBe('js: ok')
21+
await expect.poll(() => page.textContent('.js')).toBe('js: ok')
2222
})
2323

2424
test('dynamic js', async () => {
25-
await expectWithRetry(() => page.textContent('.dynamic-js')).toBe(
26-
'dynamic-js: ok',
27-
)
25+
await expect
26+
.poll(() => page.textContent('.dynamic-js'))
27+
.toBe('dynamic-js: ok')
2828
})
2929

3030
test('inline js', async () => {
31-
await expectWithRetry(() => page.textContent('.inline-js')).toBe(
32-
'inline-js: ok',
33-
)
31+
await expect.poll(() => page.textContent('.inline-js')).toBe('inline-js: ok')
3432
})
3533

3634
test('nonce attributes are not repeated', async () => {
3735
const htmlSource = await page.content()
3836
expect(htmlSource).not.toContain(/nonce=""[^>]*nonce=""/)
39-
await expectWithRetry(() => page.textContent('.double-nonce-js')).toBe(
40-
'double-nonce-js: ok',
41-
)
37+
await expect
38+
.poll(() => page.textContent('.double-nonce-js'))
39+
.toBe('double-nonce-js: ok')
4240
})
4341

4442
test('meta[property=csp-nonce] is injected', async () => {

playground/css-codesplit/__tests__/css-codesplit.spec.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {
66
listAssets,
77
page,
88
readManifest,
9-
untilUpdated,
109
} from '~utils'
1110

1211
test('should load all stylesheets', async () => {
@@ -34,7 +33,7 @@ test('should load dynamic import with module', async () => {
3433
test('style order should be consistent when style tag is inserted by JS', async () => {
3534
expect(await getColor('.order-bulk')).toBe('orange')
3635
await page.click('.order-bulk-update')
37-
await untilUpdated(() => getColor('.order-bulk'), 'green')
36+
await expect.poll(() => getColor('.order-bulk')).toBe('green')
3837
})
3938

4039
describe.runIf(isBuild)('build', () => {

playground/css-lightningcss/__tests__/css-lightningcss.spec.ts

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

@@ -21,12 +20,12 @@ test('linked css', async () => {
2120

2221
if (isBuild) return
2322
editFile('linked.css', (code) => code.replace('color: blue', 'color: red'))
24-
await untilUpdated(() => getColor(linked), 'red')
23+
await expect.poll(() => getColor(linked)).toBe('red')
2524

2625
editFile('linked-at-import.css', (code) =>
2726
code.replace('color: red', 'color: blue'),
2827
)
29-
await untilUpdated(() => getColor(atImport), 'blue')
28+
await expect.poll(() => getColor(atImport)).toBe('blue')
3029
})
3130

3231
test('css import from js', async () => {
@@ -38,12 +37,12 @@ test('css import from js', async () => {
3837

3938
if (isBuild) return
4039
editFile('imported.css', (code) => code.replace('color: green', 'color: red'))
41-
await untilUpdated(() => getColor(imported), 'red')
40+
await expect.poll(() => getColor(imported)).toBe('red')
4241

4342
editFile('imported-at-import.css', (code) =>
4443
code.replace('color: purple', 'color: blue'),
4544
)
46-
await untilUpdated(() => getColor(atImport), 'blue')
45+
await expect.poll(() => getColor(atImport)).toBe('blue')
4746
})
4847

4948
test('css modules', async () => {
@@ -56,7 +55,7 @@ test('css modules', async () => {
5655
editFile('mod.module.css', (code) =>
5756
code.replace('color: turquoise', 'color: red'),
5857
)
59-
await untilUpdated(() => getColor(imported), 'red')
58+
await expect.poll(() => getColor(imported)).toBe('red')
6059
})
6160

6261
test('inline css modules', async () => {

playground/css-no-codesplit/__tests__/css-no-codesplit.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { describe, expect, test } from 'vitest'
2-
import { expectWithRetry, getColor, isBuild, listAssets } from '~utils'
2+
import { getColor, isBuild, listAssets } from '~utils'
33

44
test('should load all stylesheets', async () => {
55
expect(await getColor('.shared-linked')).toBe('blue')
6-
await expectWithRetry(() => getColor('.async-js')).toBe('blue')
6+
await expect.poll(() => getColor('.async-js')).toBe('blue')
77
})
88

99
describe.runIf(isBuild)('build', () => {

playground/css/__tests__/sass-tests.ts

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,5 @@
11
import { expect, test } from 'vitest'
2-
import {
3-
editFile,
4-
getBg,
5-
getColor,
6-
isBuild,
7-
page,
8-
untilUpdated,
9-
viteTestUrl,
10-
} from '~utils'
2+
import { editFile, getBg, getColor, isBuild, page, viteTestUrl } from '~utils'
113

124
export const sassTest = () => {
135
test('sass', async () => {
@@ -68,17 +60,17 @@ export const sassTest = () => {
6860
editFile('sass.scss', (code) =>
6961
code.replace('color: $injectedColor', 'color: red'),
7062
)
71-
await untilUpdated(() => getColor(imported), 'red')
63+
await expect.poll(() => getColor(imported)).toBe('red')
7264

7365
editFile('nested/_index.scss', (code) =>
7466
code.replace('color: olive', 'color: blue'),
7567
)
76-
await untilUpdated(() => getColor(atImport), 'blue')
68+
await expect.poll(() => getColor(atImport)).toBe('blue')
7769

7870
editFile('nested/_partial.scss', (code) =>
7971
code.replace('color: orchid', 'color: green'),
8072
)
81-
await untilUpdated(() => getColor(partialImport), 'green')
73+
await expect.poll(() => getColor(partialImport)).toBe('green')
8274
})
8375
}
8476

@@ -101,7 +93,7 @@ export const sassModuleTests = (enableHmrTests = false) => {
10193
// editFile('composed.module.scss', (code) =>
10294
// code.replace('color: orangered', 'color: red')
10395
// )
104-
// await untilUpdated(() => getColor(imported), 'red')
96+
// await expect.poll(() => getColor(imported)).toMatch('red')
10597
})
10698

10799
test('css modules w/ sass', async () => {
@@ -116,7 +108,7 @@ export const sassModuleTests = (enableHmrTests = false) => {
116108
editFile('mod.module.scss', (code) =>
117109
code.replace('color: orangered', 'color: blue'),
118110
)
119-
await untilUpdated(() => getColor(imported), 'blue')
111+
await expect.poll(() => getColor(imported)).toBe('blue')
120112
})
121113
}
122114

0 commit comments

Comments
 (0)