Complete command-line interface for code coverage including instrumentation, report generation, and threshold checking. Supports all major Istanbul reporters and advanced features like source map handling.
Instrument and collect coverage for any Node.js script or command.
# Run a script with coverage
c8 [options] <command> [command-options]
# Examples
c8 node script.js
c8 npm test
c8 mocha test/*.js
c8 --reporter=html --reporter=text npm testUsage Examples:
# Basic usage - runs script.js and outputs text coverage report
c8 node script.js
# Multiple reporters
c8 --reporter=html --reporter=json npm test
# Custom output directory
c8 --reports-dir=./my-coverage npm test
# Include all source files (not just tested ones)
c8 --all npm testGenerate coverage reports from existing V8 coverage data.
# Generate reports from existing coverage data
c8 report [options]Usage Examples:
# Generate default text report
c8 report
# Generate HTML report
c8 report --reporter=html
# Custom output directory
c8 report --reports-dir=./coverage --reporter=htmlCheck coverage against specified thresholds and fail if coverage is insufficient.
# Check coverage thresholds
c8 check-coverage [options]
# Combine with test execution
c8 --check-coverage [options] <command>Usage Examples:
# Check that lines, functions, and branches are at least 95%
c8 check-coverage --lines 95 --functions 95 --branches 95
# Run tests and check coverage in one command
c8 --check-coverage --lines 90 npm test
# Check coverage per file
c8 check-coverage --per-file --lines 80
# Shortcut for 100% coverage
c8 --100 npm testControl coverage report format and output location.
# Reporter selection
--reporter, -r <reporter> # Reporter type (text, html, json, etc.)
--reports-dir, -o <directory> # Output directory (default: ./coverage)
--skip-full # Hide files with 100% coverageControl which files are included in coverage analysis.
# File filtering
--include, -n <pattern> # Include files matching pattern
--exclude, -x <pattern> # Exclude files matching pattern
--extension, -e <extension> # File extensions to cover
--exclude-after-remap, -a # Apply exclusions after source maps
--exclude-node-modules # Exclude node_modules (default: true)
--all # Include all source files
--src <directory> # Source directories to analyzeUsage Examples:
# Include only specific files
c8 --include='src/**/*.js' npm test
# Exclude test files
c8 --exclude='**/*.test.js' --exclude='**/*.spec.js' npm test
# Multiple source directories
c8 --all --src=src --src=lib npm test
# Include TypeScript files
c8 --extension=.ts --extension=.js npm testSet minimum coverage requirements.
# Coverage thresholds
--check-coverage # Enable threshold checking
--branches <number> # Branch coverage threshold (default: 0)
--functions <number> # Function coverage threshold (default: 0)
--lines <number> # Line coverage threshold (default: 90)
--statements <number> # Statement coverage threshold (default: 0)
--per-file # Check thresholds per file
--100 # Shortcut for 100% coverageAdvanced configuration for coverage collection and processing.
# Technical configuration
--temp-directory <directory> # V8 coverage data directory
--clean # Clean temp files before execution (default: true)
--resolve <directory> # Base directory for path resolution
--wrapper-length <number> # Wrapper prefix byte length
--omit-relative # Omit non-absolute paths (default: true)
--allow-external # Allow files outside cwd
--merge-async # Merge coverage reports asynchronously
--experimental-monocart # Use Monocart coverage reportsc8 supports configuration via files and package.json.
# Configuration file
--config, -c <file> # Path to JSON configuration fileSupported configuration files:
.c8rc.c8rc.json.nycrc.nycrc.jsonpackage.json (in c8 section)Example .c8rc.json:
{
"reporter": ["text", "html"],
"reports-dir": "./coverage",
"all": true,
"include": ["src/**/*.js"],
"exclude": ["**/*.test.js"],
"check-coverage": true,
"lines": 90,
"functions": 90,
"branches": 90
}Example package.json section:
{
"c8": {
"reporter": ["text", "html"],
"all": true,
"include": ["src/**/*.js"],
"exclude": ["**/*.test.js"]
}
}NODE_V8_COVERAGE # Default temp directory for coverage data
EXPERIMENTAL_MONOCART # Enable monocart coverage reportsc8 supports all Istanbul reporters:
text - Console text output (default)html - HTML coverage reportjson - JSON coverage datalcov - LCOV formattext-summary - Brief text summarycobertura - Cobertura XML formatteamcity - TeamCity service messagesclover - Clover XML formatCoverage threshold failures exit with code 1:
ERROR: Coverage for lines (85%) does not meet global threshold (90%)
ERROR: Coverage for functions (70%) does not meet threshold (80%) for src/utils.jsMissing dependencies or configuration errors are reported with descriptive messages and appropriate exit codes.