GithubHelp home page GithubHelp logo

bahmutov / next-and-cypress-example Goto Github PK

View Code? Open in Web Editor NEW
138.0 2.0 25.0 1.53 MB

Next.js example instrumented for code coverage from Cypress tests

JavaScript 100.00%
cypress-io cypress-example nextjs nextjs-example code-coverage cypress-code-coverage-example

next-and-cypress-example's Introduction

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 <[email protected]> © 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 <[email protected]>

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.

next-and-cypress-example's People

Contributors

bahmutov avatar mozart409 avatar renovate-bot avatar renovate[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

next-and-cypress-example's Issues

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

circleci
.circleci/config.yml
npm
package.json
  • babel-plugin-istanbul 5.2.0
  • isomorphic-unfetch 3.1.0
  • next 9.5.5
  • react 16.14.0
  • react-dom 16.14.0
  • @cypress/code-coverage 3.12.35
  • check-code-coverage 1.10.5
  • cypress 7.7.0
  • start-server-and-test 1.15.4

  • Check this box to trigger a request for Renovate to run again on this repository

Component testing Next apps doesn't serve /public static assets

Hey there Gleb,

Thanks for all of your work!

Thought maybe you could help: it seems when running cypress open-ct, static files are not served even when setting "fileServerFolder": "public" in cypress.json. (/public being the default static asset path for a Next.JS project)

Does the fileServerFolder configuration not work for the Cypress component test runner?

Thanks!

Some issues based on today's state of Cypress code coverage

Hey there @bahmutov and team,

I've been writing up an internal recipe on how to install Cypress for a Next.js app, along with code coverage, CircleCI and all goodies to have a fully working setup. Here's a few things I noted while following both this repo and some of the other examples (mainly https://github.com/lluia/cypress-typescript-coverage-example):

  1. Code coverage instrumentation is served to users, compared to https://github.com/lluia/cypress-typescript-coverage-example/blob/ae6c8a3fc0143f15943ee50e248accb0e011c467/babelrc.js#L4 which is prevent it from being bundled in

  2. Cypress 3.8.3 is used which makes the cypress/plugins/index.js portion obsolete when using Cypress 4 and up (using 4.4.0 here at the moment):

    module.exports = (on, config) => {
      require('@cypress/code-coverage/task')(on, config);
      return config;
    };

If you agree the repo should be updated to use the latest Cypress version I can open a PR and update it accordingly.

Keep up the good work on Cypress, loving the latest version!

Mocking `getInitialProps` Responses

Is it possible to mock / stub requests sent from getInitialProps or getServerSideProps? I've tried using cy.route and cy.route2 but I don't think it works since it's expecting to mock through the network layer in the browser.

Not sure if there's a way to do this or if there's a recommended practice? If there is one, I'd be glad to add it as an example in this repo.

Thanks!

How to instrument and measure unvisited pages?

I'm working on a Next.js app where we've just started adding tests with Cypress. Because there's a lot of pre-existing code, I want to use the coverage reports as a progress report to see how we're progressing... but I'm getting a very limited view from the lcov-report.

Pages in the Next.js app that aren't visited in tests do not appear in the report, so I'm getting inflated coverage numbers. Instead of "what percentage of the total statements in the app are covered?", the report gives me "what percentage of the total statements in pages that the tests visited are covered?"

I took a look at https://github.com/kylemh/next-ts-with-cypress-coverage, which led me to adding the all option to the nyc config. Now pages that haven't been touched show up in the lcov-report... but with all zeros.

I've reproduced at https://github.com/cherewaty/next-and-cypress-example/tree/uncovered-page

Screen Shot 2020-11-08 at 3 24 54 PM

uncovered.js should instead show 0/1 statements covered

My hypothesis is that Next.js's dev server isn't even attempting to build pages until they're visited, so babel-plugin-istanbul isn't instrumenting them.

Some paths to explore I've been thinking about:

  • Am I missing some configuration parameter that would make this easy?
  • Maybe I could hack up the next/babel preset a little bit when NODE_ENV==="test" so all sources get built and instrumented?
  • Do I need to run cypress against a "production" artifact from next build instead of the dev server?
  • Could I set up a spot where I import everything just for cypress runs?

Happy to make a PR to this example repo if I get this figured out!

Code instrumentation with c8

All examples for coverage I could find use babel for code instrumentation. Now with nextjs 12 moving away from babel what would be a good approach to get code coverage working?

Action Required: Fix Renovate Configuration

There is an error with this repository's Renovate configuration that needs to be fixed. As a precaution, Renovate will stop PRs until it is resolved.

Error type: undefined. Note: this is a nested preset so please contact the preset author if you are unable to fix it yourself.

Can't visit localhost:3000

Hey, I'm trying to run Cypress on my Next.js app, and I'm having trouble. localhost:3000 works in my browser. Cypress can visit any other site. But when I try to visit localhost it doesn't work. I tried running your app and I have the same problem. Do you have any tips?

TypeScript equivalent help

I've got everything in this example handled, plus I have:

.nycrc:

{
  "extension": [
    ".js",
    ".jsx",
    ".ts",
    ".tsx"
  ],
  "extends": "@istanbuljs/nyc-config-typescript",
  "include": [
    "pages/**/*",
    "components/**/*",
    "hooks/**/*",
    "utils/**/*"
  ],
  "exclude": [
    "pages/api/__coverage__.js"
  ],
  "all": true,
  "sourceMap": true,
  "instrument": true
}

Plus in my package.json:

"nyc": {
  "extends": "./.nycrc",
  "report-dir": "cypress-coverage",
  "reporter": [
    "json",
    "lcov"
  ]
},

Plus the edit needed to instrument TS inside cypress/plugins/index.js:

/**
 * @type {Cypress.PluginConfig}
 */
module.exports = (on: Cypress.PluginEvents, config: Cypress.PluginConfigOptions) => {
  // `on` is used to hook into various events Cypress emits
  // `config` is the resolved Cypress config

  const browserify = require('@cypress/browserify-preprocessor');
  const options = browserify.defaultOptions;
  options.browserifyOptions.transform[1][1].babelrc = true;
  options.typescript = require.resolve('typescript');

  require('@cypress/code-coverage/task')(on, config);
  on('file:preprocessor', browserify(options));

  return config;
};

The test runner runs, but I'm not seeing any coverage reported unless I do cypress open. Does it require --record flag if I use cypress run?

More importantly, the coverage report is reporting zeros across the board, but we have plenty of tests and should see some coverage. I see the following error:

⚠️ file /Users/kyle/Code/@air/next/.nyc_output/out.json has no coverage information
Did you forget to instrument your web application?
Read https://github.com/cypress-io/code-coverage#instrument-your-application```

Use Next@latest

i am unable to get this working with latest Next. Would be great to see this updated

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.