Skip to content

Commit

Permalink
fix(testing): ensure cypress closes the web dev server
Browse files Browse the repository at this point in the history
  • Loading branch information
FrozenPandaz committed Feb 9, 2024
1 parent 114faf8 commit 2adfe20
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions packages/cypress/plugins/cypress-preset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ 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';
import * as os from 'node:os';

// Importing the cypress type here causes the angular and next unit
// tests to fail when transpiling, it seems like the cypress types are
Expand Down Expand Up @@ -65,14 +66,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');
};
}

Expand Down

0 comments on commit 2adfe20

Please sign in to comment.