Skip to content

Commit 7e79816

Browse files
committed
feat: add Nextjs example and middleware, close #63
1 parent 7f50613 commit 7e79816

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ For example, if you want to only include files in the `app` folder, but exclude
236236
- [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.
237237
- [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).
238238
- [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`.
239+
- [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).
239240

240241
## Debugging
241242

Diff for: middleware/nextjs.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Middleware for returning server-side code coverage
3+
* for Next.js API route. To use, create new `pages/api/coverage.js` file
4+
* and re-export this default middleware function.
5+
*
6+
```
7+
// in your pages/api/coverage.js
8+
module.exports = require('@cypress/code-coverage/middleware/nextjs')
9+
// then add to your cypress.json an environment variable pointing at the API
10+
{
11+
"baseUrl": "http://localhost:3000",
12+
"env": {
13+
"codeCoverage": {
14+
"url": "/api/__coverage__"
15+
}
16+
}
17+
}
18+
```
19+
*
20+
* @see https://nextjs.org/docs#api-routes
21+
* @see https://github.com/cypress-io/code-coverage
22+
*/
23+
module.exports = function returnCodeCoverageNext (req, res) {
24+
// only GET is supported
25+
res.status(200).json({
26+
coverage: global.__coverage__ || null
27+
})
28+
}

0 commit comments

Comments
 (0)