-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Document configuring code coverage for component tests (webpack, vite) #5519
Comments
Example setup for code coverage with Create-React-App, in cypress/plugins/index.js:
|
Hello @jhiester I believe there could be two subjects to cover here: How to get component testing code coverage
How to merge it with e2e testing coverageOn that front there is good news: If this does not answer your question, feel free to respond below. Thank you in advance. |
Hey @yann-combarnous, I have similar conf and it doesn't collect coverage for CT, for E2E it does it but after running |
Hello @Dorious What @yann-combarnous is doing above should be extracting the coverage in Component testing. But this line injectDevServer(on, config); Will do behind the scenes the same as these lines. on('dev-server:start', (options) => {
return startDevServer({ options, webpackConfig });
}); They will be redundant. The first one will inject the server without setting up any coverage. We probably should check that in the processing and send a warning. Did you see that? |
@elevatebart I don't have injectDevServer because I don't use react-scripts. My cypress /// <reference types="cypress" />
import browserify from '@cypress/browserify-preprocessor';
import task from '@cypress/code-coverage/task';
import { initPlugin } from 'cypress-plugin-snapshots/plugin';
import { startDevServer } from '@cypress/webpack-dev-server';
import webpackConfig from '../../webpack.cypress';
/**
* @type {Cypress.PluginConfig}
*/
const configurePlugins: Cypress.PluginConfig = (on, config) => {
task(on, config);
initPlugin(on, config);
on(
'file:preprocessor',
browserify({
typescript: require.resolve('typescript'),
})
);
if (config.testingType === 'component') {
// console.log(webpackConfig.module.rules[4].use[0].options);
on('dev-server:start', (options) => startDevServer({ options, webpackConfig }));
}
return config;
};
export default configurePlugins; The console log is there to check is |
@elevatebart ok I figured out what was wrong in my case, in |
Lovely !!! Code coverage implementation might be a good "recipe" to add to our docs. To get coverage, you need:
const installCoverageTask = require("@cypress/code-coverage/task");
module.exports = (on, config) => {
installCoverageTask(on, config)
}
import '@cypress/code-coverage/support' |
@elevatebart I have followed all the steps mentioned in the repo. But the instrumentation is not happening at all in my case. we use the same react scripts. |
@vrknetha What is your stack? Webpack? vite? vue? react? |
@vrknetha just re-read your answer... Can you share your setup?
A repository would be even better, but I understand not everyone can take this time. |
Same thing here, CT code coverage is not working. CRA + Typescript. What finally made it work I added
just before my typescript loader, in webpack config for
This is what I have:
And it works for me like this... |
Yeah, adding code coverage is absolutely on my list! |
Thanks @elevatebart - this repo helped me a bunch: https://github.com/elevatebart/cy-ct-cra I'm not currently using integration testing, and was struggling to find resources that demo'd code coverage for a CRA app using only component testing. In case anyone runs into the same issue, the saving grace for me was in My final /// <reference types="cypress" />
require("@cypress/instrument-cra");
const injectCraDevServer = require("@cypress/react/plugins/react-scripts");
const installCoverageTask = require("@cypress/code-coverage/task");
/**
* @type {Cypress.PluginConfig}
*/
module.exports = (on, config) => {
// `on` is used to hook into various events Cypress emits
// `config` is the resolved Cypress config
installCoverageTask(on, config);
if (config.testingType === "component") {
injectCraDevServer(on, config);
}
return config;
}; My
|
One thing I have not figured out is that I want to have nyc report on all files, which is why I used in package.json: But it seems that for component testing, not all files are included in coverage percentage. Any idea how to have component testing honor the nyc setup for scope @elevatebart ? |
@elevatebart you mentioned code coverage reports should be merged automagically, but I am not finding this true (or at least not in how I have it setup currently). I am not able to run both the cypress e2e runner AND the cypress component test runner at the same time. I run one after the other and whichever one run's lasts is whose coverage report is left in the |
Hello @charleshimmer, Great question indeed. jamestalmage/istanbul-combine#2 Let me see if I can find a read documentation/tutorial using |
Update: report your coverage into separate json files then use Some help: |
@elevatebart , now trying with Vite+React to get code coverage for component testing, but getting some issues. Using vite-plugin-istanbul 2.2.2, Vite 2.7.0-beta.10, @vitejs/plugin-react 1.1.0, and Cypress 8.7.0. vite.config.js:
cypress/plugins/index.js
When running tests, tests do not run in UI. Error in Cypress UI is:
In addition, Vite is watching coverage folder, getting the following line updated for all files there:
When I just changed vite.config.js and disabled Istanbul plugin, component test just run fine. Any idea on how to get this to work with Vite + React? |
In case it helps, we've been working on getting code coverage happening over here. Hopefully we'll get some of these PRs merged in soon! https://github.com/cypress-io/cypress-component-testing-examples/pulls?q=is%3Apr+%22code+coverage%22 |
What is your folder structure? We saw the same behavior and it almost always had to do with how we're including/excluding files. |
Very helpful, thank you! Found my issue: one "process.env.NODE_ENV" left in my code. Interestingly, it only crashed with Vite code coverage plugin and Cypress. Vite build and dev server ran fine on their own. |
@yann-combarnous How did you end up solving this? I'm seeing the same error due to process.env.NODE_ENV being replaced in the vite-plugin-istanbul sourcesContent output. function cov_26q8djqde0() {
...
var coverageData = {
inputSourceMap: {
version: 3,
sourcesContent: ["export const reproduction = (item: any) => {\n if ("development" !== \"production\") {\n console.log(\"TEST\");\n }\n\n return \"\";\n};\n"],
},
};
} I also see iFaxity/vite-plugin-istanbul#8 has been raised. |
Replace process.env.NODE_ENV in your code by import.meta.env.MODE. See https://vitejs.dev/guide/env-and-mode.html#env-variables . |
Thanks for the quick reply. Sadly I don't think that will work for me since we're building with more than vite. |
If anyone is interested, we just published oob code coverage support for Cypress + TS + Vite + Quasar, check it out: https://github.com/quasarframework/quasar-testing/releases/tag/e2e-cypress-v4.1.0 This commit can be used to replicate the setup on other frameworks/tools too, as I annotate many details in the readme |
Just to provide an update. I finally got around to investigating this and raised a fix that was released in |
Has anyone got a working example of vue 3 + vite working with coverage? I have installed the vite plugin with the following config options: istanbul({
include: 'src/*',
exclude: ['node_modules'],
extension: ['.js', '.ts', '.vue'],
/**
* This allows us to omit the INSTRUMENT_BUILD env variable when running the production build via
* npm run build.
* More details below.
*/
requireEnv: false,
/**
* If forceBuildInstrument is set to true, this will add coverage instrumentation to the
* built dist files and allow the reporter to collect coverage from the (built files).
* However, when forceBuildInstrument is set to true, it will not collect coverage from
* running against the dev server: e.g. npm run dev.
*
* To allow collecting coverage from running cypress against the dev server as well as the
* preview server (built files), we use an env variable, INSTRUMENT_BUILD, to set
* forceBuildInstrument to true when running against the preview server via the
* instrument-build npm script.
*
* When you run `npm run build`, the INSTRUMENT_BUILD env variable is omitted from the npm
* script which will result in forceBuildInstrument being set to false, ensuring your
* dist/built files for production do not include coverage instrumentation code.
*/
forceBuildInstrument: Boolean(process.env.INSTRUMENT_BUILD),
}) Cypress config: component: {
devServer: {
framework: 'vue',
bundler: 'vite',
},
screenshotOnRunFailure: false,
video: false,
}, And my package.json commands looks like: "instrument-build": "cross-env INSTRUMENT_BUILD=true vite build",
"test:component": "npm run instrument-build && cypress run --component --reporter spec", I'm using cypress 10.4.0 & cypress coverage 3.10.0. Any help would be greatly appreciated |
I think we need a definitive guide on coverage. Some resources (other than the ones here):
|
Hey team! Please add your planning poker estimate with Zenhub @astone123 @marktnoonan @mike-plummer @warrensplayer @ZachJW34 |
Those are very useful, because we need framework x bundler many recipes. |
Yep, we are picking this up in our next block of work, Feb 28->Ma 14, the goal is just to document common combinations and how it works in general, so people with less common combos can configure it, too. |
I updated the CCTDD book with the instructions for the Vite version of combined code coverage : Here's the app & working combined code cov https://github.com/muratkeremozcan/tour-of-heroes-react-vite-cypress-ts. |
Hey team! Please add your planning poker estimate with Zenhub @astone123 @marktnoonan @mike-plummer @warrensplayer @jordanpowell88 |
Description
Not clear on how to do code coverage for component testing.
Why is this needed?
It has been really helpful to me and my organization to be able to use the code coverage reports from e2e/integration/unit tests to guide our testing and development. Having component level coverage tests would help in a similar way.
More Info
Acceptance Criteria
The text was updated successfully, but these errors were encountered: