Skip to content
This repository was archived by the owner on Sep 11, 2025. It is now read-only.

Commit eb894fe

Browse files
authored
feat: collect coverage of end-to-end tests (#147)
1 parent d8212bf commit eb894fe

File tree

11 files changed

+562
-56
lines changed

11 files changed

+562
-56
lines changed

.eslintrc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ module.exports = {
1818
'@typescript-eslint/ban-ts-comment': 'off',
1919
'@typescript-eslint/no-non-null-assertion': 'off',
2020
'@typescript-eslint/no-var-requires': 'off',
21+
'@typescript-eslint/ban-types': 'off',
2122
},
2223
parser: '@typescript-eslint/parser',
2324
parserOptions: {

README.md

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,20 +75,19 @@ describe('What is my browser', () => {
7575
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.
7676

7777
- `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.
78-
- `connectOptions` <[object]> [All Playwright connect options](https://github.com/microsoft/playwright/blob/master/docs/api.md#browsertypeconnectoptions) can be specified in config.
79-
- `contextOptions` <[object]> [All Playwright context options](https://github.com/microsoft/playwright/blob/master/docs/api.md#browsernewcontextoptions) can be specified in config.
78+
- `connectOptions` <[object]>. [All Playwright connect options](https://github.com/microsoft/playwright/blob/master/docs/api.md#browsertypeconnectoptions) can be specified in config.
79+
- `contextOptions` <[object]>. [All Playwright context options](https://github.com/microsoft/playwright/blob/master/docs/api.md#browsernewcontextoptions) can be specified in config.
8080
- `browsers` <[string[]]>. Define [browsers](https://github.com/microsoft/playwright/blob/master/docs/api.md#class-browsertype) to run tests in.
8181
- `chromium` Each test runs Chromium (default).
8282
- `firefox` Each test runs Firefox.
8383
- `webkit` Each test runs Webkit.
8484
- `devices` <[string[]]>. 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).
85-
- `exitOnPageError` <[boolean]> Exits process on any page error. Defaults to `true`.
86-
- `serverOptions` <[object]> [All `jest-dev-server` options](https://github.com/smooth-code/jest-puppeteer/tree/master/packages/jest-dev-server#options).
85+
- `exitOnPageError` <[boolean]>. Exits process on any page error. Defaults to `true`.
86+
- `collectCoverage` <[boolean]>. Enables the coverage collection of the `saveCoverage(page)` calls to the `.nyc_output/coverage.json` file.
87+
- `serverOptions` <[object]>. [All `jest-dev-server` options](https://github.com/smooth-code/jest-puppeteer/tree/master/packages/jest-dev-server#options).
8788
- `selectors` <[array]>. Define [selectors](https://github.com/microsoft/playwright/blob/v0.11.1/docs/api.md#class-selectors). Each selector must be an object with name and script properties.
8889

89-
Usage with [query-selector-shadow-dom](https://github.com/Georgegriff/query-selector-shadow-dom):
90-
91-
`jest-playwright.config.js`:
90+
Usage with [query-selector-shadow-dom](https://github.com/Georgegriff/query-selector-shadow-dom) in `jest-playwright.config.js`:
9291

9392
```javascript
9493
const {
@@ -127,6 +126,30 @@ Debugging tests can be hard sometimes and it is very useful to be able to pause
127126
await jestPlaywright.debug()
128127
```
129128

129+
## Tracking the coverage
130+
131+
It's possible to track the coverage of the end-to-end tests with the [babel-plugin-istanbul](https://github.com/istanbuljs/babel-plugin-istanbul) Babel plugin configured. It needs to be included in the web application which you are gonna test otherwise it won't work. To use it, you have to set `collectCoverage` in the `jest-playwright.config.js` to `true` and add the corresponding `saveCoverage(page)` call to your tests like that:
132+
133+
```js
134+
afterEach(async () => {
135+
await jestPlaywright.saveCoverage(page)
136+
})
137+
```
138+
139+
With this change, it will write the coverage data to the `.nyc_output/coverage.json` file which can be transformed using [`nyc`](https://github.com/istanbuljs/nyc#readme) to the lcov format:
140+
141+
```
142+
npx nyc report --reporter=lcovonly
143+
```
144+
145+
or to HTML:
146+
147+
```
148+
npx nyc report --reporter=html
149+
```
150+
151+
which will create a HTML website in the `coverage` directory.
152+
130153
## Start a server
131154

132155
Jest Playwright integrates a functionality to start a server when running your test suite, like [jest-puppeteer](https://github.com/smooth-code/jest-puppeteer/blob/master/README.md#start-a-server). It automatically closes the server when tests are done.

0 commit comments

Comments
 (0)