Skip to content

Commit fd6370b

Browse files
committed
coverage report
1 parent 4ca6871 commit fd6370b

File tree

10 files changed

+2720
-97
lines changed

10 files changed

+2720
-97
lines changed

README.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,59 @@ We do not want to instrument the source code in production. Thus let's move the
7979
```
8080

8181
If you run `npm run develop`, the `window.__coverage__` object will not be defined. If you execute `NODE_ENV=develop npm run develop` the code will be instrumented.
82+
83+
Let's add [Cypress](https://github.com/cypress-io/cypress) and its [code coverage plugin](https://github.com/cypress-io/code-coverage)
84+
85+
```
86+
$ npm i -D cypress @cypress/code-coverage
87+
88+
89+
```
90+
91+
We can quickly scaffold a spec file to simply load the page and assert the "Hello" is there.
92+
93+
```
94+
$ npx @bahmutov/cly init
95+
```
96+
97+
Let's use [start-server-and-test]() to start Gatsby and open Cypress
98+
99+
```
100+
$ npm i -D start-server-and-test
101+
102+
```
103+
104+
In `package.json` define the scripts
105+
106+
```json
107+
{
108+
"scripts": {
109+
"develop": "gatsby develop",
110+
"cy:open": "cypress open",
111+
"dev": "NODE_ENV=develop start-test develop 8000 cy:open"
112+
}
113+
}
114+
```
115+
116+
Follow the instructions in the code coverage plugin to add it to the plugins and support files. Then use the command `npm run dev` to open Cypress. Run the test
117+
118+
```js
119+
// cypress/integration/spec.js
120+
it("shows the Hello page", () => {
121+
cy.visit("/").contains("Hello world!")
122+
})
123+
```
124+
125+
Notice the code coverage log messages
126+
127+
![Code coverage messages](images/coverage-messages.png)
128+
129+
Open the coverage HTML report (there are reports in different formats sorted in the folder `coverage)
130+
131+
```
132+
$ open coverage/lcov-report/index.html
133+
```
134+
135+
Inspect the report
136+
137+
![Coverage report](images/coverage-report.png)

cypress.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"baseUrl": "http://localhost:8000",
3+
"fixturesFolder": false
4+
}

cypress/README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Cypress.io end-to-end tests
2+
3+
[Cypress.io](https://www.cypress.io) is an open source, MIT licensed end-to-end test runner
4+
5+
## Folder structure
6+
7+
These folders hold end-to-end tests and supporting files for the Cypress Test Runner.
8+
9+
- [fixtures](fixtures) holds optional JSON data for mocking, [read more](https://on.cypress.io/fixture)
10+
- [integration](integration) holds the actual test files, [read more](https://on.cypress.io/writing-and-organizing-tests)
11+
- [plugins](plugins) allow you to customize how tests are loaded, [read more](https://on.cypress.io/plugins)
12+
- [support](support) file runs before all tests and is a great place to write or load additional custom commands, [read more](https://on.cypress.io/writing-and-organizing-tests#Support-file)
13+
14+
## `cypress.json` file
15+
16+
You can configure project options in the [../cypress.json](../cypress.json) file, see [Cypress configuration doc](https://on.cypress.io/configuration).
17+
18+
## More information
19+
20+
- [https://github.com/cypress.io/cypress](https://github.com/cypress.io/cypress)
21+
- [https://docs.cypress.io/](https://docs.cypress.io/)
22+
- [Writing your first Cypress test](http://on.cypress.io/intro)

cypress/integration/spec.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// enables intelligent code completion for Cypress commands
2+
// https://on.cypress.io/intelligent-code-completion
3+
/// <reference types="cypress" />
4+
it("shows the Hello page", () => {
5+
cy.visit("/").contains("Hello world!")
6+
})

cypress/plugins/index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module.exports = (on, config) => {
2+
require("@cypress/code-coverage/task")(on, config)
3+
4+
// add other tasks to be registered here
5+
6+
// IMPORTANT to return the config object
7+
// with the any changed environment variables
8+
return config
9+
}

cypress/support/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import "@cypress/code-coverage/support"

images/coverage-messages.png

221 KB
Loading

images/coverage-report.png

126 KB
Loading

0 commit comments

Comments
 (0)