From 5ae52f072fc68f131fec65e008ae5cd328c4d45b Mon Sep 17 00:00:00 2001 From: Simon Knott Date: Thu, 7 Nov 2024 10:46:03 +0100 Subject: [PATCH] use runInlineTest, make outputlines local --- tests/config/commonFixtures.ts | 12 ++----- tests/playwright-test/web-server.spec.ts | 46 ++++++++++-------------- 2 files changed, 20 insertions(+), 38 deletions(-) diff --git a/tests/config/commonFixtures.ts b/tests/config/commonFixtures.ts index fddbd3bd83..57f1d76373 100644 --- a/tests/config/commonFixtures.ts +++ b/tests/config/commonFixtures.ts @@ -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') { diff --git a/tests/playwright-test/web-server.spec.ts b/tests/playwright-test/web-server.spec.ts index e5e42f2643..5b4841a54d 100644 --- a/tests/playwright-test/web-server.spec.ts +++ b/tests/playwright-test/web-server.spec.ts @@ -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'); }); });