feat(web-server): add stdout: "pipe"|"ignore" option (#22564)

Fixes https://github.com/microsoft/playwright/issues/22454
This commit is contained in:
Max Schmitt 2023-04-26 23:39:42 +02:00 committed by GitHub
parent b7656e605a
commit 7b27d70d8a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 33 additions and 1 deletions

View File

@ -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.

View File

@ -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

View File

@ -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());
});
}

View File

@ -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.
*/

View File

@ -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
});