Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(test): improve stability of ssr tests #2954

Merged
merged 1 commit into from
Apr 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
)
}
}