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
23 changes: 16 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,17 @@ module.exports = {
- `skipInitialization` <[boolean]>. Add you ability to skip first setup `playwright` process. Possible use cases can be found [here](https://github.com/playwright-community/jest-playwright/issues/424)
- `useDefaultBrowserType` <[boolean]>. [Sometimes](https://github.com/microsoft/playwright/issues/2787) `browser` + `device` combinations don't have any sense. With this option tests will be run with [`defaultBrowserType`](https://github.com/microsoft/playwright/pull/3731) of device. Pay attention that you should define **devices** to correct usage of this option.

### Usage of process environment to define browser

You can control the browser with passing environment variable.

```js
// jest-playwright.config.js
module.exports = {
browsers: [process.env.BROWSER],
}
```

### Specific browser options

For `launchOptions`, `connectOptions` and `contextOptions` you can define special browser options.
Expand Down Expand Up @@ -185,11 +196,6 @@ module.exports = {
}
```

### Notes

You can also specify browser with the `BROWSER` environment variable. You should do it only if you are using the whole playwright package.
You can specify device with `DEVICE` environment variable.

## Globals

- `browserName` <[string]> - name of the current browser (chromium, firefox or webkit)
Expand Down Expand Up @@ -519,11 +525,14 @@ It's important to not change the `testEnvironment` to `node`. Otherwise it won't

### Error reporting with Jest

If you face into error messages like
If you face into error messages like

```
UnhandledPromiseRejectionWarning: Error: Protocol error (Runtime.callFunctionOn): Target closed.
```
```

or

```
Timeout - Async callback was not invoked within the 20000ms timeout specified by jest.setTimeout.Timeout - Async callback was not invoked within the 20000ms timeout specified by jest.setTimeout.Error:
```
Expand Down
3 changes: 1 addition & 2 deletions src/PlaywrightEnvironment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import {
getBrowserOptions,
getBrowserType,
getDeviceBrowserType,
getDeviceType,
getPlaywrightInstance,
} from './utils'
import { saveCoverageOnPage, saveCoverageToFile } from './coverage'
Expand Down Expand Up @@ -205,7 +204,7 @@ export const getPlaywrightEnv = (basicEnv = 'node'): unknown => {
browserName,
this._jestPlaywrightConfig.contextOptions,
)
const device = getDeviceType(this._config.device)
const device = this._config.device
const deviceName: Nullable<string> = getDeviceName(device)
const {
name,
Expand Down
20 changes: 2 additions & 18 deletions src/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { Playwright } from '../types/global'
const {
readConfig,
getBrowserType,
getDeviceType,
checkBrowserEnv,
checkDeviceEnv,
checkDevice,
Expand Down Expand Up @@ -172,11 +171,9 @@ describe('getBrowserType', () => {
const browserType = getBrowserType()
expect(browserType).toBe(CHROMIUM)
})
it('should return BROWSER if defined', async () => {
process.env.BROWSER = 'webkit'
it('should return passed browser if it is passed', async () => {
const browserType = getBrowserType('firefox')
expect(browserType).toBe(process.env.BROWSER)
delete process.env.BROWSER
expect(browserType).toBe('firefox')
})
})

Expand Down Expand Up @@ -205,19 +202,6 @@ describe('getBrowserOptions', () => {
})
})

describe('getDeviceType', () => {
it('should return "null" when there is no device', async () => {
const device = getDeviceType(null)
expect(device).toBe(null)
})
it('should return BROWSER if defined', async () => {
process.env.DEVICE = 'iPhone 11'
const device = getDeviceType(null)
expect(device).toBe(process.env.DEVICE)
delete process.env.DEVICE
})
})

describe('checkBrowserEnv', () => {
it('should throw Error with unknown type', async () => {
const browserType = getBrowserType('unknown' as BrowserType)
Expand Down
9 changes: 0 additions & 9 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,16 +104,7 @@ export const getDisplayName = (
return `browser: ${browser}`
}

export const getDeviceType = (device: DeviceType): DeviceType => {
const processDevice = process.env.DEVICE
return processDevice || device
}

export const getBrowserType = (browser?: BrowserType): BrowserType => {
const processBrowser = process.env.BROWSER
if (processBrowser) {
return processBrowser as BrowserType
}
return browser || CHROMIUM
}

Expand Down