Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

chore(test): hopefully fix ct tests on Windows (#37852)

Adam Gastineau63d41d2ae75a
diff --git a/tests/components/test-all.spec.js b/tests/components/test-all.spec.js
index b67bb1b9e7..a23d0f44c6 100644
--- a/tests/components/test-all.spec.js
+++ b/tests/components/test-all.spec.js
@@ -1,10 +1,11 @@
const { test, expect } = require('@playwright/test');
-const { spawn } = require('child_process');
+const { spawn, execSync } = require('child_process');
const fs = require('fs');
const path = require('path');
let activeChild = undefined;
+const isWindows = process.platform === 'win32';
for (const dir of fs.readdirSync(__dirname)) {
const folder = path.join(__dirname, dir);
@@ -30,26 +31,41 @@ for (const dir of fs.readdirSync(__dirname)) {
test.afterEach(async () => {
// Make sure to kill server even if timeout occurs
- if (activeChild) {
- activeChild.kill();
- activeChild = undefined;
- }
+ onExit();
});
async function run(command, args, folder) {
const child = spawn(command, args, {
cwd: folder,
stdio: 'pipe',
+ env: process.env,
shell: true,
- env: process.env
+ // On non-windows platforms, `detached: true` makes child process a leader of a new
+ // process group, making it possible to kill child process tree with `.kill(-pid)` command.
+ // @see https://nodejs.org/api/child_process.html#child_process_options_detached
+ detached: !isWindows,
});
activeChild = child;
child.stdout.on('data', data => process.stdout.write(data));
child.stderr.on('data', data => process.stdout.write(data));
- process.on('exit', () => {
- activeChild = undefined;
- child.kill();
- });
+ process.on('exit', onExit);
const code = await new Promise(f => child.on('close', f));
expect(code).toEqual(0);
}
+
+function onExit() {
+ if (activeChild) {
+ try {
+ if (activeChild.exitCode !== null || activeChild.signalCode !== null)
+ return;
+
+ if (isWindows) {
+ execSync(`taskkill /pid ${activeChild.pid} /T /F`, { stdio: 'ignore' });
+ } else {
+ process.kill(-activeChild.pid, 'SIGKILL');
+ }
+ } finally {
+ activeChild = undefined;
+ }
+ }
+}