{"id":15552260,"url":"https://github.com/bahmutov/next-and-cypress-example","last_synced_at":"2025-09-28T22:30:24.152Z","repository":{"id":35724195,"uuid":"202934069","full_name":"bahmutov/next-and-cypress-example","owner":"bahmutov","description":"Next.js example instrumented for code coverage from Cypress tests","archived":false,"fork":false,"pushed_at":"2024-09-28T06:37:52.000Z","size":1703,"stargazers_count":140,"open_issues_count":18,"forks_count":25,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-10-03T14:14:05.997Z","etag":null,"topics":["code-coverage","cypress-code-coverage-example","cypress-example","cypress-io","nextjs","nextjs-example"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bahmutov.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-08-17T21:47:20.000Z","updated_at":"2024-09-28T06:35:48.000Z","dependencies_parsed_at":"2023-01-16T04:05:48.692Z","dependency_job_id":"8b51ceba-aaae-4eaf-bb99-2948b1ef1d2d","html_url":"https://github.com/bahmutov/next-and-cypress-example","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bahmutov%2Fnext-and-cypress-example","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bahmutov%2Fnext-and-cypress-example/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bahmutov%2Fnext-and-cypress-example/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bahmutov%2Fnext-and-cypress-example/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bahmutov","download_url":"https://codeload.github.com/bahmutov/next-and-cypress-example/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":234565092,"owners_count":18853249,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["code-coverage","cypress-code-coverage-example","cypress-example","cypress-io","nextjs","nextjs-example"],"created_at":"2024-10-02T14:14:04.100Z","updated_at":"2025-09-28T22:30:23.729Z","avatar_url":"https://github.com/bahmutov.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# next.js and Cypress example [![renovate-app badge][renovate-badge]][renovate-app] [![CircleCI](https://circleci.com/gh/bahmutov/next-and-cypress-example.svg?style=svg)](https://circleci.com/gh/bahmutov/next-and-cypress-example)\n\u003e [Next.js](https://nextjs.org/) example instrumented for code coverage from [Cypress tests](https://www.cypress.io/)\n\n## Install and run\n\n```sh\nnpm install\nnpm run e2e\n```\n\nThis starts the application at `localhost:3000` and opens Cypress test runner.\n\n![npm run e2e](images/e2e.png)\n\nClick on the spec file to run the end-to-end tests.\n\n![all tests](images/tests.png)\n\n## Coverage\n\nThe tests are instrumented following the instructions in [Cypress code coverage guide](https://on.cypress.io/code-coverage). In particular, the front-end is instrumented using [.babelrc](.babelrc) file like\n\n```json\n{\n  \"presets\": [\"next/babel\"],\n  \"plugins\": [\"istanbul\"]\n}\n```\n\nWhen you run tests, the code coverage report is saved in `coverage` folder. There are reports in several formats, but open the HTML one for human-readable report.\n\n```shell\n$ open coverage/lcov-report/index.html\n```\n\n![Code coverage report](images/report.png)\n\nTo fetch the server-side code coverage, the `@cypress/code-coverage` plugin needs an endpoint. This endpoint is implemented in [pages/api/__coverage__.js](pages/api/__coverage__.js) file following the [Next.js API convention](https://nextjs.org/docs#api-routes). This endpoint just returns the existing global coverage object or `null`\n\n```js\nexport default (req, res) =\u003e {\n  res.status(200).json({\n    coverage: global.__coverage__ || null\n  })\n}\n```\n\nCypress plugin requests the right endpoint using the environment variable from [cypress.json](cypress.json) file\n\n```json\n{\n  \"baseUrl\": \"http://localhost:3000\",\n  \"env\": {\n    \"codeCoverage\": {\n      \"url\": \"/api/__coverage__\"\n    }\n  }\n}\n```\n\nOn CI after the tests finish, we store the coverage reports. We also run a script to check if the coverage dropped below 100%\n\n```shell\n$ npx nyc report --reporter=text-summary --check-coverage --statements 100\n================== Coverage summary =========\nStatements   : 100% ( 22/22 )\nBranches     : 100% ( 0/0 )\nFunctions    : 100% ( 9/9 )\nLines        : 100% ( 22/22 )\n=============================================\n```\n\n## See more\n\n- [Cypress code coverage guide](https://on.cypress.io/code-coverage)\n\n### Small print\n\nAuthor: Gleb Bahmutov \u0026lt;gleb.bahmutov@gmail.com\u0026gt; \u0026copy; 2019\n\n* [@bahmutov](https://twitter.com/bahmutov)\n* [glebbahmutov.com](https://glebbahmutov.com)\n* [blog](https://glebbahmutov.com/blog)\n\nLicense: MIT - do anything with the code, but don't blame me if it does not work.\n\nSupport: if you find any problems with this module, email / tweet /\n[open issue](https://github.com/bahmutov/next-and-cypress-example/issues) on Github\n\n## MIT License\n\nCopyright (c) 2019 Gleb Bahmutov \u0026lt;gleb.bahmutov@gmail.com\u0026gt;\n\nPermission is hereby granted, free of charge, to any person\nobtaining a copy of this software and associated documentation\nfiles (the \"Software\"), to deal in the Software without\nrestriction, including without limitation the rights to use,\ncopy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the\nSoftware is furnished to do so, subject to the following\nconditions:\n\nThe above copyright notice and this permission notice shall be\nincluded in all copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND,\nEXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES\nOF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\nNONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT\nHOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,\nWHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING\nFROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR\nOTHER DEALINGS IN THE SOFTWARE.\n\n[renovate-badge]: https://img.shields.io/badge/renovate-app-blue.svg\n[renovate-app]: https://renovateapp.com/\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbahmutov%2Fnext-and-cypress-example","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbahmutov%2Fnext-and-cypress-example","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbahmutov%2Fnext-and-cypress-example/lists"}