Skip to content
This repository was archived by the owner on Sep 11, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 30 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ test('should display correct browser', async () => {

### Notes

It's recommend to use a separate Jest configuration `jest.e2e.config.js` for `jest-playwright` to gain speed improvments and by that to only use Playwright in the end-to-end tests. For that you have to use the `-c` flag when calling Jest and use the [`testMatch`](https://jestjs.io/docs/en/configuration#testmatch-arraystring) or [`testRegex`](https://jestjs.io/docs/en/configuration#testregex-string--arraystring) in your Jest config to split them.
It's recommend to use a separate Jest configuration `jest.e2e.config.js` for `jest-playwright` to gain speed improvements and by that to only use Playwright in the end-to-end tests. For that you have to use the `-c` flag when calling Jest and use the [`testMatch`](https://jestjs.io/docs/en/configuration#testmatch-arraystring) or [`testRegex`](https://jestjs.io/docs/en/configuration#testregex-string--arraystring) in your Jest config to split them.

Be sure to remove any existing `testEnvironment` option from your Jest configuration. The `jest-playwright-preset` preset needs to manage that option itself.

Expand Down Expand Up @@ -110,11 +110,11 @@ module.exports = {
- `launchType` <[**LAUNCH**](https://github.com/microsoft/playwright/blob/v1.7.0/docs/api.md#browsertypelaunchoptions) | [**PERSISTENT**](https://github.com/microsoft/playwright/blob/v1.7.0/docs/api.md#browsertypelaunchpersistentcontextuserdatadir-options) | [**SERVER**](https://github.com/microsoft/playwright/blob/v1.7.0/docs/api.md#browsertypelaunchserveroptions)>. 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/v1.7.0/docs/api.md#browsertypeconnectparams) can be specified in config.
- `contextOptions` <[object]>. [All Playwright context options](https://github.com/microsoft/playwright/blob/v1.7.0/docs/api.md#browsernewcontextoptions) can be specified in config.
- `browsers` <[string[]]>. Define [browsers](https://github.com/microsoft/playwright/blob/v1.7.0/docs/api.md#class-browsertype) to run tests in.
- [browsers](#browser-configuration) <[(string | object)[]]>. Define [browsers](https://github.com/microsoft/playwright/blob/v1.7.0/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/v1.7.0/docs/api.md#playwrightdevices) to run tests in. Actual list of devices can be found [here](https://github.com/Microsoft/playwright/blob/v1.7.0/src/server/deviceDescriptors.ts).
- [devices](#device-configuration) <[(string | object)[] | RegExp]>. Define a [devices](https://github.com/microsoft/playwright/blob/v1.7.0/docs/api.md#playwrightdevices) to run tests in. Actual list of devices can be found [here](https://github.com/Microsoft/playwright/blob/v1.7.0/src/server/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-process-manager` options](https://github.com/playwright-community/jest-process-manager#options).
Expand Down Expand Up @@ -152,9 +152,35 @@ module.exports = {
}
```

### Browser configuration

There are different ways to define devices in your configuration file:

- You can use array of browser names:

```js
module.exports = {
browsers: ["chromium", "webkit"],
...
}
```

- You can define custom browser. You can find out use cases [here](https://github.com/playwright-community/jest-playwright/issues/539):

```js
{
// Name of browser
name: 'chromium' | 'firefox' | 'webkit'
// Display name for test
displayName: string
...
// Browser options
}
```

### Device configuration

There are different ways to define browsers in your tests:
There are different ways to define devices in your configuration file:

- You can use array of device names:

Expand Down
28 changes: 20 additions & 8 deletions src/PlaywrightRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ import {
readConfig,
getPlaywrightInstance,
getBrowserOptions,
getBrowserType,
getDeviceBrowserType,
deepMerge,
} from './utils'
import {
DEFAULT_TEST_PLAYWRIGHT_TIMEOUT,
Expand All @@ -45,7 +47,10 @@ const getBrowserTest = ({
device,
}: BrowserTest): JestPlaywrightTest => {
const { displayName, testEnvironmentOptions } = test.context.config
const playwrightDisplayName = getDisplayName(browser, device)
const playwrightDisplayName = getDisplayName(
config.displayName || browser,
device,
)
return {
...test,
context: {
Expand Down Expand Up @@ -149,23 +154,30 @@ class PlaywrightRunner extends JestRunner {
}
} else {
for (const browser of browsers) {
checkBrowserEnv(browser)
const browserType = getBrowserType(
typeof browser === 'string' ? browser : browser?.name,
)
const browserConfig =
typeof browser === 'string'
? config
: deepMerge(config, browser || {})
checkBrowserEnv(browserType)
const { devices: availableDevices, instance } = getPlaywrightInstance(
browser,
browserType,
)
const resultDevices = getDevices(devices, availableDevices)
const wsEndpoint: WsEndpointType = await this.launchServer(
config,
getBrowserOptions(browser, connectOptions)?.wsEndpoint || null,
browser,
browserConfig,
getBrowserOptions(browserType, connectOptions)?.wsEndpoint || null,
browserType,
instance as GenericBrowser,
)

const browserTest = {
test: test as JestPlaywrightTest,
config,
config: browserConfig,
wsEndpoint,
browser,
browser: browserType,
}

if (resultDevices.length) {
Expand Down
5 changes: 1 addition & 4 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,7 @@ export const checkDevice = (
}
}

export const getDisplayName = (
browser: BrowserType,
device: DeviceType,
): string => {
export const getDisplayName = (browser: string, device: DeviceType): string => {
if (device !== null) {
if (typeof device === 'string') {
return `browser: ${browser} device: ${device}`
Expand Down
3 changes: 2 additions & 1 deletion types/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,8 @@ export interface JestPlaywrightConfig {
contextOptions?: Options<BrowserContextOptions>
userDataDir?: string
exitOnPageError: boolean
browsers: BrowserType[]
displayName?: string
browsers: (BrowserType | (JestPlaywrightConfig & { name: BrowserType }))[]
devices?: ConfigDeviceType[] | RegExp
useDefaultBrowserType?: boolean
serverOptions?: JestProcessManagerOptions
Expand Down