Start with a Mocha test that already passes, then collect coverage with nyc.

Does an unimported module belong in the report? Does a passing test cover both return paths? This example separates those questions: all controls file scope, while your inputs control execution. It uses CommonJS source and a local Mocha and nyc installation.
Wrap the test command once
Install the development dependencies, then add a separate coverage script. Running the ordinary test command first makes a test failure easier to distinguish from a coverage configuration problem.
npm install --save-dev mocha nycpackage.json (merge these scripts)
{
"scripts": {
"test": "mocha \"test/**/*.test.cjs\"",
"coverage": "nyc npm test"
}
}.nycrc.json
{
"all": true,
"include": ["src/**/*.cjs"],
"reporter": ["text", "html"]
}Place nyc options before the command it runs. If test already invokes nyc, remove that wrapper before using this coverage script.
Give each result an assertion
src/delivery.cjs
function deliveryFee(total) {
if (total >= 50) return 0;
return 5;
}
module.exports = { deliveryFee };test/delivery.test.cjs
const assert = require('node:assert/strict');
const { deliveryFee } = require('../src/delivery.cjs');
describe('deliveryFee', () => {
it('waives the fee at the boundary', () => {
assert.equal(deliveryFee(50), 0);
});
it('charges below the boundary', () => {
assert.equal(deliveryFee(49), 5);
});
});npm test
npm run coverageRemove the second test temporarily and inspect the file in coverage/index.html. The remaining test calls the function, but cannot exercise the fee-returning path. Restore the test after inspecting the difference. These inputs illustrate the behavior; no percentage is promised for your application.
Diagnose a surprising report
| Symptom | Check |
|---|---|
| No source rows | The src/**/*.cjs glob and project root. |
| An untouched file is absent | all: true and supported extensions. |
| Tests pass but CI fails | Coverage thresholds. |
| Import syntax fails | Your module or Babel setup. |
Use the HTML view to locate an uncovered decision, then add an assertion for the expected behavior. More execution alone does not establish that a test checks the right result.
Primary sources
Check official documentation against the tools installed in your project. These explanations and examples are independent.