diff --git a/README.md b/README.md index 03ea6b71..ecf58b3d 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/middleware/nextjs.js b/middleware/nextjs.js new file mode 100644 index 00000000..df00661e --- /dev/null +++ b/middleware/nextjs.js @@ -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 + }) +}