Skip to content

Commit

Permalink
fix(test): improve stability of ssr testsuites by using chokidar poll…
Browse files Browse the repository at this point in the history
…ing mode and additional waits (#2954)
  • Loading branch information
dominikg committed Apr 12, 2021
1 parent 0825f7e commit 705a2b3
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 53 deletions.
13 changes: 3 additions & 10 deletions packages/playground/ssr-react/__tests__/ssr-react.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
import {
editFile,
getColor,
isBuild,
untilUpdated,
autoRetry
} from '../../testUtils'
import { editFile, getColor, isBuild, untilUpdated } from '../../testUtils'
import { port } from './serve'
import fetch from 'node-fetch'

Expand Down Expand Up @@ -44,10 +38,9 @@ test('hmr', async () => {
})

test('client navigation', async () => {
await untilUpdated(() => page.textContent('a[href="/about"]'), 'About')
await page.click('a[href="/about"]')
await autoRetry(async () => {
expect(await page.textContent('h1')).toMatch('About')
})
await untilUpdated(() => page.textContent('h1'), 'About')
editFile('src/pages/About.jsx', (code) =>
code.replace('<h1>About', '<h1>changed')
)
Expand Down
8 changes: 7 additions & 1 deletion packages/playground/ssr-react/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,13 @@ async function createServer(
root,
logLevel: isTest ? 'error' : 'info',
server: {
middlewareMode: true
middlewareMode: true,
watch: {
// During tests we edit the files too fast and sometimes chokidar
// misses change events, so enforce polling for consistency
usePolling: true,
interval: 100
}
}
})
// use vite's connect instance as middleware
Expand Down
13 changes: 3 additions & 10 deletions packages/playground/ssr-vue/__tests__/ssr-vue.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
import {
editFile,
getColor,
isBuild,
untilUpdated,
autoRetry
} from '../../testUtils'
import { editFile, getColor, isBuild, untilUpdated } from '../../testUtils'
import { port } from './serve'
import fetch from 'node-fetch'

Expand Down Expand Up @@ -115,10 +109,9 @@ test('hmr', async () => {
})

test('client navigation', async () => {
await untilUpdated(() => page.textContent('a[href="/about"]'), 'About')
await page.click('a[href="/about"]')
await autoRetry(async () => {
expect(await page.textContent('h1')).toMatch('About')
})
await untilUpdated(() => page.textContent('h1'), 'About')
editFile('src/pages/About.vue', (code) => code.replace('About', 'changed'))
await untilUpdated(() => page.textContent('h1'), 'changed')
})
8 changes: 7 additions & 1 deletion packages/playground/ssr-vue/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,13 @@ async function createServer(
root,
logLevel: isTest ? 'error' : 'info',
server: {
middlewareMode: true
middlewareMode: true,
watch: {
// During tests we edit the files too fast and sometimes chokidar
// misses change events, so enforce polling for consistency
usePolling: true,
interval: 100
}
}
})
// use vite's connect instance as middleware
Expand Down
31 changes: 0 additions & 31 deletions packages/playground/testUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,34 +119,3 @@ export async function untilUpdated(
}
}
}

export async function autoRetry(
test: () => void | Promise<void>
): Promise<void> {
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<void> {
return new Promise((resolve) =>
setTimeout(() => {
resolve()
}, milliseconds)
)
}
}

0 comments on commit 705a2b3

Please sign in to comment.