forked from vitejs/vite
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: use websocket to test server liveness before client reload (vite…
…js#17891) Co-authored-by: sapphi-red <[email protected]>
- Loading branch information
1 parent
91a1acb
commit 7f9f8c6
Showing
13 changed files
with
170 additions
and
22 deletions.
There are no files selected for viewing
This file contains 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 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 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,75 @@ | ||
import path from 'node:path' | ||
import { type ServerOptions, type ViteDevServer, createServer } from 'vite' | ||
import { afterEach, describe, expect, test } from 'vitest' | ||
import { hmrPorts, isServe, page, ports } from '~utils' | ||
|
||
let server: ViteDevServer | ||
|
||
afterEach(async () => { | ||
await server?.close() | ||
}) | ||
|
||
async function testClientReload(serverOptions: ServerOptions) { | ||
// start server | ||
server = await createServer({ | ||
root: path.resolve(import.meta.dirname, '..'), | ||
logLevel: 'silent', | ||
server: { | ||
strictPort: true, | ||
...serverOptions, | ||
}, | ||
}) | ||
|
||
await server.listen() | ||
const serverUrl = server.resolvedUrls.local[0] | ||
|
||
// open page and wait for connection | ||
const connectedPromise = page.waitForEvent('console', { | ||
predicate: (message) => message.text().includes('[vite] connected.'), | ||
timeout: 5000, | ||
}) | ||
await page.goto(serverUrl) | ||
await connectedPromise | ||
|
||
// input state | ||
await page.locator('input').fill('hello') | ||
|
||
// restart and wait for reconnection after reload | ||
const reConnectedPromise = page.waitForEvent('console', { | ||
predicate: (message) => message.text().includes('[vite] connected.'), | ||
timeout: 5000, | ||
}) | ||
await server.restart() | ||
await reConnectedPromise | ||
expect(await page.textContent('input')).toBe('') | ||
} | ||
|
||
describe.runIf(isServe)('client-reload', () => { | ||
test('default', async () => { | ||
await testClientReload({ | ||
port: ports['client-reload'], | ||
}) | ||
}) | ||
|
||
test('custom hmr port', async () => { | ||
await testClientReload({ | ||
port: ports['client-reload/hmr-port'], | ||
hmr: { | ||
port: hmrPorts['client-reload/hmr-port'], | ||
}, | ||
}) | ||
}) | ||
|
||
test('custom hmr port and cross origin isolation', async () => { | ||
await testClientReload({ | ||
port: ports['client-reload/cross-origin'], | ||
hmr: { | ||
port: hmrPorts['client-reload/cross-origin'], | ||
}, | ||
headers: { | ||
'Cross-Origin-Embedder-Policy': 'require-corp', | ||
'Cross-Origin-Opener-Policy': 'same-origin', | ||
}, | ||
}) | ||
}) | ||
}) |
This file contains 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,6 @@ | ||
// do nothing here since server is managed inside spec | ||
export async function serve(): Promise<{ close(): Promise<void> }> { | ||
return { | ||
close: () => Promise.resolve(), | ||
} | ||
} |
This file contains 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,4 @@ | ||
<body> | ||
<h4>Test Client Reload</h4> | ||
<input /> | ||
</body> |
This file contains 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 @@ | ||
{ | ||
"name": "@vitejs/test-client-reload", | ||
"private": true, | ||
"version": "0.0.0", | ||
"type": "module", | ||
"scripts": { | ||
"debug": "node --inspect-brk ../../packages/vite/bin/vite", | ||
"dev": "vite", | ||
"build": "vite build", | ||
"preview": "vite preview" | ||
} | ||
} |
This file contains 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,5 @@ | ||
import { defineConfig } from 'vite' | ||
|
||
export default defineConfig({ | ||
server: {}, | ||
}) |
This file contains 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 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 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 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 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.