Performance: Code coverage basics
Code coverage tools impact the testing phase by adding runtime instrumentation, which can slow down test execution and increase memory usage.
Configure nyc to include only source files and exclude node_modules: nyc --include 'src/**/*.js' mocha
Run tests with coverage on all files including large dependencies without filtering: nyc mocha| Pattern | Instrumentation Scope | Test Runtime Overhead | Memory Usage | Verdict |
|---|---|---|---|---|
| Instrument all files including dependencies | All project files + node_modules | High (30-50% slower) | High | [X] Bad |
| Instrument only source files | Source files only | Moderate (10-20% slower) | Moderate | [OK] Good |
nyc mocha runs mocha tests with coverage collection.add?
function add(a, b) {
if (a > 0) {
return a + b;
} else {
return 0;
}
}
// Test
add(2, 3);nyc mocha but the coverage report shows 0% coverage. What is the most likely cause?function checkValue(x) {
if (x) {
return 'Yes';
} else {
return 'No';
}
}
Your tests only call checkValue(true). What change will increase coverage?Pass the test and this pattern moves unknown