diff-cover
v2.0.5
Published
A Tool to Show Code Coverage for Modified Lines in a Diff
Maintainers
Readme
diff-cover-ts
A high-performance TypeScript port of the diff_cover tool.
Automatically find lines of code in your git diff that are missing test coverage or quality checks.
📖 Table of Contents
🚀 Introduction
diff_cover_ts helps you maintain high code quality by focusing on what matters most: the code you just changed. Instead of failing builds because of low legacy coverage, this tool ensures that every new commit meets your quality standards.
It works by comparing your current branch with a base branch (e.g., origin/main) and reporting coverage/quality gaps only for the modified lines.
✨ Features
- 🎯 Precision Coverage: Report code coverage only for modified lines in your git diff.
- 🛡️ Quality Gate: Enforce linting checks only on changed code.
- 🤖 Auto-Configuration: Seamlessly detects
vite.config.ts/jsorvitest.config.ts/jsto find coverage reports. - 📊 Multi-Format Support: Compatible with
lcov,cobertura,clover,jacoco, and generic XML reports. - ⚡ Git Integration: Built-in git history analysis to identify modified lines accurately.
- 🚫 Fail-Under Checks: Set thresholds to fail CI/CD pipelines if coverage/quality is too low.
📦 Installation
# Using npm (Global)
npm install -g diff-cover
# Using Bun (Global)
bun add -g diff-cover
# Using npm (Dev Dependency)
npm install diff-cover --save-dev🛠 Usage
Diff Cover
Automatically identify lines in your diff that lack test coverage.
⚡ Auto Configuration (Recommended)
If you are using Vite or Vitest, simply run:
diff-coverThe tool will intelligently parse your configuration files to locate the coverage report and determine the format.
📝 Manual Usage
You can explicitly specify the coverage report files:
diff-cover coverage/lcov.info
# OR
diff-cover coverage/cobertura.xml⚙️ Options
| Option | Description | Default |
| :-------------------------- | :----------------------------------------------------------- | :------------ |
| --compare-branch <branch> | Branch to compare against | origin/main |
| --fail-under <score> | Returns a non-zero exit code if coverage is below this value | 0 |
| --show-uncovered | Print uncovered lines to the console | false |
| --expand-coverage-report | Append missing lines based on previous line hits | false |
| --ignore-staged | Ignore staged changes in the diff | false |
| --ignore-unstaged | Ignore unstaged changes in the diff | false |
| --include-untracked | Include untracked files in the analysis | false |
| --exclude <patterns...> | Exclude files matching glob patterns | [] |
| --include <patterns...> | Include files matching glob patterns | [] |
| --html-report <file> | Generate an HTML report at the specified path | null |
| --json-report <file> | Generate a JSON report at the specified path | null |
Diff Quality
Run static analysis tools and report violations only on modified lines.
diff-quality report.txt --violations <driver>🔌 Supported Drivers
eslintpylintflake8shellcheckcppcheckcheckstylefindbugs
⚙️ Options
| Option | Description | Default |
| :-------------------------- | :----------------------------------------------------------- | :------------ |
| --compare-branch <branch> | Branch to compare against | origin/main |
| --fail-under <score> | Returns a non-zero exit code if quality score is below value | 0 |
| --include-untracked | Include untracked files | false |
| --exclude <patterns...> | Exclude files matching glob patterns | [] |
| --html-report <file> | Generate an HTML report at the specified path | null |
💻 Development
Setup
# Install dependencies
bun installTesting
# Run test suite
bun test
# Run tests with coverage
bun test --coverageCode Quality
We use oxlint for linting and oxfmt for formatting. Pre-commit hooks via husky ensure code quality.
# Lint code
bun run lint
# Format code
bun run format📄 License
This project is licensed under the MIT License.
