Skip to content

Commit a1aaa74

Browse files
authored
chore: Enable code coverage for cypress tests (#10602)
1 parent bebd1cf commit a1aaa74

File tree

8 files changed

+588
-45
lines changed

8 files changed

+588
-45
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ dist
77
**/test/test-elements/generated
88
.dev-server-port
99

10+
# Code coverage
11+
coverage
12+
.nyc_output
13+
1014
# scoping feature generated entry points for vite consumption
1115
packages/compat/test/pages/scoped
1216
packages/main/test/pages/scoped

docs/4-development/10-testing.md

+39-26
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
The test framework of choice for UI5 Web Components is [WebdriverIO](https://webdriver.io/) or WDIO for short.
55
It has a straightforward API - [https://webdriver.io/docs/api.html](https://webdriver.io/docs/api.html), and has excellent support for Web Components.
66

7-
The browser of choice for test execution is [Google Chrome](https://www.google.com/chrome/), respectively the WebDriver used is [ChromeDriver](https://chromedriver.chromium.org/).
7+
The browser of choice for test execution is [Google Chrome](https://www.google.com/chrome/), respectively the WebDriver used is [ChromeDriver](https://chromedriver.chromium.org/).
88

99
### Prerequisites
1010

@@ -16,24 +16,24 @@ You can install it with `npm`:
1616
- `npm i --save-dev chromedriver`
1717

1818
or with `yarn`:
19-
- `yarn add -D chromedriver`
19+
- `yarn add -D chromedriver`
2020

2121
**Note:** Google Chrome and ChromeDriver need to be the same version to work together. Whenever you update Google Chrome on
2222
your system (or it updates automatically, if allowed), you are expected to also update ChromeDriver to the respective version.
2323

2424
### Running the tests
2525

26-
#### 1. Test configuration
26+
#### 1. Test configuration
2727

2828
The configuration for WDIO can be found in the `config/` directory under `wdio.conf.js`.
2929

30-
As explained [here](./01-package.md) in the section about the `config/` directory, you can
30+
As explained [here](./01-package.md) in the section about the `config/` directory, you can
3131
customize, or even completely replace the default configuration.
3232

3333
However, before doing so, please note the following two benefits of working with the default configuration, provided by UI5 Web Components:
34-
- Hooks, synchronizing the execution of all relevant WDIO commands (e.g. `click`, `url`, `$`, `$$`) with the rendering of the framework to
34+
- Hooks, synchronizing the execution of all relevant WDIO commands (e.g. `click`, `url`, `$`, `$$`) with the rendering of the framework to
3535
ensure consistency when reading or changing the state of the components.
36-
- Additional API methods: `setProperty`, `setAttribute`, `removeAttribute`, `hasClass`.
36+
- Additional API methods: `setProperty`, `setAttribute`, `removeAttribute`, `hasClass`.
3737

3838
So our recommendation would be to modify it, if necessary, but not completely replace it.
3939

@@ -74,7 +74,7 @@ describe("ui5-demo rendering", async () => {
7474
assert.ok(innerContent, "content rendered");
7575
});
7676
});
77-
```
77+
```
7878

7979
Key points:
8080
- Load the test page with the `browser.url` command. You can do this once for each test suite or for each individual test.
@@ -87,7 +87,7 @@ Key points:
8787

8888
For WDIO capabilities, see:
8989
- Official API: [https://webdriver.io/docs/api.html](https://webdriver.io/docs/api.html).
90-
- Additional commands provided in our standard WDIO configuration: `setProperty`, `setAttribute`, `removeAttribute`, `hasClass`.
90+
- Additional commands provided in our standard WDIO configuration: `setProperty`, `setAttribute`, `removeAttribute`, `hasClass`.
9191

9292
**Note:** The standard WDIO configuration we provide automatically synchronizes all test commands' execution with the framework rendering cycle.
9393
Therefore, in the example above, the `shadow$` command will internally wait for all rendering to be over before being executed. The
@@ -100,19 +100,19 @@ Debugging with WDIO is really simple. Just follow these 3 steps:
100100
1. Change the WDIO configuration file `config/wdio.conf.js` to disable `headless` mode for Google Chrome as follows:
101101

102102
From:
103-
103+
104104
```js
105105
module.exports = require("@ui5/webcomponents-tools/components-package/wdio.js");
106106
```
107-
107+
108108
to:
109109

110110
```js
111111
const result = require("@ui5/webcomponents-tools/components-package/wdio.js");
112112
result.config.capabilities[0]["goog:chromeOptions"].args = ['--disable-gpu']; // From: ['--disable-gpu', '--headless']
113113
module.exports = result;
114114
```
115-
115+
116116
If you happen to debug often, it's recommended to keep the file in this format and just comment out the middle line when you're done debugging.
117117

118118
2. Set a breakpoint with `browser.debug` somewhere in your test:
@@ -124,29 +124,29 @@ Debugging with WDIO is really simple. Just follow these 3 steps:
124124
assert.ok(innerContent, "content rendered");
125125
});
126126
```
127-
127+
128128
For more on `debug`, see [https://webdriver.io/docs/api/browser/debug.html](https://webdriver.io/docs/api/browser/debug.html).
129129

130130
3. Run the single test spec and wait for the browser to open and pause on your breakpoint:
131131

132132
- Run the dev server, if you haven't already:
133-
134-
`yarn start`
135-
136-
or
137-
133+
134+
`yarn start`
135+
136+
or
137+
138138
`npm run start`.
139139

140140
- Run the single test spec:
141-
142-
`yarn test test/specs/Demo.spec.js`
143-
144-
or
145-
141+
142+
`yarn test test/specs/Demo.spec.js`
143+
144+
or
145+
146146
`npm run test test/specs/Demo.spec.js`.
147-
148-
Google Chrome will then open in a new window, controlled by WDIO via the ChromeDriver, and your test will pause on your
149-
breakpoint of choice. Proceed to debug normally.
147+
148+
Google Chrome will then open in a new window, controlled by WDIO via the ChromeDriver, and your test will pause on your
149+
breakpoint of choice. Proceed to debug normally.
150150

151151
### Best practices for writing tests
152152

@@ -174,7 +174,7 @@ Use:
174174
Preferred:
175175
```js
176176
assert.ok(await browser.$(<SELECTOR>).isExisting())
177-
```
177+
```
178178

179179
instead of:
180180

@@ -433,3 +433,16 @@ describe("My Component Tests", () => {
433433
});
434434
```
435435

436+
437+
### Code coverage
438+
439+
Cypress tests automatically run with instrumentation switched on. To see the code coverage report, run the following commands:
440+
```sh
441+
# build the project
442+
yarn build
443+
# run the tests for a pacakge
444+
cd packages/main
445+
yarn test:cypress
446+
# start a static server in the `coverage` folder and inspect the results in the browser
447+
http-server coverage
448+
```

packages/tools/components-package/cypress.config.js

+5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
const { defineConfig } = require('cypress')
22
const path = require("path");
3+
const coverageTask = require('@cypress/code-coverage/task');
34

45
module.exports = defineConfig({
56
component: {
7+
setupNodeEvents(on, config) {
8+
coverageTask(on, config)
9+
return config
10+
},
611
supportFile: path.join(__dirname, "cypress/support/component.js"),
712
indexHtmlFile: path.join(__dirname, "cypress/support/component-index.html"),
813
specPattern: ["**/specs/*.cy.{js,ts}", "**/specs/**/*.cy.{js,ts}"],

packages/tools/components-package/cypress/support/component.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import '@cypress/code-coverage/support'
12
import { setupHooks } from '@cypress/mount-utils';
23
import { unsafeHTML } from 'lit-html/directives/unsafe-html.js';
34
import { mount } from 'cypress-ct-lit'

packages/tools/components-package/nps.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,8 @@ const getScripts = (options) => {
122122
},
123123
start: "nps prepare watch.devServer",
124124
test: `node "${LIB}/test-runner/test-runner.js"`,
125-
"test-cy-ci": `yarn cypress run --component --browser chrome`,
126-
"test-cy-open": `yarn cypress open --component --browser chrome`,
125+
"test-cy-ci": `CYPRESS_COVERAGE=true yarn cypress run --component --browser chrome`,
126+
"test-cy-open": `CYPRESS_COVERAGE=true yarn cypress open --component --browser chrome`,
127127
"test-suite-1": `node "${LIB}/test-runner/test-runner.js" --suite suite1`,
128128
"test-suite-2": `node "${LIB}/test-runner/test-runner.js" --suite suite2`,
129129
startWithScope: "nps scope.prepare scope.watchWithBundle",

packages/tools/package.json

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
},
2424
"dependencies": {
2525
"@custom-elements-manifest/analyzer": "^0.8.4",
26+
"@cypress/code-coverage": "^3.13.10",
2627
"@typescript-eslint/eslint-plugin": "^6.9.0",
2728
"@typescript-eslint/parser": "^6.9.0",
2829
"@wdio/cli": "^7.19.7",
@@ -70,6 +71,7 @@
7071
"rimraf": "^3.0.2",
7172
"slash": "3.0.0",
7273
"vite": "^5.4.8",
74+
"vite-plugin-istanbul": "^6.0.2",
7375
"wdio-chromedriver-service": "^7.3.2"
7476
},
7577
"peerDependencies": {

vite.config.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import customHotUpdate from "@ui5/webcomponents-tools/lib/dev-server/custom-hot-
44
import path, { dirname, join, resolve } from 'path';
55
import tsconfigPaths from 'vite-tsconfig-paths';
66
import { checker } from 'vite-plugin-checker';
7+
import istanbul from 'vite-plugin-istanbul';
78

89
// use after path.join and path.resolve as they turn paths to windows separators and comparisons and replacements stop working
910
const toPosixPath = (pathStr) => {
@@ -98,7 +99,6 @@ const customResolver = (id, source, options) => {
9899
return resolved;
99100
}
100101
}
101-
102102
export default defineConfig(async () => {
103103
return {
104104
build: {
@@ -114,7 +114,14 @@ export default defineConfig(async () => {
114114
tsconfigPath: "packages/fiori/tsconfig.json",
115115
buildMode: true,
116116
}
117-
})
117+
}),
118+
istanbul({
119+
include: ['packages/**/src/*','src/*'],
120+
exclude: ['node_modules', 'test/'],
121+
extension: ['.js', '.ts', '.tsx'],
122+
requireEnv: true,
123+
cypress: true,
124+
}),
118125
],
119126
resolve: {
120127
alias: [

0 commit comments

Comments
 (0)