Skip to content

Commit

Permalink
Cherry Pick "feat(test-runner): mark launch as experimental (#7757)" #…
Browse files Browse the repository at this point in the history
…7770

Cherry pick of #7757
  • Loading branch information
JoelEinbinder authored Jul 21, 2021
1 parent dd4d0c6 commit d4b295d
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 87 deletions.
75 changes: 0 additions & 75 deletions docs/src/test-advanced.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ These options would be typically different between local development and CI oper
- `reportSlowTests: { max: number, threshold: number } | null` - Whether to report slow tests. When `null`, slow tests are not reported. Otherwise, tests that took more than `threshold` milliseconds are reported as slow, but no more than `max` number of them. Passing zero as `max` reports all slow tests that exceed the threshold.
- `shard: { total: number, current: number } | null` - [Shard](./test-parallel.md#shards) information.
- `updateSnapshots: boolean` - Whether to update expected snapshots with the actual results produced by the test run.
- `launch: { command: string, waitForPort?: number, waitForPortTimeout?: number, strict?: boolean, cwd?: string, env?: object }[]` - Launch a process before the tests will start. When using `waitForPort` it will wait until the server is available, see [launch server](#launching-a-development-web-server-during-the-tests) configuration for examples. `strict` will verify that the `waitForPort` port is available instead of using it by default.
- `workers: number` - The maximum number of concurrent worker processes to use for parallelizing tests.

Note that each [test project](#projects) can provide its own test suite options, for example two projects can run different tests by providing different `testDir`s. However, test run options are shared between all projects.
Expand Down Expand Up @@ -201,80 +200,6 @@ export const test = base.extend<{ saveLogs: void }>({
});
```

## Launching a development web server during the tests

To launch a server during the tests, use the `launch` option in the [configuration file](#configuration-object).

You can specify a port via `waitForPort` or additional environment variables, see [here](#configuration-object). When a port is specified, the server will wait for it to be available before starting. For continuous integration, you may want to use the `strict` option which ensures that the port is available before starting the server.

The port gets then passed over to Playwright as a [`param: baseURL`] when creating the context [`method: Browser.newContext`].

```js js-flavor=ts
// playwright.config.ts
import { PlaywrightTestConfig } from '@playwright/test';

const config: PlaywrightTestConfig = {
launch: {
command: 'npm run start',
waitForPort: 3000,
waitForPortTimeout: 120 * 1000,
strict: !!process.env.CI,
},
};

export default config;
```

```js js-flavor=js
// playwright.config.js
// @ts-check
/** @type {import('@playwright/test').PlaywrightTestConfig} */
const config = {
launch: {
command: 'npm run start',
waitForPort: 3000,
waitForPortTimeout: 120 * 1000,
strict: !!process.env.CI,
},
};

mode.exports = config;
```

Now you can use a relative path when navigating the page, or use `baseURL` fixture:

```js js-flavor=ts
// test.spec.ts
import { test } = from '@playwright/test';

test('test', async ({ page, baseURL }) => {
// baseURL is taken directly from your web server,
// e.g. http://localhost:3000
await page.goto(baseURL + '/bar');

// Alternatively, just use relative path, because baseURL is already
// set for the default context and page.
// For example, this will result in http://localhost:3000/foo
await page.goto('/foo');
});
```

```js js-flavor=js
// test.spec.js
const { test } = require('@playwright/test');

test('test', async ({ page, baseURL }) => {
// baseURL is taken directly from your web server,
// e.g. http://localhost:3000
await page.goto(baseURL + '/bar');

// Alternatively, just use relative path, because baseURL is already
// set for the default context and page.
// For example, this will result in http://localhost:3000/foo
await page.goto('/foo');
});
```

## Global setup and teardown

To set something up once before running all tests, use `globalSetup` option in the [configuration file](#configuration-object).
Expand Down
1 change: 0 additions & 1 deletion docs/src/test-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,6 @@ In addition to configuring [Browser] or [BrowserContext], videos or screenshots,
- `testIgnore`: Glob patterns or regular expressions that should be ignored when looking for the test files. For example, `'**/test-assets'`.
- `testMatch`: Glob patterns or regular expressions that match test files. For example, `'**/todo-tests/*.spec.ts'`. By default, Playwright Test runs `.*(test|spec)\.(js|ts|mjs)` files.
- `timeout`: Time in milliseconds given to each test.
- `launch: { command: string, waitForPort?: number, waitForPortTimeout?: number, strict?: boolean, cwd?: string, env?: object }` - Launch a process before the tests will start. When using `waitForPort` it will wait until the server is available, see [launch server](./test-advanced.md#launching-a-development-web-server-during-the-tests) configuration for examples. `strict` will verify that the `waitForPort` port is available instead of using it by default.
- `workers`: The maximum number of concurrent worker processes to use for parallelizing tests.

You can specify these options in the configuration file. Note that testing options are **top-level**, do not put them into the `use` section.
Expand Down
4 changes: 2 additions & 2 deletions src/test/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export class Loader {
this._fullConfig.shard = takeFirst(this._configOverrides.shard, this._config.shard, baseFullConfig.shard);
this._fullConfig.updateSnapshots = takeFirst(this._configOverrides.updateSnapshots, this._config.updateSnapshots, baseFullConfig.updateSnapshots);
this._fullConfig.workers = takeFirst(this._configOverrides.workers, this._config.workers, baseFullConfig.workers);
this._fullConfig.launch = takeFirst(toLaunchServers(this._configOverrides.launch), toLaunchServers(this._config.launch), baseFullConfig.launch);
this._fullConfig._launch = takeFirst(toLaunchServers(this._configOverrides._launch), toLaunchServers(this._config._launch), baseFullConfig._launch);

for (const project of projects)
this._addProject(project, this._fullConfig.rootDir);
Expand Down Expand Up @@ -435,7 +435,7 @@ const baseFullConfig: FullConfig = {
shard: null,
updateSnapshots: 'missing',
workers: 1,
launch: [],
_launch: [],
};

function resolveReporters(reporters: Config['reporter'], rootDir: string): ReporterDescription[]|undefined {
Expand Down
2 changes: 1 addition & 1 deletion src/test/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ export class Runner {
testFiles.forEach(file => allTestFiles.add(file));
}

const launchServers = await LaunchServers.create(config.launch);
const launchServers = await LaunchServers.create(config._launch);
let globalSetupResult: any;
if (config.globalSetup)
globalSetupResult = await (await this._loader.loadGlobalHook(config.globalSetup, 'globalSetup'))(this._loader.fullConfig());
Expand Down
12 changes: 6 additions & 6 deletions tests/playwright-test/launch-server.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ test('should create a server', async ({ runInlineTest }, { workerIndex }) => {
`,
'playwright.config.ts': `
module.exports = {
launch: {
_launch: {
command: 'node ${JSON.stringify(path.join(__dirname, 'assets', 'simple-server.js'))} ${port}',
waitForPort: ${port},
},
Expand Down Expand Up @@ -82,7 +82,7 @@ test('should create a server with environment variables', async ({ runInlineTest
`,
'playwright.config.ts': `
module.exports = {
launch: {
_launch: {
command: 'node ${JSON.stringify(path.join(__dirname, 'assets', 'simple-server.js'))} ${port}',
waitForPort: ${port},
env: {
Expand Down Expand Up @@ -110,7 +110,7 @@ test('should time out waiting for a server', async ({ runInlineTest }, { workerI
`,
'playwright.config.ts': `
module.exports = {
launch: {
_launch: {
command: 'node ${JSON.stringify(JSON.stringify(path.join(__dirname, 'assets', 'simple-server.js')))} ${port}',
waitForPort: ${port},
waitForPortTimeout: 100,
Expand Down Expand Up @@ -169,7 +169,7 @@ test('should be able to use an existing server when strict is false ', async ({
`,
'playwright.config.ts': `
module.exports = {
launch: {
_launch: {
command: 'node ${JSON.stringify(path.join(__dirname, 'assets', 'simple-server.js'))} ${port}',
waitForPort: ${port},
strict: false,
Expand Down Expand Up @@ -202,7 +202,7 @@ test('should throw when a server is already running on the given port and strict
`,
'playwright.config.ts': `
module.exports = {
launch: {
_launch: {
command: 'node ${JSON.stringify(path.join(__dirname, 'assets', 'simple-server.js'))} ${port}',
waitForPort: ${port},
strict: true,
Expand All @@ -228,7 +228,7 @@ test('should create multiple servers', async ({ runInlineTest }, { workerIndex }
`,
'playwright.config.ts': `
module.exports = {
launch: [{
_launch: [{
command: 'node ${JSON.stringify(path.join(__dirname, 'assets', 'simple-server.js'))} ${port1}',
waitForPort: ${port1},
},{
Expand Down
4 changes: 2 additions & 2 deletions types/test.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ interface ConfigBase {
/**
* Launch a web server before running tests.
*/
launch?: LaunchConfig | LaunchConfig[];
_launch?: LaunchConfig | LaunchConfig[];

/**
* The maximum number of concurrent worker processes to use for parallelizing tests.
Expand Down Expand Up @@ -275,7 +275,7 @@ export interface FullConfig {
shard: Shard;
updateSnapshots: UpdateSnapshots;
workers: number;
launch: LaunchConfig[];
_launch: LaunchConfig[];
}

export type TestStatus = 'passed' | 'failed' | 'timedOut' | 'skipped';
Expand Down

0 comments on commit d4b295d

Please sign in to comment.