Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to instrument and measure unvisited pages? #115

Open
cherewaty opened this issue Nov 8, 2020 · 9 comments
Open

How to instrument and measure unvisited pages? #115

cherewaty opened this issue Nov 8, 2020 · 9 comments

Comments

@cherewaty
Copy link

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!

@AleksaC
Copy link

AleksaC commented Dec 19, 2020

I'm facing the same issue. I think you are right about next not building pages until they are visited, because it shows 100% coverage for 404 page even though I don't touch it in the tests. According to docs that page is statically generated at build time, hence the coverage.

@bahmutov
Copy link
Owner

bahmutov commented Dec 19, 2020 via email

@AleksaC
Copy link

AleksaC commented Dec 19, 2020

Sure, that's the goal. It would just be nice to be reminded that they aren't by failing coverage-check.

@bahmutov
Copy link
Owner

bahmutov commented Dec 20, 2020 via email

@AleksaC
Copy link

AleksaC commented Dec 20, 2020

Here's how I use it:

{
  "all": true,
  "include": [
    "pages/**/*.js",
    "components/**/*.js",
    "constants/**/*.js",
    "utils/**/*.js"
  ],
  "exclude": ["pages/api/__coverage__.js"]
}

Is this what you meant?

@bahmutov
Copy link
Owner

bahmutov commented Dec 20, 2020 via email

@AleksaC
Copy link

AleksaC commented Dec 20, 2020

When I run the tests and get coverage report all works well for the pages that are covered by the tests and for the ones that are not I get a result like the one in the image above in the original issue. Also I have a custm 404 page which is also not touched by the tests but the coverage is reported at 100% for it like I mentioned in my first comment.

I can provide a repo reproducing the issue if you want to take a look.

@bahmutov
Copy link
Owner

bahmutov commented Dec 20, 2020 via email

@AleksaC
Copy link

AleksaC commented Dec 20, 2020

Sure, I was thinking of doing that earlier but I saw this issue so I decided to check here first.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants