use runInlineTest, make outputlines local

This commit is contained in:
Simon Knott 2024-11-07 10:46:03 +01:00
parent 02284ed4aa
commit 5ae52f072f
No known key found for this signature in database
GPG key ID: 8CEDC00028084AEC
2 changed files with 20 additions and 38 deletions

View file

@ -151,17 +151,9 @@ export class TestChildProcess {
this.exitCode = this.exited.then(r => r.exitCode);
}
outputLines(options: { prefix?: string } = {}): string[] {
outputLines(): string[] {
const strippedOutput = stripAnsi(this.output);
return strippedOutput
.split('\n')
.map(line => {
if (options.prefix && line.startsWith(options.prefix))
return line.substring(options.prefix.length);
return line;
})
.filter(line => line.startsWith('%%'))
.map(line => line.substring(2).trim());
return strippedOutput.split('\n').filter(line => line.startsWith('%%')).map(line => line.substring(2).trim());
}
async kill(signal: 'SIGINT' | 'SIGKILL' = 'SIGKILL') {

View file

@ -17,6 +17,7 @@
import type http from 'http';
import path from 'path';
import { test, expect, parseTestRunnerOutput } from './playwright-test-fixtures';
import type { RunResult } from './playwright-test-fixtures';
import { createHttpServer } from '../../packages/playwright-core/lib/utils/network';
const SIMPLE_SERVER_PATH = path.join(__dirname, 'assets', 'simple-server.js');
@ -775,40 +776,29 @@ test.describe('kill option', () => {
};
};
test('sends SIGKILL by default', async ({ interactWithTestRunner }) => {
const testProcess = await interactWithTestRunner(files(), { workers: 1 });
function parseOutputLines(result: RunResult): string[] {
const prefix = '[WebServer] %%';
return result.output.split('\n').filter(line => line.startsWith(prefix)).map(line => line.substring(prefix.length));
}
await testProcess.waitForOutput('webserver started');
process.kill(testProcess.process.pid!, 'SIGINT');
await testProcess.exited;
expect(testProcess.outputLines({ prefix: '[WebServer] ' })).toEqual([]);
test('sends SIGKILL by default', async ({ runInlineTest }) => {
const result = await runInlineTest(files(), { workers: 1 });
expect(parseOutputLines(result)).toEqual([]);
});
test('can be configured to send SIGTERM', async ({ interactWithTestRunner }) => {
const testProcess = await interactWithTestRunner(files({ kill: { SIGTERM: 500 } }), { workers: 1 });
await testProcess.waitForOutput('webserver started');
process.kill(testProcess.process.pid!, 'SIGINT');
await testProcess.exited;
expect(testProcess.outputLines({ prefix: '[WebServer] ' })).toEqual(['webserver received SIGTERM but stubbornly refuses to wind down']);
test('can be configured to send SIGTERM', async ({ runInlineTest }) => {
const result = await runInlineTest(files({ kill: { SIGTERM: 500 } }), { workers: 1 });
expect(parseOutputLines(result)).toEqual(['webserver received SIGTERM but stubbornly refuses to wind down']);
});
test('can be configured to send SIGINT', async ({ interactWithTestRunner }) => {
const testProcess = await interactWithTestRunner(files({ kill: { SIGINT: 500 } }), { workers: 1 });
await testProcess.waitForOutput('webserver started');
process.kill(testProcess.process.pid!, 'SIGINT');
await testProcess.exited;
expect(testProcess.outputLines({ prefix: '[WebServer] ' })).toEqual(['webserver received SIGINT but stubbornly refuses to wind down']);
test('can be configured to send SIGINT', async ({ runInlineTest }) => {
const result = await runInlineTest(files({ kill: { SIGINT: 500 } }), { workers: 1 });
expect(parseOutputLines(result)).toEqual(['webserver received SIGINT but stubbornly refuses to wind down']);
});
test('throws when mixed', async ({ interactWithTestRunner }) => {
const testProcess = await interactWithTestRunner(files({ kill: { SIGINT: 500, SIGTERM: 500 } }), { workers: 1 });
await testProcess.exited;
expect(testProcess.output).toContain('Only one of SIGINT or SIGTERM can be specified in config.webServer.kill');
test('throws when mixed', async ({ runInlineTest }) => {
const result = await runInlineTest(files({ kill: { SIGINT: 500, SIGTERM: 500 } }), { workers: 1 });
expect(result.exitCode).toBe(1);
expect(result.output).toContain('Only one of SIGINT or SIGTERM can be specified in config.webServer.kill');
});
});