diff --git a/packages/playground/ssr-react/__tests__/ssr-react.spec.ts b/packages/playground/ssr-react/__tests__/ssr-react.spec.ts index 12660c81e4be14..64457de30b17a3 100644 --- a/packages/playground/ssr-react/__tests__/ssr-react.spec.ts +++ b/packages/playground/ssr-react/__tests__/ssr-react.spec.ts @@ -1,4 +1,10 @@ -import { editFile, getColor, isBuild, untilUpdated } from '../../testUtils' +import { + editFile, + getColor, + isBuild, + untilUpdated, + autoRetry +} from '../../testUtils' import { port } from './serve' import fetch from 'node-fetch' @@ -39,8 +45,9 @@ test('hmr', async () => { test('client navigation', async () => { await page.click('a[href="/about"]') - await page.waitForTimeout(10) - expect(await page.textContent('h1')).toMatch('About') + await autoRetry(async () => { + expect(await page.textContent('h1')).toMatch('About') + }) editFile('src/pages/About.jsx', (code) => code.replace('

About', '

changed') ) diff --git a/packages/playground/ssr-vue/__tests__/ssr-vue.spec.ts b/packages/playground/ssr-vue/__tests__/ssr-vue.spec.ts index 63a6034cc7b36d..343c1140f81106 100644 --- a/packages/playground/ssr-vue/__tests__/ssr-vue.spec.ts +++ b/packages/playground/ssr-vue/__tests__/ssr-vue.spec.ts @@ -1,4 +1,10 @@ -import { editFile, getColor, isBuild, untilUpdated } from '../../testUtils' +import { + editFile, + getColor, + isBuild, + untilUpdated, + autoRetry +} from '../../testUtils' import { port } from './serve' import fetch from 'node-fetch' @@ -110,8 +116,9 @@ test('hmr', async () => { test('client navigation', async () => { await page.click('a[href="/about"]') - await page.waitForTimeout(10) - expect(await page.textContent('h1')).toMatch('About') + await autoRetry(async () => { + expect(await page.textContent('h1')).toMatch('About') + }) editFile('src/pages/About.vue', (code) => code.replace('About', 'changed')) await untilUpdated(() => page.textContent('h1'), 'changed') }) diff --git a/packages/playground/testUtils.ts b/packages/playground/testUtils.ts index 3da60754c891d0..10e2d9d618532c 100644 --- a/packages/playground/testUtils.ts +++ b/packages/playground/testUtils.ts @@ -119,3 +119,34 @@ export async function untilUpdated( } } } + +export async function autoRetry( + test: () => void | Promise +): Promise { + const timeout = 60 * 1000 + const period = 100 + const numberOfTries = timeout / period + let i = 0 + while (true) { + try { + await test() + return + } catch (err) { + i = i + 1 + if (i > numberOfTries) { + throw err + } + } + await sleep(period) + } + + return + + function sleep(milliseconds: number): Promise { + return new Promise((resolve) => + setTimeout(() => { + resolve() + }, milliseconds) + ) + } +}