Skip to content

Commit

Permalink
feat: add hapi middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
bahmutov committed Jun 25, 2019
1 parent 6cc65fc commit 95788c4
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 13 deletions.
25 changes: 13 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ You can also instrument your server-side code and produce combined coverage repo
```js
const express = require('express')
const app = express()
require('@cypress/code-coverage/middleware')(app)
require('@cypress/code-coverage/middleware/express')(app)
```

**Tip:** you can register the endpoint only if there is global code coverage object, and you can exclude the middleware code from the coverage numbers
Expand All @@ -82,24 +82,25 @@ require('@cypress/code-coverage/middleware')(app)
// https://github.com/gotwarlost/istanbul/blob/master/ignoring-code-for-coverage.md
/* istanbul ignore next */
if (global.__coverage__) {
require('@cypress/code-coverage/middleware')(app)
require('@cypress/code-coverage/middleware/express')(app)
}
```

If you use Hapi server, define the endpoint yourself and return the object

```js
// https://github.com/gotwarlost/istanbul/blob/master/ignoring-code-for-coverage.md
/* istanbul ignore next */
if (global.__coverage__) {
// https://hapijs.com/tutorials/routing?lang=en_US
server.route({
method: 'GET',
path: '/__coverage__',
handler () {
return { coverage: global.__coverage__ }
}
})
require('@cypress/code-coverage/middleware/hapi')(server)
}
```

For any other server, define the endpoint yourself and return the coverage object:

```js
if (global.__coverage__) {
// add method "GET /__coverage__" and response with JSON
onRequest = (response) =>
response.sendJSON({coverage: global.__coverage__ })
}
```

Expand Down
1 change: 1 addition & 0 deletions middleware.js → middleware/express.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// for Express.js
module.exports = app => {
// expose "GET __coverage__" endpoint that just returns
// global coverage information (if the application has been instrumented)
Expand Down
14 changes: 14 additions & 0 deletions middleware/hapi.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// for Hapi.js
module.exports = server => {
// expose "GET __coverage__" endpoint that just returns
// global coverage information (if the application has been instrumented)

// https://hapijs.com/tutorials/routing?lang=en_US
server.route({
method: 'GET',
path: '/__coverage__',
handler () {
return { coverage: global.__coverage__ }
}
})
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
},
"homepage": "https://github.com/cypress-io/code-coverage#readme",
"files": [
"*.js"
"*.js",
"middleware"
],
"publishConfig": {
"access": "public"
Expand Down

0 comments on commit 95788c4

Please sign in to comment.