From 785ca19e5188c9dab16ce5f526b9d074e5627233 Mon Sep 17 00:00:00 2001 From: Dmitry Gozman Date: Fri, 23 Aug 2024 03:52:27 -0700 Subject: [PATCH] fix(webserver): prefix each line of webserver output (#32286) This unflakes various `web-server.spec.ts` tests and makes the output more consistent. --- .../playwright/src/plugins/webServerPlugin.ts | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/packages/playwright/src/plugins/webServerPlugin.ts b/packages/playwright/src/plugins/webServerPlugin.ts index 96a369bddb..e2474f35f2 100644 --- a/packages/playwright/src/plugins/webServerPlugin.ts +++ b/packages/playwright/src/plugins/webServerPlugin.ts @@ -113,13 +113,13 @@ export class WebServerPlugin implements TestRunnerPlugin { debugWebServer(`Process started`); - launchedProcess.stderr!.on('data', line => { + launchedProcess.stderr!.on('data', data => { if (debugWebServer.enabled || (this._options.stderr === 'pipe' || !this._options.stderr)) - this._reporter!.onStdErr?.(colors.dim('[WebServer] ') + line.toString()); + this._reporter!.onStdErr?.(prefixOutputLines(data.toString())); }); - launchedProcess.stdout!.on('data', line => { + launchedProcess.stdout!.on('data', data => { if (debugWebServer.enabled || this._options.stdout === 'pipe') - this._reporter!.onStdOut?.(colors.dim('[WebServer] ') + line.toString()); + this._reporter!.onStdOut?.(prefixOutputLines(data.toString())); }); } @@ -201,3 +201,14 @@ export const webServerPluginsForConfig = (config: FullConfigInternal): TestRunne return webServerPlugins; }; + +function prefixOutputLines(output: string) { + const lastIsNewLine = output[output.length - 1] === '\n'; + let lines = output.split('\n'); + if (lastIsNewLine) + lines.pop(); + lines = lines.map(line => colors.dim('[WebServer] ') + line); + if (lastIsNewLine) + lines.push(''); + return lines.join('\n'); +}