bahmutov / next-and-cypress-example Goto Github PK
View Code? Open in Web Editor NEWNext.js example instrumented for code coverage from Cypress tests
Next.js example instrumented for code coverage from Cypress tests
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?
This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.
These updates have all been created already. Click a checkbox below to force a retry/rebase of any.
react
, react-dom
).circleci/config.yml
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
i am unable to get this working with latest Next. Would be great to see this updated
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?
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!
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```
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!
I am not able to find any example of instrumentation of app created by npx create-next-app@latest
. I added Cypress by following the nextjs doc. But, I am not able to follow the cypress doc for CC. You can check the project here.
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):
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
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!
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.
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
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:
next/babel
preset a little bit when NODE_ENV==="test"
so all sources get built and instrumented?cypress
against a "production" artifact from next build
instead of the dev
server?cypress
runs?Happy to make a PR to this example repo if I get this figured out!
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.