Skip to content

Commit 0367d1b

Browse files
authored
Add fullstack example (cypress-io#187)
* add more links to internal examples * start fullstack example * instrument specs on the fly * add screenshot and unit tests * add circle job * add link to example from README
1 parent 1fa46c8 commit 0367d1b

File tree

14 files changed

+294
-1
lines changed

14 files changed

+294
-1
lines changed

.circleci/config.yml

+33
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,38 @@ workflows:
104104
node ../../scripts/only-covered server.js
105105
working_directory: examples/backend
106106

107+
- cypress/run:
108+
name: fullstack coverage
109+
requires:
110+
- cypress/install
111+
# grab the workspace created by cypress/install job
112+
attach-workspace: true
113+
working_directory: examples/fullstack
114+
start: npm start
115+
wait-on: 'http://localhost:3003'
116+
command: '../../node_modules/.bin/cypress run'
117+
118+
# there are no jobs to follow this one
119+
# so no need to save the workspace files (saves time)
120+
no-workspace: true
121+
post-steps:
122+
# store the created coverage report folder
123+
# you can click on it in the CircleCI UI
124+
# to see live static HTML site
125+
- store_artifacts:
126+
path: examples/backend/coverage
127+
- run:
128+
command: npm run report
129+
working_directory: examples/fullstack
130+
- run:
131+
name: Check code coverage 📈
132+
command: |
133+
node ../../scripts/check-coverage server.js
134+
node ../../scripts/check-coverage main.js
135+
node ../../scripts/check-coverage string-utils.js
136+
node ../../scripts/only-covered server.js main.js string-utils.js
137+
working_directory: examples/fullstack
138+
107139
- cypress/run:
108140
attach-workspace: true
109141
name: example-before-each-visit
@@ -322,6 +354,7 @@ workflows:
322354
- unit
323355
- frontend coverage
324356
- backend coverage
357+
- fullstack coverage
325358
- example-before-each-visit
326359
- example-before-all-visit
327360
- example-ts-example

README.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -332,8 +332,13 @@ npm run dev:no:coverage
332332

333333
### Internal examples
334334

335-
- [examples/before-each-visit](examples/before-each-visit) checks if code coverage correctly keeps track of code when doing `cy.visit` before each test
335+
Full examples we use for testing in this repository:
336+
337+
- [examples/backend](examples/backend) only instruments the backend Node server and saves the coverage report
338+
- [examples/fullstack](examples/fullstack) instruments and merges backend, e2e and unit test coverage into a single report
336339
- [examples/before-all-visit](examples/before-all-visit) checks if code coverage works when `cy.visit` is made once in the `before` hook
340+
- [examples/before-each-visit](examples/before-each-visit) checks if code coverage correctly keeps track of code when doing `cy.visit` before each test
341+
- [examples/one-spec.js](examples/one-spec.js) confirms that coverage is collected and filtered correctly if the user only executes a single Cypress test
337342
- [examples/ts-example](examples/ts-example) uses Babel + Parcel to instrument and serve TypeScript file
338343

339344
### External examples

examples/fullstack/README.md

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# example: fullstack
2+
3+
> Combined code coverage from the backend code, and e2e and unit tests
4+
5+
This example runs instrumented server code, that serves instrumented frontend code, and instruments the unit tests on the fly. The final report combines all 3 sources of information.
6+
7+
To run
8+
9+
```sh
10+
$ npm run dev
11+
```
12+
13+
You should see messages from the plugin when it saves each coverage object
14+
15+
![Coverage messages](images/fullstack.png)
16+
17+
In the produced report, you should see
18+
19+
- `server/server.js` coverage for backend
20+
- `main.js` coverage from end-to-end tests
21+
- `string-utils.js` coverage from unit tests

examples/fullstack/cypress.json

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"fixturesFolder": false,
3+
"baseUrl": "http://localhost:3003",
4+
"env": {
5+
"codeCoverage": {
6+
"url": "http://localhost:3003/__coverage__"
7+
}
8+
}
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/// <reference types="Cypress" />
2+
3+
// load extra files to instrument on the fly
4+
const { reverse } = require('../../string-utils')
5+
6+
it('uses frontend code and calls backend', () => {
7+
cy.visit('/')
8+
cy.contains('Page body').should('be.visible')
9+
10+
cy.window()
11+
.invoke('add', 2, 3)
12+
.should('equal', 5)
13+
14+
cy.window()
15+
.invoke('sub', 2, 3)
16+
.should('equal', -1)
17+
18+
cy.log('**backend request**')
19+
cy.request('/hello')
20+
21+
cy.log('**unit test**')
22+
expect(reverse('Hello')).to.equal('olleH')
23+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = (on, config) => {
2+
require('../../../../task')(on, config)
3+
// instrument loaded spec files (and the application code loaded from them)
4+
on('file:preprocessor', require('../../../../use-browserify-istanbul'))
5+
return config
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import '../../../../support'
189 KB
Loading

examples/fullstack/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

examples/fullstack/package.json

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "example-fullstack",
3+
"description": "Combined code coverage from the backend code, and e2e and unit tests",
4+
"devDependencies": {},
5+
"scripts": {
6+
"start": "../../node_modules/.bin/nyc --silent node server/server",
7+
"cy:open": "../../node_modules/.bin/cypress open",
8+
"dev": "../../node_modules/.bin/start-test 3003 cy:open",
9+
"report": "../../node_modules/.bin/nyc report --reporter text"
10+
}
11+
}

examples/fullstack/server/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/fullstack/server/main-instrumented.js

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

examples/fullstack/server/server.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const express = require('express')
2+
const app = express()
3+
const port = 3003
4+
5+
// if there is code coverage information
6+
// then expose an endpoint that returns it
7+
/* istanbul ignore next */
8+
if (global.__coverage__) {
9+
console.log('have code coverage, will add middleware for express')
10+
console.log(`to fetch: GET :${port}/__coverage__`)
11+
require('../../../middleware/express')(app)
12+
}
13+
14+
app.use(express.static(__dirname))
15+
16+
app.get('/hello', (req, res) => {
17+
console.log('sending hello world')
18+
res.send('Hello World!')
19+
})
20+
21+
app.listen(port, () => console.log(`Example app listening on port ${port}!`))

examples/fullstack/string-utils.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// reverses a string
2+
const reverse = s => {
3+
return s
4+
.split('')
5+
.reverse()
6+
.join('')
7+
}
8+
module.exports = {
9+
reverse
10+
}

0 commit comments

Comments
 (0)