istanbul-js
Menu
Tutorials

TypeScript coverage: preserve maps and choose one build path

Make the coverage report point to the TypeScript you edit.

Transformed source is connected to coverage results by a source mapping.
Preserve TypeScript source maps and choose one build path so results point to original source.

Are tests loading source through a hook or executing emitted JavaScript? Can the report recover original locations? This recipe uses a CommonJS ts-node hook. It does not configure native TypeScript ESM loading or a browser bundler.

Start with the TypeScript preset

The nyc README recommends @istanbuljs/nyc-config-typescript. Its documentation requires maps and describes ts-node and source-map-support. Install these alongside a compatible Mocha and TypeScript installation:

npm install --save-dev nyc @istanbuljs/nyc-config-typescript ts-node source-map-support typescript mocha

.nycrc.json

{
  "extends": "@istanbuljs/nyc-config-typescript",
  "all": true,
  "include": ["src/**/*.ts"],
  "require": ["ts-node/register", "source-map-support/register"],
  "reporter": ["text", "html"]
}

tsconfig.json (compilerOptions fragment)

{
  "compilerOptions": {
    "module": "CommonJS",
    "sourceMap": true
  }
}

package.json (script fragment)

{
  "scripts": {
    "typecheck": "tsc --noEmit",
    "coverage": "nyc mocha --extension ts \"test/**/*.test.ts\""
  }
}

Merge compiler options rather than discarding project settings. The package must be compatible with CommonJS loading, and tests need their usual type declarations. Type checking and coverage are separate commands.

Preserve useful preset defaults

The preset supplies extension, cache and exclusion settings. Replacing an exclusion array can remove defaults you meant to retain, including treatment of declarations. Start with the preset, then change scope when you can explain the missing or unwanted files.

Loading the transform through nyc also lets it inspect unloaded source when all is enabled. A hook installed only inside the runner may not cover that stage.

Check one original conditional

Open a source file in the HTML report and compare a known decision with its TypeScript location. If the report shows emitted JavaScript, check map generation and preservation.

For a precompiled build, retain maps and original files from the same revision. The nyc README documents exclude-after-remap: false for pre-instrumented codebases whose mapped files would otherwise be excluded. Apply that setting to the relevant pipeline, not automatically to every TypeScript project.

Find the failing stage

SymptomCheck
Unknown .ts extensionLoader configuration and CommonJS assumptions.
Only emitted code appearsMaps and original source availability.
Declarations enter the reportPreset exclusions and overrides.
Untouched source is absentall, include rules and nyc require hooks.

For a Babel transform, use one instrumentation owner. Browser builds need an explicit collection bridge.

Primary sources

Check official documentation against the tools installed in your project. These explanations and examples are independent.