Skip to content

feature: Allow multiple backend processes with urls config #339

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions examples/multiple-backends/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# example: multiple-backends

> Combined code coverage from multiple backend processes, and e2e and unit tests

This example runs instrumented server code on two processes, that serves instrumented frontend code, and instruments the unit tests on the fly. The final report combines all 4 sources of information.

To run

```sh
$ npm run dev
```

You should see messages from the plugin when it saves each coverage object

![Coverage messages](images/multiple-backends.png)

In the produced report, you should see

- `server1/server.js` coverage for backend
- `server2/server.js` coverage for backend
- `main.js` coverage from end-to-end tests
- `string-utils.js` coverage from unit tests
9 changes: 9 additions & 0 deletions examples/multiple-backends/cypress.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"fixturesFolder": false,
"baseUrl": "http://localhost:3003",
"env": {
"codeCoverage": {
"url": "http://localhost:3003/__coverage__,http://localhost:3004/__coverage__"
}
}
}
26 changes: 26 additions & 0 deletions examples/multiple-backends/cypress/integration/spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/// <reference types="Cypress" />

// load extra files to instrument on the fly
const { reverse } = require('../../string-utils')

it('uses frontend code and calls backend', () => {
cy.visit('/')
cy.contains('Page body').should('be.visible')

cy.window()
.invoke('add', 2, 3)
.should('equal', 5)

cy.window()
.invoke('sub', 2, 3)
.should('equal', -1)

cy.log('**backend request**')
cy.request('/hello')

cy.log('**other backend request**')
cy.request('http://localhost:3004/goodbye')

cy.log('**unit test**')
expect(reverse('Hello')).to.equal('olleH')
})
6 changes: 6 additions & 0 deletions examples/multiple-backends/cypress/plugins/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = (on, config) => {
require('../../../../task')(on, config)
// instrument loaded spec files (and the application code loaded from them)
on('file:preprocessor', require('../../../../use-browserify-istanbul'))
return config
}
1 change: 1 addition & 0 deletions examples/multiple-backends/cypress/support/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import '../../../../support'
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions examples/multiple-backends/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
window.add = (a, b) => a + b

window.sub = (a, b) => a - b
15 changes: 15 additions & 0 deletions examples/multiple-backends/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "example-multiple-backends",
"description": "Combined code coverage from multiple backend code bases, and e2e and unit tests",
"devDependencies": {
"npm-run-all": "^4.1.5"
},
"scripts": {
"start": "npm-run-all -p start:server1 start:server2",
"start:server1": "../../node_modules/.bin/nyc --silent node server1/server",
"start:server2": "../../node_modules/.bin/nyc --silent node server2/server",
"cy:open": "../../node_modules/.bin/cypress open",
"dev": "../../node_modules/.bin/start-test 3003 cy:open",
"report": "../../node_modules/.bin/nyc report --reporter text"
}
}
4 changes: 4 additions & 0 deletions examples/multiple-backends/server1/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<body>
Page body
<script src="main-instrumented.js"></script>
</body>
146 changes: 146 additions & 0 deletions examples/multiple-backends/server1/main-instrumented.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions examples/multiple-backends/server1/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const express = require('express')
const app = express()
const port = 3003

// if there is code coverage information
// then expose an endpoint that returns it
/* istanbul ignore next */
if (global.__coverage__) {
console.log('have code coverage, will add middleware for express')
console.log(`to fetch: GET :${port}/__coverage__`)
require('../../../middleware/express')(app)
}

app.use(express.static(__dirname))

app.get('/hello', (req, res) => {
console.log('sending hello world')
res.send('Hello World!')
})

app.listen(port, () => console.log(`Example app listening on port ${port}!`))
Loading