Skip to content
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
5 changes: 5 additions & 0 deletions .changeset/jolly-tables-tan.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@yamatomo/playwright": patch
---

fix: Correct subprocess termination
23 changes: 23 additions & 0 deletions packages/playwright/src/getDescendantPids.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { execSync } from 'node:child_process'

export const getDescendantPids = (pid: number) => {
if (process.platform.startsWith('win')) {
throw new Error('getDescendantPids unsupported on Windows')
}

let children: number[] = []
try {
children = execSync(`pgrep -P ${pid}`, { encoding: 'utf8' }).trim().split('\n').map(Number)
} catch {}

const pids: number[] = []

if (children.length > 0) {
children.forEach((childPid) => {
pids.push(childPid)
pids.push(...getDescendantPids(childPid))
})
}

return pids
}
20 changes: 15 additions & 5 deletions packages/playwright/src/playwrightTestHelpers.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import type { ChildProcess } from 'node:child_process'

import { test as base } from '@playwright/test'
import { waitPort } from '@yamatomo/internal-utils'
import { createServer } from '@yamatomo/msw-server'
import { setupServer } from 'msw/node'

import { type AppEnv, type MswParams, store } from './fixtureConfig'
import { getDescendantPids } from './getDescendantPids.ts'

const startApp = async (baseURL: string) => {
if (!store.startApp) throw new Error('startApp is not defined.')
Expand All @@ -15,6 +18,17 @@ const startApp = async (baseURL: string) => {
return { process, env }
}

const killAppProcess = (appProcess: ChildProcess) => {
if (!appProcess.pid) throw new Error('App process PID is unknown.')

getDescendantPids(appProcess.pid)
.reverse()
.forEach((pid) => {
process.kill(pid)
})
appProcess.kill('SIGTERM')
}

const startMswServer = async (appEnv: AppEnv) => {
if (!store.resolveMswParams) throw new Error('resolveMswParams is not defined.')

Expand All @@ -37,13 +51,9 @@ const test = base.extend<TestFixtureType, WorkerFixtureType>({

const app = await startApp(baseURL)

process.on('exit', () => {
app.process.kill('SIGINT')
})

await use(app)

app.process.kill('SIGINT')
killAppProcess(app.process)
},
{ scope: 'worker' },
],
Expand Down