Combined Code Coverage
Code coverage is not a be all, end all metric. However, we can empirically prove that practicing TDD at all levels can at the end yield a high amount of code coverage. We can also analyze where we may be missing coverage, or have redundant source code looking at analytics.
We will acquire coverage from Cypress CT, Cypress E2e, as well as any Jest and / or RTL tests. We will then combine the coverage into a single report.
References
This appendix is based on the blog post Triple combined code coverage for React Apps with Jest, Cypress component and e2e tests, using Github Actions. We will be adapting it to TypeScript.
Setup Cypress Component & E2e coverage
Add the packages
With TS the packages are slightly different with @babel/preset-typescript
:
Instrument the app for E2e
This script is the same as the JS version
Configure nyc
for local coverage evaluation
nyc
for local coverage evaluationThis is similar to the JS version, with TS and TSX included.
Configure cypress.config.js
for code coverage, instrument the app for component testing
cypress.config.js
for code coverage, instrument the app for component testingThere are 3 key differences on the TS version of the configuration. In the beginning of the file we have to import @cypress/instrument-cra
. We need to include @babel/preset-typescript
in the module presets, and the test property has to be TS instead of JS.
This is our preferred component test config because
@babel/plugin-transform-modules-commonjs
lets us customize webpack config, so that we can make all imports accessible from any file including specs. This allows to spy/stub a wider range of code in the component tests.
Configure both cypress/support/e2e.ts
and cypress/support/component.ts
cypress/support/e2e.ts
and cypress/support/component.ts
Indifferent to a JS app.
Add coverage convenience scripts to package.json
Same convenience scripts as in the JS version
In the TS version there are no differences in local combined coverage, CodeCov service, Github Actions setup
We can replicate the same steps from the JS variant of the guide.
jest
to ignore cy.ts*
files
jest
to ignore cy.ts*
filesWe need to tell Jest not to include coverage from cy.ts*
files. This can be done in the package.json
script.
Redundant source code
In Heroes.tsx
and Villiains.tsx
we have a section
Code coverage points out that this line is not covered, although we have tests in Hero.cy.tsx
etc. explicitly checking for the spinner. This is leftover code from prior to implementing ErrorBoundary. We can safely remove it.
Coverage measurements
TDD has ensured that we build and explore the application through tests, covering everything that matters. Code coverage has not been a thought while practicing TDD, but TDD helped us gain 100% combined code coverage at the end. Here is the CodeCov report:
Addendum - configuration refactor
Check out Optimizing and simplifying Cypress config for scale. Based on the optimizations, the final app can refactored to make the config file even simpler.
We create one file for plugins, cypress/support/plugins.ts
,and have all plugins in there.
Similarly, and as an extra, we can create one file for tasks, cypress/support/tasks.ts
, and have all tasks in there.
Let's have a simple task, some Node code, to log out to the CLI when running cy.task('log', 'hello')
. This logs out to the CLI as opposed to browser console.
We can now convert the config file to TS, import plugins and tasks from the files. This way, in the future, if we have new plugins or tasks, the config file does not have to change. Also helps us if we have multiple config files for deployments (dev, stage, prod, etc.), none of them have to change in case the plugins change.
Addendum - code coverage with Vite instead of Webpack
Assume that instead of Webpack, we want to use Vite. We replicated the original Webpack version of the Tour of Heroes repo in Vite over at tour-of-heroes-react-vite-cypress-ts. We want to have Cypress e2e and component test coverage with Vite.
TL, DR; check out the PR for setting up Cypress e2e + CT code coverage with Vite.
Add the packages
For Vite code coverage with Vite, we need a lot less packages:
Instrument the app (helps both E2e & CT)
We need to modify our vite.config.ts
slightly.
Configure nyc
for local coverage evaluation
nyc
for local coverage evaluationThis is exactly the same as the Webpack version of things, we will not repeat the text here.
Configure cypress.config.js
for code coverage, instrument the app for component testing
cypress.config.js
for code coverage, instrument the app for component testingNo additional settings here; we do not need @cypress/instrument-cra
.
Configure both cypress/support/e2e.ts
and cypress/support/component.ts
cypress/support/e2e.ts
and cypress/support/component.ts
This is exactly the same as the Webpack version of things, we will not repeat the text here.
Add coverage convenience scripts to package.json
Again, no distinctions here vs the Webpack version of things.
Last updated