Support for code coverage instrumentation in the app code
See original GitHub issueJust like you can say npm test -- --coverage
I think it would be useful to be able to say npm start -- --coverage
or npm run build -- --coverage
to instrument the app code for code coverage. You could then get code coverage information from end-to-end tests (which IMO are the most useful kind of tests). Here’a simple step-by-step demo to show what I mean:
create-react-app coverage-demo
cd coverage-demo
npm install --save-dev chromedriver istanbul-lib-coverage selenium-webdriver
- To src/App.test.js, add
import { Builder } from "selenium-webdriver";
import libCoverage from "istanbul-lib-coverage";
it("report code coverage after end-to-end testing", async () => {
// Go to site
const builder = new Builder();
const driver = builder.forBrowser("chrome").build();
await driver.get("localhost:3000");
// A real test would do more here of course
// Collect coverage data and report it
const __coverage__ = await driver.executeScript("return __coverage__;");
const map = libCoverage.createCoverageMap(__coverage__);
let result = "";
map.files().forEach(function(f) {
var fc = map.fileCoverageFor(f);
result += `${fc.computeSimpleTotals("s").pct + "%"} for ${f}\n`
});
console.log(result);
await driver.quit();
});
- To
const plugins
in node_modules/babel-preset-react-app/index.js, add the following item last to the array
// TODO: Enable only when '--coverage' is passed to 'npm start/build'
require.resolve('babel-plugin-istanbul'),
npm start
npm test # in another terminal
Expected output:
100% for /home/martinno/src/coverage-demo/src/App.js
100% for /home/martinno/src/coverage-demo/src/index.js
6.9% for /home/martinno/src/coverage-demo/src/registerServiceWorker.js
The coverage data can of course be processed in more useful ways. In my own app I write the json data to files and then use istanbul report --root coverage-data html
to get a nice HTML presentation of coverage data.
Would you be open to a contribution that did step 5 properly?
Issue Analytics
- State:
- Created 6 years ago
- Reactions:6
- Comments:5 (1 by maintainers)
Top Results From Across the Web
Code Coverage - Cypress Documentation
Code coverage requires inserting additional counters into your source code before running it. This step is called instrumentation. Instrumentation takes code ...
Read more >Top 15 Code Coverage Tools (For Java, JavaScript, C++, C# ...
List of the most popular code coverage tools for Java, JavaScript, C, C++, C#, PHP, Eclipse, .Net and many other programming languages:.
Read more >Code Coverage vs Test Coverage: A Detailed Guide
Code coverage verifies with instrumentation. Instrumentation monitors performance inserts trace information, and detects errors in the source ...
Read more >9 Best Code Coverage Tools for Java, Python, C, C++, C#, .NET
3) JaCoCo · JaCoCo offers instructions, line and branch coverage · It is one of the best java code coverage tools which supports...
Read more >The Best Code Coverage Tools By Programming Language
The overall value of code coverage in your app will be expressed as a percentage. Best practice is to establish what percentage of...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
(I would love this)
I’ve asked a question relating to this on stack-overflow.
See: https://stackoverflow.com/questions/53485370/code-coverage-on-react-with-puppeteer-istanbul