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

chore: reduce test flakiness #14684

Merged
merged 7 commits into from
Oct 19, 2023
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
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ playground/tsconfig-json-load-error/has-error/tsconfig.json
playground/html/invalid.html
playground/html/valid.html
playground/worker/classic-worker.js
playground/external/public/slash@3.0.0.js
6 changes: 3 additions & 3 deletions playground/external/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
<script type="importmap">
{
"imports": {
"vue": "https://unpkg.com/vue@3.2.0/dist/vue.runtime.esm-browser.js",
"slash5": "https://unpkg.com/slash@5.0.0/index.js",
"slash3": "https://esm.sh/slash@3.0.0"
"vue": "/vue@3.2.0.js",
"slash5": "/slash@5.js",
"slash3": "/slash@3.0.0.js"
}
}
</script>
Expand Down
3 changes: 2 additions & 1 deletion playground/external/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"slash3": "npm:slash@^3.0.0",
"slash5": "npm:slash@^5.1.0",
"vite": "workspace:*",
"vue": "^3.3.4"
"vue": "^3.3.4",
"vue32": "npm:vue@~3.2.0"
}
}
5 changes: 5 additions & 0 deletions playground/external/public/slash@3.0.0.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions playground/external/vite.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
import fs from 'node:fs/promises'
import { defineConfig } from 'vite'

const npmDirectServeConfig = {
'/vue@3.2.0.js': 'vue32/dist/vue.runtime.esm-browser.js',
'/slash@5.js': 'slash5/index.js',
}
/** @type {import('vite').Connect.NextHandleFunction} */
const serveNpmCodeDirectlyMiddleware = async (req, res, next) => {
for (const [url, file] of Object.entries(npmDirectServeConfig)) {
if (req.originalUrl === url) {
const code = await fs.readFile(
new URL(`./node_modules/${file}`, import.meta.url),
)
res.setHeader('Content-Type', 'application/javascript')
res.end(code)
return
}
}
next()
}

export default defineConfig({
optimizeDeps: {
include: ['dep-that-imports', 'dep-that-requires'],
Expand All @@ -14,4 +34,15 @@ export default defineConfig({
esmExternals: ['vue', 'slash5'],
},
},
plugins: [
{
name: 'serve-npm-code-directly',
configureServer({ middlewares }) {
middlewares.use(serveNpmCodeDirectlyMiddleware)
},
configurePreviewServer({ middlewares }) {
middlewares.use(serveNpmCodeDirectlyMiddleware)
},
},
],
})
21 changes: 13 additions & 8 deletions playground/hmr/__tests__/hmr.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,7 @@ if (!isBuild) {

await untilBrowserLogAfter(
() => page.goto(`${viteTestUrl}/${testDir}/`),
'>>> ready <<<',
[CONNECTED, '>>> ready <<<'],
(logs) => {
expect(logs).toContain('loaded:some:a0b0c0default0')
expect(logs).toContain('some >>>>>> a0, b0, c0')
Expand All @@ -640,10 +640,11 @@ if (!isBuild) {

await untilBrowserLogAfter(
async () => {
const loadPromise = page.waitForEvent('load')
editFile(file, (code) => code.replace(/([abc])0/g, '$11'))
await page.waitForEvent('load')
await loadPromise
},
'>>> ready <<<',
[CONNECTED, '>>> ready <<<'],
(logs) => {
expect(logs).toContain('loaded:some:a1b1c1default0')
expect(logs).toContain('some >>>>>> a1, b1, c1')
Expand Down Expand Up @@ -712,21 +713,25 @@ if (!isBuild) {
const importCode = "import 'missing-modules'"
const unImportCode = `// ${importCode}`

await page.goto(viteTestUrl + '/missing-import/index.html', {
waitUntil: 'load',
})
await untilBrowserLogAfter(
() =>
page.goto(viteTestUrl + '/missing-import/index.html', {
waitUntil: 'load',
}),
/connected/, // wait for HMR connection
)

await untilBrowserLogAfter(async () => {
const loadPromise = page.waitForEvent('load')
editFile(file, (code) => code.replace(importCode, unImportCode))
await loadPromise
}, 'missing test')
}, ['missing test', /connected/])

await untilBrowserLogAfter(async () => {
const loadPromise = page.waitForEvent('load')
editFile(file, (code) => code.replace(unImportCode, importCode))
await loadPromise
}, /500/)
}, [/500/, /connected/])
})

test('should hmr when file is deleted and restored', async () => {
Expand Down
5 changes: 4 additions & 1 deletion playground/lib/__tests__/lib.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
readFile,
serverLogs,
untilUpdated,
withRetry,
} from '~utils'

describe.runIf(isBuild)('build', () => {
Expand Down Expand Up @@ -84,5 +85,7 @@ describe.runIf(isBuild)('build', () => {
})

test.runIf(isServe)('dev', async () => {
expect(await page.textContent('.demo')).toBe('It works')
await withRetry(async () => {
expect(await page.textContent('.demo')).toBe('It works')
})
})
15 changes: 13 additions & 2 deletions playground/proxy-hmr/__tests__/proxy-hmr.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
import { test } from 'vitest'
import { editFile, page, untilUpdated, viteTestUrl } from '~utils'
import {
editFile,
page,
untilBrowserLogAfter,
untilUpdated,
viteTestUrl,
} from '~utils'

test('proxy-hmr', async () => {
await page.goto(viteTestUrl)
await untilBrowserLogAfter(
() => page.goto(viteTestUrl),
// wait for both main and sub app HMR connection
[/connected/, /connected/],
)

const otherAppTextLocator = page.frameLocator('iframe').locator('.content')
await untilUpdated(() => otherAppTextLocator.textContent(), 'other app')
editFile('other-app/index.html', (code) =>
Expand Down
57 changes: 57 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.