|
| 1 | +import { execSync } from 'node:child_process' |
| 2 | +import { expect, onTestFinished, test } from 'vitest' |
| 3 | +import { instances, provider, runInlineBrowserTests } from './utils' |
| 4 | + |
| 5 | +// walk the process tree because the provider does not expose the browser pid |
| 6 | +function findDescendantBrowserProcesses(): number[] { |
| 7 | + const output = execSync('ps -eo pid=,ppid=,args=', { encoding: 'utf-8' }) |
| 8 | + const childrenByParent = new Map<number, number[]>() |
| 9 | + const argsByPid = new Map<number, string>() |
| 10 | + for (const line of output.split('\n')) { |
| 11 | + const [pidRaw, ppidRaw, ...args] = line.trim().split(/\s+/) |
| 12 | + const pid = Number(pidRaw) |
| 13 | + const ppid = Number(ppidRaw) |
| 14 | + if (!Number.isInteger(pid) || !Number.isInteger(ppid)) { |
| 15 | + continue |
| 16 | + } |
| 17 | + const children = childrenByParent.get(ppid) ?? [] |
| 18 | + children.push(pid) |
| 19 | + childrenByParent.set(ppid, children) |
| 20 | + argsByPid.set(pid, args.join(' ')) |
| 21 | + } |
| 22 | + const browserPids: number[] = [] |
| 23 | + const queue = [process.pid] |
| 24 | + while (queue.length) { |
| 25 | + const pid = queue.shift()! |
| 26 | + for (const child of childrenByParent.get(pid) ?? []) { |
| 27 | + queue.push(child) |
| 28 | + if (/headless[ _]shell|chromium|chrome/i.test(argsByPid.get(child) ?? '')) { |
| 29 | + browserPids.push(child) |
| 30 | + } |
| 31 | + } |
| 32 | + } |
| 33 | + return browserPids |
| 34 | +} |
| 35 | + |
| 36 | +function signalAll(pids: number[], signal: NodeJS.Signals) { |
| 37 | + for (const pid of pids) { |
| 38 | + try { |
| 39 | + process.kill(pid, signal) |
| 40 | + } |
| 41 | + catch { |
| 42 | + // the process is already gone |
| 43 | + } |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +// SIGSTOP freezes the browser without closing its websocket, standing in for |
| 48 | +// any browser death that leaves the socket open (vitest-dev/vitest#10791); |
| 49 | +// requires a locally launched playwright browser and POSIX signals |
| 50 | +test.runIf( |
| 51 | + provider.name === 'playwright' |
| 52 | + && process.platform !== 'win32' |
| 53 | + && !process.env.BROWSER_WS_ENDPOINT, |
| 54 | +)('fails instead of hanging when the browser stops responding mid-run', { timeout: 60_000 }, async () => { |
| 55 | + process.env.VITEST_BROWSER_HEARTBEAT_INTERVAL = '1000' |
| 56 | + let frozenPids: number[] = [] |
| 57 | + onTestFinished(() => { |
| 58 | + delete process.env.VITEST_BROWSER_HEARTBEAT_INTERVAL |
| 59 | + signalAll(frozenPids, 'SIGCONT') |
| 60 | + }) |
| 61 | + |
| 62 | + const { ctx, fs } = await runInlineBrowserTests( |
| 63 | + { |
| 64 | + 'basic.test.ts': ` |
| 65 | + import { test } from 'vitest' |
| 66 | +
|
| 67 | + test('first', () => {}) |
| 68 | +
|
| 69 | + test('never finishes', async () => { |
| 70 | + await new Promise(resolve => setTimeout(resolve, 60_000)) |
| 71 | + }) |
| 72 | + `, |
| 73 | + }, |
| 74 | + { |
| 75 | + reporters: [ |
| 76 | + { |
| 77 | + onTestCaseResult() { |
| 78 | + if (!frozenPids.length) { |
| 79 | + frozenPids = findDescendantBrowserProcesses() |
| 80 | + expect(frozenPids.length).toBeGreaterThan(0) |
| 81 | + signalAll(frozenPids, 'SIGSTOP') |
| 82 | + } |
| 83 | + }, |
| 84 | + // unfreeze before `startVitest` closes the provider, so the |
| 85 | + // browser can answer the close message |
| 86 | + onTestRunEnd() { |
| 87 | + signalAll(frozenPids, 'SIGCONT') |
| 88 | + }, |
| 89 | + }, |
| 90 | + ], |
| 91 | + browser: { |
| 92 | + instances: [instances[0]], |
| 93 | + }, |
| 94 | + }, |
| 95 | + ) |
| 96 | + |
| 97 | + const unhandledErrors = ctx!.state.getUnhandledErrors() as Error[] |
| 98 | + const messages = unhandledErrors.map((error) => { |
| 99 | + const cause = error.cause as Error | undefined |
| 100 | + return cause ? `${error.message} ${cause.message}` : error.message |
| 101 | + }) |
| 102 | + expect(messages).toContainEqual( |
| 103 | + `Failed to run the test ${fs.resolveFile('basic.test.ts')}. ` |
| 104 | + + `[vitest] The browser orchestrator did not respond to a heartbeat ping for 2000ms. ` |
| 105 | + + `The browser process might be frozen or killed. Closing the connection.`, |
| 106 | + ) |
| 107 | +}) |
| 108 | + |
| 109 | +test('warns when VITEST_BROWSER_HEARTBEAT_INTERVAL is not a number and uses the default', async () => { |
| 110 | + process.env.VITEST_BROWSER_HEARTBEAT_INTERVAL = 'not-a-number' |
| 111 | + onTestFinished(() => { |
| 112 | + delete process.env.VITEST_BROWSER_HEARTBEAT_INTERVAL |
| 113 | + }) |
| 114 | + |
| 115 | + const { ctx, stderr } = await runInlineBrowserTests( |
| 116 | + { |
| 117 | + 'basic.test.ts': ` |
| 118 | + import { test } from 'vitest' |
| 119 | +
|
| 120 | + test('works', () => {}) |
| 121 | + `, |
| 122 | + }, |
| 123 | + { |
| 124 | + browser: { |
| 125 | + instances: [instances[0]], |
| 126 | + }, |
| 127 | + }, |
| 128 | + ) |
| 129 | + |
| 130 | + expect(stderr).toContain( |
| 131 | + 'VITEST_BROWSER_HEARTBEAT_INTERVAL is expected to be a number, received "not-a-number". ' |
| 132 | + + 'Using the default interval of 15000ms instead.', |
| 133 | + ) |
| 134 | + expect(ctx!.state.getUnhandledErrors()).toEqual([]) |
| 135 | +}) |
0 commit comments