From e4c40d66e07fe78f93e93fed473b4d9307f7d863 Mon Sep 17 00:00:00 2001 From: Jason Jean Date: Fri, 9 Feb 2024 18:10:17 -0500 Subject: [PATCH] fix(testing): ensure cypress closes the web dev server (#21759) (cherry picked from commit 2fe4396e51b5726eab69a1c50a92dbcb22e6cf49) --- packages/cypress/plugins/cypress-preset.ts | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/packages/cypress/plugins/cypress-preset.ts b/packages/cypress/plugins/cypress-preset.ts index b9e6ae46991d3..824b63083e685 100644 --- a/packages/cypress/plugins/cypress-preset.ts +++ b/packages/cypress/plugins/cypress-preset.ts @@ -5,7 +5,7 @@ import { lstatSync } from 'fs'; import vitePreprocessor from '../src/plugins/preprocessor-vite'; import { NX_PLUGIN_OPTIONS } from '../src/utils/constants'; -import { exec } from 'child_process'; +import { spawn } from 'child_process'; import { request as httpRequest } from 'http'; import { request as httpsRequest } from 'https'; @@ -65,14 +65,19 @@ export function nxBaseCypressPreset( } function startWebServer(webServerCommand: string) { - const serverProcess = exec(webServerCommand, { + const serverProcess = spawn(webServerCommand, { cwd: workspaceRoot, + shell: true, + // Detaching the process on unix will create a process group, allowing us to kill it later + // Windows is fine so we leave it attached to this process + detached: process.platform !== 'win32', + stdio: 'inherit', }); - serverProcess.stdout.pipe(process.stdout); - serverProcess.stderr.pipe(process.stderr); return () => { - serverProcess.kill(); + // child.kill() does not work on linux + // process.kill will kill the whole process group on unix + process.kill(-serverProcess.pid, 'SIGKILL'); }; }