Skip to content

Commit

Permalink
Docs fixes (#202)
Browse files Browse the repository at this point in the history
* Fix device config

* Move playwright dependencies to 1.2.0

* Docs improvements

* Move playwright-core to dependencies section

* Fix merging connect options

* Fix merging connect options

* Fix connect options
  • Loading branch information
mmarkelov committed Jul 7, 2020
1 parent 0e7228d commit 560e847
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 92 deletions.
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,15 @@ Be sure to remove any existing `testEnvironment` option from your Jest configura

You can specify a `jest-playwright.config.js` at the root of the project or define a custom path using the `JEST_PLAYWRIGHT_CONFIG` environment variable. It should export a config object.

- `launchOptions` <[object]> [All Playwright launch options](https://github.com/microsoft/playwright/blob/master/docs/api.md#browsertypelaunchoptions) can be specified in config. Since it is JavaScript, you can use all stuff you need, including environment.
- `launchOptions` <[object]>. [All Playwright launch options](https://github.com/microsoft/playwright/blob/master/docs/api.md#browsertypelaunchoptions) can be specified in config. Since it is JavaScript, you can use all stuff you need, including environment.
- `launchType` <[**LAUNCH**](https://github.com/microsoft/playwright/blob/master/docs/api.md#browsertypelaunchoptions) | [**PERSISTENT**](https://github.com/microsoft/playwright/blob/master/docs/api.md#browsertypelaunchpersistentcontextuserdatadir-options) | [**SERVER**](https://github.com/microsoft/playwright/blob/master/docs/api.md#browsertypeconnectoptions)>. Method to launch browser instance. `jest-playwright` attaches Playwright to an existing browser instance by default.
- `connectOptions` <[object]>. [All Playwright connect options](https://github.com/microsoft/playwright/blob/master/docs/api.md#browsertypeconnectoptions) can be specified in config.
- `contextOptions` <[object]>. [All Playwright context options](https://github.com/microsoft/playwright/blob/master/docs/api.md#browsernewcontextoptions) can be specified in config.
- `browsers` <[string[]]>. Define [browsers](https://github.com/microsoft/playwright/blob/master/docs/api.md#class-browsertype) to run tests in.
- `chromium` Each test runs Chromium (default).
- `firefox` Each test runs Firefox.
- `webkit` Each test runs Webkit.
- `devices` <[(string | object)[] | RegExp]>. Define a [devices](https://github.com/microsoft/playwright/blob/master/docs/api.md#browsertypedevices) to run tests in. Actual list of devices can be found [here](https://github.com/Microsoft/playwright/blob/master/src/deviceDescriptors.ts).

- `exitOnPageError` <[boolean]>. Exits process on any page error. Defaults to `true`.
- `collectCoverage` <[boolean]>. Enables the coverage collection of the `saveCoverage(page)` calls to the `.nyc_output/coverage.json` file.
- `serverOptions` <[object]>. [All `jest-dev-server` options](https://github.com/smooth-code/jest-puppeteer/tree/master/packages/jest-dev-server#options).
Expand All @@ -93,7 +93,7 @@ You can specify a `jest-playwright.config.js` at the root of the project or defi

There are different ways to define browsers in your tests:

- You can you array of device names:
- You can use array of device names:

```js
module.exports = {
Expand Down Expand Up @@ -408,6 +408,10 @@ in your tests at the top. (30 seconds is the default Playwright timeout for wait

If for your individual tests a new entire browser instance spins up each time and it won't be reused, then you probably run them in parallel. If you run them in a synchronous way with the `--runInBand` CLI option for Jest, then the same browser instance will be re-used and this should fix the issue.

## Examples

Demonstration the usage of `jest-playwright` for various test cases can be found in [`playwright-jest-examples`](https://github.com/playwright-community/playwright-jest-examples)

## Inspiration

Thanks to [Smooth Code](https://github.com/smooth-code) for the great [jest-puppeteer](https://github.com/smooth-code/jest-puppeteer).
Expand Down
104 changes: 29 additions & 75 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"jest-environment-node": "^26.0.1",
"jest-process-manager": "^0.2.3",
"nyc": "^15.1.0",
"playwright-core": ">=1.2.0",
"rimraf": "^3.0.2",
"uuid": "^8.1.0"
},
Expand All @@ -69,9 +70,8 @@
"husky": "4.2.5",
"jest": "26.1.0",
"lint-staged": "10.2.11",
"playwright": ">=1.1.1",
"playwright-chromium": ">=1.1.1",
"playwright-core": "npm:playwright-chromium@>=1.1.1",
"playwright": ">=1.2.0",
"playwright-chromium": ">=1.2.0",
"prettier": "2.0.5",
"ts-jest": "26.1.1",
"typescript": "3.9.6"
Expand Down
19 changes: 10 additions & 9 deletions src/PlaywrightEnvironment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,19 +83,20 @@ export const getPlaywrightEnv = (basicEnv = 'node'): unknown => {
async setup(): Promise<void> {
const { rootDir, wsEndpoint, browserName } = this._config
this._jestPlaywrightConfig = await readConfig(rootDir)
if (
wsEndpoint &&
!this._jestPlaywrightConfig.connectOptions?.wsEndpoint
) {
this._jestPlaywrightConfig.connectOptions = { wsEndpoint }
}
const browserType = getBrowserType(browserName)
const {
connectOptions,
collectCoverage,
exitOnPageError,
selectors,
collectCoverage,
launchType,
} = this._jestPlaywrightConfig
if (wsEndpoint && !connectOptions?.wsEndpoint) {
this._jestPlaywrightConfig.connectOptions = {
...connectOptions,
wsEndpoint,
}
}
const browserType = getBrowserType(browserName)
let contextOptions = getBrowserOptions(
browserName,
this._jestPlaywrightConfig.contextOptions,
Expand Down Expand Up @@ -259,7 +260,7 @@ export const getPlaywrightEnv = (basicEnv = 'node'): unknown => {
})
},
saveCoverage: async (page: Page): Promise<void> =>
saveCoverageOnPage(page, this._jestPlaywrightConfig.collectCoverage),
saveCoverageOnPage(page, collectCoverage),
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { JestPlaywrightConfig } from './types'
import type { JestPlaywrightConfig, ConnectOptions } from './types'

export const IMPORT_KIND_PLAYWRIGHT = 'playwright'

Expand All @@ -13,6 +13,7 @@ export const SERVER = 'SERVER'
export const DEFAULT_CONFIG: JestPlaywrightConfig = {
launchType: SERVER,
launchOptions: {},
connectOptions: {} as ConnectOptions,
contextOptions: {},
browsers: [CHROMIUM],
exitOnPageError: true,
Expand Down
2 changes: 1 addition & 1 deletion src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ type LaunchType = typeof LAUNCH | typeof SERVER | typeof PERSISTENT

type Options<T> = T & Partial<Record<BrowserType, T>>

type ConnectOptions = Parameters<GenericBrowser['connect']>[0]
export type ConnectOptions = Parameters<GenericBrowser['connect']>[0]

export interface JestPlaywrightConfig {
launchType?: LaunchType
Expand Down

0 comments on commit 560e847

Please sign in to comment.