Skip to content

Commit b28c847

Browse files
authored
test: rewrite waitForNavigation to waitForEvent('load') (#13413)
1 parent 1197b24 commit b28c847

File tree

4 files changed

+31
-21
lines changed

4 files changed

+31
-21
lines changed

playground/assets/__tests__/assets.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,12 +408,13 @@ test('inline style test', async () => {
408408
if (!isBuild) {
409409
test('@import in html style tag hmr', async () => {
410410
await untilUpdated(() => getColor('.import-css'), 'rgb(0, 136, 255)')
411+
const loadPromise = page.waitForEvent('load')
411412
editFile(
412413
'./css/import.css',
413414
(code) => code.replace('#0088ff', '#00ff88'),
414415
true,
415416
)
416-
await page.waitForNavigation()
417+
await loadPromise
417418
await untilUpdated(() => getColor('.import-css'), 'rgb(0, 255, 136)')
418419
})
419420
}

playground/css/postcss-caching/css.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ test.runIf(isServe)('postcss config', async () => {
3333

3434
blueApp = await startServer(blueAppDir)
3535

36-
await page.goto(`http://localhost:${port}`)
36+
await page.goto(`http://localhost:${port}`, { waitUntil: 'load' })
3737
const blueA = await page.$('.postcss-a')
3838
expect(await getColor(blueA)).toBe('blue')
3939
const blueB = await page.$('.postcss-b')
@@ -44,9 +44,9 @@ test.runIf(isServe)('postcss config', async () => {
4444
await blueApp.close()
4545
blueApp = null
4646

47-
const navigationPromise = page.waitForNavigation() // wait for server restart auto reload
47+
const loadPromise = page.waitForEvent('load') // wait for server restart auto reload
4848
greenApp = await startServer(greenAppDir)
49-
await navigationPromise
49+
await loadPromise
5050

5151
const greenA = await page.$('.postcss-a')
5252
expect(await getColor(greenA)).toBe('black')

playground/hmr/__tests__/hmr.spec.ts

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -229,16 +229,17 @@ if (!isBuild) {
229229
})
230230

231231
test('not loaded dynamic import', async () => {
232-
await page.goto(viteTestUrl + '/counter/index.html')
232+
await page.goto(viteTestUrl + '/counter/index.html', { waitUntil: 'load' })
233233

234234
let btn = await page.$('button')
235235
expect(await btn.textContent()).toBe('Counter 0')
236236
await btn.click()
237237
expect(await btn.textContent()).toBe('Counter 1')
238238

239239
// Modifying `index.ts` triggers a page reload, as expected
240+
const indexTsLoadPromise = page.waitForEvent('load')
240241
editFile('counter/index.ts', (code) => code)
241-
await page.waitForNavigation()
242+
await indexTsLoadPromise
242243
btn = await page.$('button')
243244
expect(await btn.textContent()).toBe('Counter 0')
244245

@@ -251,13 +252,12 @@ if (!isBuild) {
251252
// (Note that, a dynamic import that is never loaded and that does not
252253
// define `accept.module.hot.accept` may wrongfully trigger a full page
253254
// reload, see discussion at #7561.)
255+
const depTsLoadPromise = page.waitForEvent('load', { timeout: 1000 })
254256
editFile('counter/dep.ts', (code) => code)
255-
try {
256-
await page.waitForNavigation({ timeout: 1000 })
257-
} catch (err) {
258-
const errMsg = 'page.waitForNavigation: Timeout 1000ms exceeded.'
259-
expect(err.message.slice(0, errMsg.length)).toBe(errMsg)
260-
}
257+
await expect(depTsLoadPromise).rejects.toThrow(
258+
/page\.waitForEvent: Timeout \d+ms exceeded while waiting for event "load"/,
259+
)
260+
261261
btn = await page.$('button')
262262
expect(await btn.textContent()).toBe('Counter 1')
263263
})
@@ -653,21 +653,25 @@ if (!isBuild) {
653653
test('css in html hmr', async () => {
654654
await page.goto(viteTestUrl)
655655
expect(await getBg('.import-image')).toMatch('icon')
656-
await page.goto(viteTestUrl + '/foo/')
656+
await page.goto(viteTestUrl + '/foo/', { waitUntil: 'load' })
657657
expect(await getBg('.import-image')).toMatch('icon')
658+
659+
const loadPromise = page.waitForEvent('load')
658660
editFile('index.html', (code) => code.replace('url("./icon.png")', ''))
659-
await page.waitForNavigation()
661+
await loadPromise
660662
expect(await getBg('.import-image')).toMatch('')
661663
})
662664

663665
test('HTML', async () => {
664666
await page.goto(viteTestUrl + '/counter/index.html')
665667
let btn = await page.$('button')
666668
expect(await btn.textContent()).toBe('Counter 0')
669+
670+
const loadPromise = page.waitForEvent('load')
667671
editFile('counter/index.html', (code) =>
668672
code.replace('Counter', 'Compteur'),
669673
)
670-
await page.waitForNavigation()
674+
await loadPromise
671675
btn = await page.$('button')
672676
expect(await btn.textContent()).toBe('Compteur 0')
673677
})
@@ -701,18 +705,20 @@ if (!isBuild) {
701705
const unImportCode = `// ${importCode}`
702706
const timeout = 2000
703707

704-
await page.goto(viteTestUrl + '/missing-import/index.html')
708+
await page.goto(viteTestUrl + '/missing-import/index.html', {
709+
waitUntil: 'load',
710+
})
705711

706712
await untilBrowserLogAfter(async () => {
707-
const navigationPromise = page.waitForNavigation({ timeout })
713+
const loadPromise = page.waitForEvent('load', { timeout })
708714
editFile(file, (code) => code.replace(importCode, unImportCode))
709-
await navigationPromise
715+
await loadPromise
710716
}, 'missing test')
711717

712718
await untilBrowserLogAfter(async () => {
713-
const navigationPromise = page.waitForNavigation({ timeout })
719+
const loadPromise = page.waitForEvent('load', { timeout })
714720
editFile(file, (code) => code.replace(unImportCode, importCode))
715-
await navigationPromise
721+
await loadPromise
716722
}, /500/)
717723
})
718724

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,13 @@ describe.runIf(isServe)('hmr', () => {
4848
await page.goto(url)
4949
const el = await page.$('.virtual')
5050
expect(await el.textContent()).toBe('[success]')
51+
52+
const loadPromise = page.waitForEvent('load')
5153
editFile('src/importedVirtual.js', (code) =>
5254
code.replace('[success]', '[wow]'),
5355
)
54-
await page.waitForNavigation()
56+
await loadPromise
57+
5558
await untilUpdated(async () => {
5659
const el = await page.$('.virtual')
5760
return await el.textContent()

0 commit comments

Comments
 (0)