From 89f194050929d934e8ac341ce1f72319ad8cec22 Mon Sep 17 00:00:00 2001 From: Max Schmitt Date: Wed, 17 May 2023 00:46:59 +0200 Subject: [PATCH] chore: be able to hide webServer stderr (#23063) Follow-up to https://github.com/microsoft/playwright/pull/22564. --- docs/src/test-api/class-testconfig.md | 1 + docs/src/test-webserver-js.md | 1 + .../src/plugins/webServerPlugin.ts | 6 ++++- packages/playwright-test/types/test.d.ts | 5 ++++ tests/playwright-test/web-server.spec.ts | 24 ++++++++++++++++++- 5 files changed, 35 insertions(+), 2 deletions(-) diff --git a/docs/src/test-api/class-testconfig.md b/docs/src/test-api/class-testconfig.md index e8ac912802..1555a7a0b7 100644 --- a/docs/src/test-api/class-testconfig.md +++ b/docs/src/test-api/class-testconfig.md @@ -571,6 +571,7 @@ export default defineConfig({ - `timeout` ?<[int]> How long to wait for the process to start up and be available in milliseconds. Defaults to 60000. - `reuseExistingServer` ?<[boolean]> If true, it will re-use an existing server on the `port` or `url` when available. If no server is running on that `port` or `url`, it will run the command to start a new server. If `false`, it will throw if an existing process is listening on the `port` or `url`. This should be commonly set to `!process.env.CI` to allow the local dev server when running tests locally. - `stdout` ?<["pipe"|"ignore"]> If `"pipe"`, it will pipe the stdout of the command to the process stdout. If `"ignore"`, it will ignore the stdout of the command. Default to `"ignore"`. + - `stderr` ?<["pipe"|"ignore"]> Whether to pipe the stderr of the command to the process stderr or ignore it. Defaults to `"pipe"`. - `cwd` ?<[string]> Current working directory of the spawned process, defaults to the directory of the configuration file. - `env` ?<[Object]<[string], [string]>> Environment variables to set for the command, `process.env` by default. diff --git a/docs/src/test-webserver-js.md b/docs/src/test-webserver-js.md index bd0acb26f2..478dbb8cb4 100644 --- a/docs/src/test-webserver-js.md +++ b/docs/src/test-webserver-js.md @@ -29,6 +29,7 @@ export default defineConfig({ | `url`| URL of your http server that is expected to return a 2xx, 3xx, 400, 401, 402, or 403 status code when the server is ready to accept connections. | | `reuseExistingServer`| If `true`, it will re-use an existing server on the url when available. If no server is running on that url, it will run the command to start a new server. If `false`, it will throw if an existing process is listening on the url. To see the stdout, you can set the `DEBUG=pw:webserver` environment variable. | | `stdout` | If `"pipe"`, it will pipe the stdout of the command to the process stdout. If `"ignore"`, it will ignore the stdout of the command. Default to `"ignore"`. | +| `stderr` | Whether to pipe the stderr of the command to the process stderr or ignore it. Defaults to `"pipe"`. | ## Adding a server timeout diff --git a/packages/playwright-test/src/plugins/webServerPlugin.ts b/packages/playwright-test/src/plugins/webServerPlugin.ts index 5582ef1aa2..f48fa379ed 100644 --- a/packages/playwright-test/src/plugins/webServerPlugin.ts +++ b/packages/playwright-test/src/plugins/webServerPlugin.ts @@ -35,6 +35,7 @@ export type WebServerPluginOptions = { cwd?: string; env?: { [key: string]: string; }; stdout?: 'pipe' | 'ignore'; + stderr?: 'pipe' | 'ignore'; }; const DEFAULT_ENVIRONMENT_VARIABLES = { @@ -107,7 +108,10 @@ export class WebServerPlugin implements TestRunnerPlugin { debugWebServer(`Process started`); - launchedProcess.stderr!.on('data', line => this._reporter!.onStdErr?.('[WebServer] ' + line.toString())); + launchedProcess.stderr!.on('data', line => { + if (debugWebServer.enabled || (this._options.stderr === 'pipe' || !this._options.stderr)) + this._reporter!.onStdErr?.('[WebServer] ' + line.toString()); + }); launchedProcess.stdout!.on('data', line => { if (debugWebServer.enabled || this._options.stdout === 'pipe') this._reporter!.onStdOut?.('[WebServer] ' + line.toString()); diff --git a/packages/playwright-test/types/test.d.ts b/packages/playwright-test/types/test.d.ts index 6cb8fbc4dd..0e4639682d 100644 --- a/packages/playwright-test/types/test.d.ts +++ b/packages/playwright-test/types/test.d.ts @@ -6504,6 +6504,11 @@ interface TestConfigWebServer { */ stdout?: "pipe"|"ignore"; + /** + * Whether to pipe the stderr of the command to the process stderr or ignore it. Defaults to `"pipe"`. + */ + stderr?: "pipe"|"ignore"; + /** * Current working directory of the spawned process, defaults to the directory of the configuration file. */ diff --git a/tests/playwright-test/web-server.spec.ts b/tests/playwright-test/web-server.spec.ts index a38f64373e..abf3743be4 100644 --- a/tests/playwright-test/web-server.spec.ts +++ b/tests/playwright-test/web-server.spec.ts @@ -679,5 +679,27 @@ test('should forward stdout when set to "pipe"', async ({ runInlineTest }, { wor expect(result.exitCode).toBe(0); expect(result.passed).toBe(1); expect(result.output).toContain('[WebServer] listening'); - expect(result.output).toContain('[WebServer] error from server'); // stderr is always getting forwarded + expect(result.output).toContain('[WebServer] error from server'); // stderr is piped by default +}); + +test('should be able to ignore "stderr"', async ({ runInlineTest }, { workerIndex }) => { + const port = workerIndex * 2 + 10500; + const result = await runInlineTest({ + 'test.spec.ts': ` + import { test, expect } from '@playwright/test'; + test('pass', async ({}) => {}); + `, + 'playwright.config.ts': ` + module.exports = { + webServer: { + command: 'node ${JSON.stringify(SIMPLE_SERVER_PATH)} ${port}', + port: ${port}, + stderr: 'ignore', + } + }; + `, + }, undefined); + expect(result.exitCode).toBe(0); + expect(result.passed).toBe(1); + expect(result.output).not.toContain('error from server'); });