Skip to content
Next.js example instrumented for code coverage from Cypress tests
JavaScript
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.circleci
components
cypress
images
pages
.babelrc
.gitignore
README.md
cypress.json
package-lock.json chore(deps): update dependency cypress to v3.7.0 Nov 30, 2019
package.json
renovate.json add circle and renovate Aug 17, 2019

README.md

next.js and Cypress example renovate-app badge CircleCI

Next.js example instrumented for code coverage from Cypress tests

Install and run

npm install
npm run e2e

This starts the application at localhost:3000 and opens Cypress test runner.

npm run e2e

Click on the spec file to run the end-to-end tests.

all tests

Coverage

The tests are instrumented following the instructions in Cypress code coverage guide. In particular, the front-end is instrumented using .babelrc file like

{
  "presets": ["next/babel"],
  "plugins": ["istanbul"]
}

When 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.

$ open coverage/lcov-report/index.html

Code coverage report

To fetch the server-side code coverage, the @cypress/code-coverage plugin needs an endpoint. This endpoint is implemented in pages/api/coverage.js file following the Next.js API convention. This endpoint just returns the existing global coverage object or null

export default (req, res) => {
  res.status(200).json({
    coverage: global.__coverage__ || null
  })
}

Cypress plugin requests the right endpoint using the environment variable from cypress.json file

{
  "baseUrl": "http://localhost:3000",
  "env": {
    "codeCoverage": {
      "url": "/api/__coverage__"
    }
  }
}

On CI after the tests finish, we store the coverage reports. We also run a script to check if the coverage dropped below 100%

$ npx nyc report --reporter=text-summary --check-coverage --statements 100
================== Coverage summary =========
Statements   : 100% ( 22/22 )
Branches     : 100% ( 0/0 )
Functions    : 100% ( 9/9 )
Lines        : 100% ( 22/22 )
=============================================

See more

Small print

Author: Gleb Bahmutov <gleb.bahmutov@gmail.com> © 2019

License: MIT - do anything with the code, but don't blame me if it does not work.

Support: if you find any problems with this module, email / tweet / open issue on Github

MIT License

Copyright (c) 2019 Gleb Bahmutov <gleb.bahmutov@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

You can’t perform that action at this time.