From 7b27d70d8af324694a3e1097542f754c5d816355 Mon Sep 17 00:00:00 2001 From: Max Schmitt Date: Wed, 26 Apr 2023 23:39:42 +0200 Subject: [PATCH] feat(web-server): add stdout: "pipe"|"ignore" option (#22564) Fixes https://github.com/microsoft/playwright/issues/22454 --- docs/src/test-api/class-testconfig.md | 1 + docs/src/test-webserver-js.md | 1 + .../src/plugins/webServerPlugin.ts | 3 ++- packages/playwright-test/types/test.d.ts | 6 +++++ tests/playwright-test/web-server.spec.ts | 23 +++++++++++++++++++ 5 files changed, 33 insertions(+), 1 deletion(-) diff --git a/docs/src/test-api/class-testconfig.md b/docs/src/test-api/class-testconfig.md index 9d953d3f02..e4ac5de1a4 100644 --- a/docs/src/test-api/class-testconfig.md +++ b/docs/src/test-api/class-testconfig.md @@ -599,6 +599,7 @@ export default defineConfig({ - `ignoreHTTPSErrors` ?<[boolean]> Whether to ignore HTTPS errors when fetching the `url`. Defaults to `false`. - `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"`. - `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 1586df74c4..779e7d6b31 100644 --- a/docs/src/test-webserver-js.md +++ b/docs/src/test-webserver-js.md @@ -28,6 +28,7 @@ export default defineConfig({ | `command`| Shell command to start the local dev server of your app. | | `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"`. | ## Adding a server timeout diff --git a/packages/playwright-test/src/plugins/webServerPlugin.ts b/packages/playwright-test/src/plugins/webServerPlugin.ts index 987f1a57b4..6d933f63d2 100644 --- a/packages/playwright-test/src/plugins/webServerPlugin.ts +++ b/packages/playwright-test/src/plugins/webServerPlugin.ts @@ -34,6 +34,7 @@ export type WebServerPluginOptions = { reuseExistingServer?: boolean; cwd?: string; env?: { [key: string]: string; }; + stdout?: 'pipe' | 'ignore'; }; const DEFAULT_ENVIRONMENT_VARIABLES = { @@ -108,7 +109,7 @@ export class WebServerPlugin implements TestRunnerPlugin { launchedProcess.stderr!.on('data', line => this._reporter!.onStdErr?.('[WebServer] ' + line.toString())); launchedProcess.stdout!.on('data', line => { - if (debugWebServer.enabled) + 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 0e7a2ae06c..d854521ab5 100644 --- a/packages/playwright-test/types/test.d.ts +++ b/packages/playwright-test/types/test.d.ts @@ -6370,6 +6370,12 @@ interface TestConfigWebServer { */ reuseExistingServer?: boolean; + /** + * 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"`. + */ + stdout?: "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 0d463ccf84..a38f64373e 100644 --- a/tests/playwright-test/web-server.spec.ts +++ b/tests/playwright-test/web-server.spec.ts @@ -658,3 +658,26 @@ test('should check ipv4 and ipv6 with happy eyeballs when URL is passed', async expect(result.output).toContain(`HTTP GET: http://localhost:${port}/`); expect(result.output).toContain('WebServer available'); }); + +test('should forward stdout when set to "pipe"', 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}, + stdout: 'pipe', + } + }; + `, + }, undefined); + 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 +});