-
-
Notifications
You must be signed in to change notification settings - Fork 110
test: add HMR tests with playwright #750
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
Merged
Merged
Changes from 5 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
6cdf913
test: add playwright foundation
posva 6269b00
refactor: cleanup
posva 2d489a8
ci: fix playwright
posva 9f65a48
test: fix
posva 7608e89
test: fix
posva 4579ea0
ci: no retries
posva 9a5a3a4
test: logs to debug ci
posva a5a7ec6
test: fix it
posva 932e3da
ci: use polling
posva 17be53e
ci: fix?
posva 1dd2daf
chore: dedupe fs import'
posva c2ed5f3
ci: try without polling
posva 6917b30
ci: fix
posva f3508cd
test: no need for watch
posva File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| name: Playwright Tests | ||
| on: | ||
| push: | ||
| branches: [main] | ||
| pull_request: | ||
| branches: [main] | ||
| jobs: | ||
| test: | ||
| timeout-minutes: 60 | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - uses: actions/checkout@v5 | ||
| - uses: pnpm/action-setup@v4 | ||
| - uses: actions/setup-node@v6 | ||
| with: | ||
| node-version: lts/* | ||
| cache: 'pnpm' | ||
| - name: Install dependencies | ||
| run: pnpm install | ||
| - name: Install Playwright Browsers | ||
| run: pnpm exec playwright install --with-deps | ||
| - name: Run Playwright tests | ||
| run: pnpm exec playwright test | ||
| - uses: actions/upload-artifact@v5 | ||
| if: ${{ !cancelled() }} | ||
| with: | ||
| name: playwright-report | ||
| path: playwright-report/ | ||
| retention-days: 30 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| playground-tmp |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| import { test as base, expect } from '@playwright/test' | ||
| import { createServer, type ViteDevServer } from 'vite' | ||
| import { type AddressInfo } from 'node:net' | ||
| import path from 'node:path' | ||
| import fs from 'node:fs' | ||
| import { fileURLToPath } from 'node:url' | ||
| import { cpSync, rmSync } from 'node:fs' | ||
|
|
||
| type ViteFixtures = { | ||
| devServer: ViteDevServer | ||
| baseURL: string | ||
| projectRoot: string | ||
| } | ||
|
|
||
| export function applyEditFile( | ||
| sourceFilePath: string, | ||
| newContentFilePath: string | ||
| ) { | ||
| fs.writeFileSync( | ||
| path.join(projectRoot, sourceFilePath), | ||
| fs.readFileSync(path.join(projectRoot, newContentFilePath), 'utf8'), | ||
| 'utf8' | ||
| ) | ||
| } | ||
|
|
||
| const sourceDir = fileURLToPath(new URL('../playground', import.meta.url)) | ||
| const fixtureDir = fileURLToPath(new URL('../playground-tmp', import.meta.url)) | ||
| const projectRoot = path.resolve(fixtureDir) | ||
|
|
||
| export const test = base.extend<ViteFixtures>({ | ||
| projectRoot, | ||
|
|
||
| // @ts-expect-error: type matched what is passed to use(server) | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| devServer: [ | ||
| async ({}, use) => { | ||
| console.log(projectRoot) | ||
coderabbitai[bot] marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| rmSync(fixtureDir, { force: true, recursive: true }) | ||
| cpSync(sourceDir, fixtureDir, { | ||
| recursive: true, | ||
| filter: (src) => { | ||
| return ( | ||
| !src.includes('.cache') && | ||
| !src.endsWith('.sock') && | ||
| !src.includes('.output') && | ||
| !src.includes('.vite') | ||
| ) | ||
| }, | ||
| }) | ||
| // Start a real Vite dev server with your plugin(s) & config. | ||
| // If you already have vite.config.ts, omit configFile:false and rely on it. | ||
| const server = await createServer({ | ||
| configFile: path.join(fixtureDir, 'vite.config.ts'), | ||
| // If you need to inline the plugin directly, you could do: | ||
| // configFile: false, | ||
| // plugins: [myPlugin()], | ||
| server: { host: '127.0.0.1', port: 0, strictPort: false }, // random open port | ||
| logLevel: 'error', | ||
| }) | ||
|
|
||
| await server.listen() | ||
|
|
||
| const http = server.httpServer | ||
| if (!http) throw new Error('No httpServer from Vite') | ||
|
|
||
| // Expose the running server & URL to tests | ||
| await use(server) | ||
|
|
||
| await server.close() | ||
| }, | ||
| { scope: 'worker' }, | ||
| ], | ||
|
|
||
| baseURL: async ({ devServer }, use) => { | ||
| const http = devServer.httpServer! | ||
| const addr = http.address() as AddressInfo | ||
| await use(`http://127.0.0.1:${addr.port}`) | ||
| }, | ||
posva marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }) | ||
|
|
||
| export { expect } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| import { fileURLToPath } from 'node:url' | ||
| import { cpSync, rmSync } from 'node:fs' | ||
| import { test as setup } from '@playwright/test' | ||
|
|
||
| const fixtureDir = fileURLToPath(new URL('./playground-tmp', import.meta.url)) | ||
| const sourceDir = fileURLToPath(new URL('./playground', import.meta.url)) | ||
|
|
||
| setup('create temporary hmr playground directory', () => { | ||
| rmSync(fixtureDir, { force: true, recursive: true }) | ||
| cpSync(sourceDir, fixtureDir, { | ||
| recursive: true, | ||
| filter: (src) => { | ||
| return ( | ||
| !src.includes('.cache') && | ||
| !src.endsWith('.sock') && | ||
| !src.includes('.output') && | ||
| !src.includes('.vite') | ||
| ) | ||
| }, | ||
| }) | ||
| }) | ||
posva marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| import { Page } from '@playwright/test' | ||
| import { test, expect, applyEditFile } from './fixtures/vite-server' | ||
|
|
||
| test.describe('Pages HMR', () => { | ||
| let hmrToken: number = -1 | ||
| // reset hmr token before each test | ||
| test.beforeEach(() => { | ||
| hmrToken = -1 | ||
| }) | ||
|
|
||
| async function ensureHmrToken(page: Page) { | ||
| hmrToken = await page.evaluate( | ||
| () => ((window as any).__hmrToken ??= Math.random()) | ||
| ) | ||
| } | ||
|
|
||
| // ensure hmr token is stable across tests | ||
| test.afterEach(async ({ page }) => { | ||
| if (hmrToken === -1) { | ||
| throw new Error('hmrToken was not set in the test') | ||
| } | ||
| await expect | ||
| .poll(async () => page.evaluate(() => (window as any).__hmrToken)) | ||
| .toBe(hmrToken) | ||
| }) | ||
|
|
||
| test('applies meta changes in <route> block', async ({ page, baseURL }) => { | ||
| await page.goto(baseURL + '/') | ||
|
|
||
| await expect(page.locator('[data-testid="meta-hello"]')).toHaveText('') | ||
|
|
||
| await ensureHmrToken(page) | ||
| applyEditFile('src/pages/(home).vue', 'edits/(home)-with-route-block.vue') | ||
|
|
||
| await expect(page.locator('[data-testid="meta-hello"]')).toHaveText('world') | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| <template> | ||
| <main> | ||
| <h1>Home</h1> | ||
|
|
||
| <pre data-testid="meta-hello">{{ $route.meta.hello }}</pre> | ||
| </main> | ||
| </template> | ||
|
|
||
| <route lang="json"> | ||
| { | ||
| "meta": { | ||
| "hello": "world" | ||
| } | ||
| } | ||
| </route> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| <!doctype html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="UTF-8" /> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
| <title>HMR E2E Tests</title> | ||
| </head> | ||
| <body> | ||
| <div id="app"></div> | ||
| <script type="module" src="./src/main.ts"></script> | ||
| </body> | ||
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| { | ||
| "private": true, | ||
| "name": "fixture-hmr", | ||
| "scripts": { | ||
| "build": "vite build" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| <template> | ||
| <RouterView /> | ||
| </template> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| import { createApp } from 'vue' | ||
| import App from './App.vue' | ||
| import { router } from './router' | ||
|
|
||
| const app = createApp(App) | ||
| app.use(router) | ||
| app.mount('#app') | ||
|
|
||
| // small logger for navigations, useful to check HMR | ||
| router.isReady().then(() => { | ||
| router.beforeEach((to, from) => { | ||
| console.log('🧭', from.fullPath, '->', to.fullPath) | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| <template> | ||
| <main> | ||
| <h1>Home</h1> | ||
|
|
||
| <pre data-testid="meta-hello">{{ $route.meta.hello }}</pre> | ||
| </main> | ||
| </template> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| import { createRouter, createWebHistory } from 'vue-router' | ||
| import { routes, handleHotUpdate } from 'vue-router/auto-routes' | ||
|
|
||
| export const router = createRouter({ | ||
| history: createWebHistory(), | ||
| routes, | ||
| }) | ||
|
|
||
| if (import.meta.hot) { | ||
| handleHotUpdate(router, (routes) => { | ||
| console.log('🔥 HMR with', routes) | ||
| }) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| import { defineConfig } from 'vite' | ||
| import { fileURLToPath, URL } from 'node:url' | ||
| import VueRouter from '../../../src/vite' | ||
| import Vue from '@vitejs/plugin-vue' | ||
|
|
||
| const root = fileURLToPath(new URL('./', import.meta.url)) | ||
|
|
||
| export default defineConfig({ | ||
| root, | ||
| clearScreen: false, | ||
| resolve: { | ||
| alias: { | ||
| '@': fileURLToPath(new URL('./src', import.meta.url)), | ||
| '~': fileURLToPath(new URL('./src', import.meta.url)), | ||
| 'unplugin-vue-router/runtime': fileURLToPath( | ||
| new URL('../../../src/runtime.ts', import.meta.url) | ||
| ), | ||
| 'unplugin-vue-router/types': fileURLToPath( | ||
| new URL('../../../src/types.ts', import.meta.url) | ||
| ), | ||
| 'unplugin-vue-router/data-loaders/basic': fileURLToPath( | ||
| new URL('../../../src/data-loaders/entries/basic.ts', import.meta.url) | ||
| ), | ||
| 'unplugin-vue-router/data-loaders/pinia-colada': fileURLToPath( | ||
| new URL( | ||
| '../../../src/data-loaders/entries/pinia-colada.ts', | ||
| import.meta.url | ||
| ) | ||
| ), | ||
| 'unplugin-vue-router/data-loaders': fileURLToPath( | ||
| new URL('../../../src/data-loaders/entries/index.ts', import.meta.url) | ||
| ), | ||
| }, | ||
| }, | ||
| build: { | ||
| sourcemap: true, | ||
| }, | ||
|
|
||
| plugins: [ | ||
| VueRouter({ | ||
| root, | ||
| logs: true, | ||
| // getRouteName: getPascalCaseRouteName, | ||
| experimental: { | ||
| autoExportsDataLoaders: ['src/loaders/**/*', '@/loaders/**/*'], | ||
| paramParsers: false, | ||
| }, | ||
| }), | ||
| Vue(), | ||
| ], | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.