Skip to content

Commit 2df9344

Browse files
authored
Start testing full examples (cypress-io#151)
* add example running tool in a subfolder * add example to circleci * add local dev tools * install deps in root folder * print current folder * print output json file * use relative paths
1 parent c4d12d1 commit 2df9344

File tree

14 files changed

+3109
-0
lines changed

14 files changed

+3109
-0
lines changed

.circleci/config.yml

+26
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,31 @@ workflows:
6868
# and look at the server index file - should be fully covered
6969
- run: npx nyc report --check-coverage true --lines 100 --include test-backend/index.js
7070

71+
- cypress/run:
72+
name: example-before-each-visit
73+
requires:
74+
- cypress/install
75+
# install dependencies in the root folder
76+
post-install:
77+
- run: npm ci
78+
# there are no jobs to follow this one
79+
# so no need to save the workspace files (saves time)
80+
no-workspace: true
81+
working_directory: examples/before-each-visit
82+
# store screenshots and videos
83+
store_artifacts: true
84+
post-steps:
85+
- run: cat examples/before-each-visit/.nyc_output/out.json
86+
# store the created coverage report folder
87+
# you can click on it in the CircleCI UI
88+
# to see live static HTML site
89+
- store_artifacts:
90+
path: examples/before-each-visit/coverage
91+
# make sure the examples captures 100% of code
92+
- run:
93+
command: npx nyc report --check-coverage true --lines 100
94+
working_directory: examples/before-each-visit
95+
7196
- publish:
7297
filters:
7398
branches:
@@ -76,3 +101,4 @@ workflows:
76101
requires:
77102
- frontend coverage
78103
- backend coverage
104+
- example-before-each-visit

README.md

+40
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,46 @@ You can specify custom coverage reporter(s) to use. For example to output text s
181181

182182
**Tip:** find list of reporters [here](https://istanbul.js.org/docs/advanced/alternative-reporters/)
183183

184+
## Custom NYC command
185+
186+
Sometimes NYC tool might be installed in a different folder, or you might want to customize the report command. In that case, put the custom command into `package.json` in the current folder and this plugin will automatically use it.
187+
188+
```json
189+
{
190+
"scripts": {
191+
"coverage:report": "call NYC report ..."
192+
}
193+
}
194+
```
195+
196+
See examples below.
197+
198+
### Install NYC on the fly
199+
200+
The simplest solution: let `npx` install `nyc` on the fly
201+
202+
```json
203+
{
204+
"scripts": {
205+
"coverage:report": "npx nyc report --report-dir ./coverage --temp-dir .nyc_output --reporter=lcov --reporter=clover --reporter=json"
206+
}
207+
}
208+
```
209+
210+
### Find NYC in a parent folder
211+
212+
If you have [bin-up](https://github.com/bahmutov/bin-up) installed globally, you can use it to find `nyc` installed somewhere in the higher folder.
213+
214+
```json
215+
{
216+
"scripts": {
217+
"coverage:report": "bin-up nyc report --report-dir ./coverage --temp-dir .nyc_output --reporter=lcov --reporter=clover --reporter=json"
218+
}
219+
}
220+
```
221+
222+
**Tip:** finding and running pre-installed tool is always faster than installing it again and again.
223+
184224
## TypeScript users
185225

186226
TypeScript source files are NOT included in the code coverage report by default, even if they are properly instrumented. In order to tell `nyc` to include TS files in the report, you need to:

examples/before-each-visit/README.md

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# example: before-each-visit
2+
3+
Code coverage example where the `cy.visit` happens in `beforeEach` hook
4+
5+
Code was instrumented with
6+
7+
```shell
8+
npx nyc instrument --compact false main.js > main-instrumented.js
9+
```
10+
11+
and then removed absolute folder paths, leaving just relative path `main.js` in the produced file.
12+
13+
The code uses custom coverage report command in [package.json](package.json) to call `nyc`
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"name": "Using fixtures to represent data",
3+
"email": "hello@cypress.io",
4+
"body": "Fixtures are a great way to mock data for responses to routes"
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/// <reference types="cypress" />
2+
describe('coverage information', () => {
3+
beforeEach(() => {
4+
cy.visit('index.html')
5+
})
6+
7+
it('calls add', () => {
8+
cy.window()
9+
.invoke('add', 2, 3)
10+
.should('equal', 5)
11+
})
12+
13+
it('calls sub', () => {
14+
cy.window()
15+
.invoke('sub', 2, 3)
16+
.should('equal', -1)
17+
})
18+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = (on, config) => {
2+
on('task', require('../../../../task'))
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import '../../../../support'

examples/before-each-visit/index.html

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<body>
2+
Page body
3+
<script src="main-instrumented.js"></script>
4+
</body>

examples/before-each-visit/main-instrumented.js

+146
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/before-each-visit/main.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
window.add = (a, b) => a + b
2+
3+
window.sub = (a, b) => a - b

0 commit comments

Comments
 (0)