Skip to content

Commit

Permalink
feat: add Nextjs example and middleware, close #63
Browse files Browse the repository at this point in the history
  • Loading branch information
bahmutov committed Aug 18, 2019
1 parent 7f50613 commit 7e79816
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ For example, if you want to only include files in the `app` folder, but exclude
- [rootstrap/react-redux-base](https://github.com/rootstrap/react-redux-base) shows an example with a realistic Webpack config. Instruments the source code using `babel-plugin-istanbul` during tests.
- [skylock/cypress-angular-coverage-example](https://github.com/skylock/cypress-angular-coverage-example) shows Angular 8 + TypeScript application with instrumentation done using [istanbul-instrumenter-loader](https://github.com/webpack-contrib/istanbul-instrumenter-loader).
- [bahmutov/testing-react](https://github.com/bahmutov/testing-react) shows how to get code coverage for a React application created using [CRA v3](https://github.com/facebook/create-react-app) without ejecting `react-scripts`.
- [bahmutov/next-and-cypress-example](https://github.com/bahmutov/next-and-cypress-example) shows how to get backend and fronend coverage for a [Next.js](https://nextjs.org) project. Uses [middleware/nextjs.js](middleware/nextjs.js).

## Debugging

Expand Down
28 changes: 28 additions & 0 deletions middleware/nextjs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Middleware for returning server-side code coverage
* for Next.js API route. To use, create new `pages/api/coverage.js` file
* and re-export this default middleware function.
*
```
// in your pages/api/coverage.js
module.exports = require('@cypress/code-coverage/middleware/nextjs')
// then add to your cypress.json an environment variable pointing at the API
{
"baseUrl": "http://localhost:3000",
"env": {
"codeCoverage": {
"url": "/api/__coverage__"
}
}
}
```
*
* @see https://nextjs.org/docs#api-routes
* @see https://github.com/cypress-io/code-coverage
*/
module.exports = function returnCodeCoverageNext (req, res) {
// only GET is supported
res.status(200).json({
coverage: global.__coverage__ || null
})
}

0 comments on commit 7e79816

Please sign in to comment.