https://github.com/jpb06/node-coverage-badges
Generating test coverage badges from a json summary file
https://github.com/jpb06/node-coverage-badges
coverage-badges effect-ts
Last synced: 6 months ago
JSON representation
Generating test coverage badges from a json summary file
- Host: GitHub
- URL: https://github.com/jpb06/node-coverage-badges
- Owner: jpb06
- License: mit
- Created: 2024-02-13T00:13:00.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2026-01-10T21:26:24.000Z (6 months ago)
- Last Synced: 2026-01-10T23:57:52.991Z (6 months ago)
- Topics: coverage-badges, effect-ts
- Language: TypeScript
- Homepage:
- Size: 719 KB
- Stars: 4
- Watchers: 1
- Forks: 0
- Open Issues: 12
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# node-coverage-badges
[](https://github.dev/jpb06/node-coverage-badges)

[](https://sonarcloud.io/summary/new_code?id=jpb06_node-coverage-badges)
[](https://sonarcloud.io/dashboard?id=jpb06_node-coverage-badges)
[](https://sonarcloud.io/dashboard?id=jpb06_node-coverage-badges)
[](https://sonarcloud.io/dashboard?id=jpb06_node-coverage-badges)
[](https://sonarcloud.io/dashboard?id=jpb06_node-coverage-badges)

[](https://sonarcloud.io/summary/new_code?id=jpb06_node-coverage-badges)
[](https://sonarcloud.io/summary/new_code?id=jpb06_node-coverage-badges)
[](https://sonarcloud.io/dashboard?id=jpb06_node-coverage-badges)
[](https://sonarcloud.io/summary/new_code?id=jpb06_node-coverage-badges)
[](https://sonarcloud.io/summary/new_code?id=jpb06_node-coverage-badges)
[](https://sonarcloud.io/dashboard?id=jpb06_node-coverage-badges)


Generating coverage badges from jest coverage report.
## ⚡ Badges for everyone
This package uses [shields.io](https://shields.io/) to created coverage badges from a coverage json summary file generated by your favorite test runner.
| Badge | Description |
| ----------------------------------------------- | ---------------------------------------------- |
|  | Percentage of DD-paths followed during tests |
|  | Percentage of functions executed within tests |
|  | Percentage of lines covered by tests |
|  | Percentage of statements executed within tests |
|  | Average of the above coverage percentages |
## ⚡ Github action
If you want to integrate this to your CI/CD, you have a [github action available for this](https://github.com/marketplace/actions/coverage-badges-generation-action).
## ⚡ Requirements
You need a test runner to generate the report summary file. For example [vitest](https://vitest.dev/guide/) or [jest](https://jestjs.io/docs/getting-started).
## ⚡ Setup
### 🔶 Install
```shell
npm i -D node-coverage-badges
# or
yarn add -D node-coverage-badges
# or
pnpm i -D node-coverage-badges
```
### 🔶 Test runner configuration
You will need to add json-summary to coverage reporters in your test runner config.
#### 🧿 vitest
`vite config`
```typescript
import { defineConfig } from 'vitest/config';
export default defineConfig({
test: {
coverage: {
reporter: ['json-summary'],
// ...
},
},
});
```
#### 🧿 jest
`jest config`
```javascript
module.exports = {
coverageReporters: ["json-summary"];
// ...
};
```
## ⚡ Usage
You have two ways to generate coverage badges: cli and node. Both will create a folder where .svg files will be written.
### 🔶 Cli
```bash
generateBadges -c [coverageJsonSummaryPath] -o [outputPath] -l [logo]
Options:
--help Show help [boolean]
--version Show version number [boolean]
-c coverage file path[default: "./coverage/coverage-summary.json"]
-o output path [default: "./badges"]
-l vitest [default: ]
-p badges label prefix [default: "Test coverage"]
-d debug [default: false]
Examples:
generateBadges -c ./coverage/coverage-summary.json -o ./badges -l vitest
Generates badges from a coverage report
```
You can add a script to your package.json like so:
```json
"scripts": {
"badges": "generateBadges", // cjs
"badges-esm": "generateBadgesEsm" // esm
},
```
The `generateBadges` function accepts three optional arguments to specify:
- a custom path for the input json summary file.
- a custom path for the output path.
- a [simple icon](https://simpleicons.org/) slug to specify a custom badge icon.
```shell
# will generate badges from './coverage/coverage-summary.json' in './badges' (default)
pnpm generateBadges
# will generate badges from './myModule/coverage-summary.json' in './cool' folder.
pnpm generateBadges -c ./myModule/coverage-summary.json -o ./cool
# will generate badges from './myModule/coverage-summary.json' in './cool' folder using the vitest icon.
pnpm generateBadges -c ./myModule/coverage-summary.json -o ./cool -l vitest
# will generate badges from './myModule/coverage-summary.json' in './cool' folder using the vitest icon with badges labels matching 'Repo test coverage: %d'.
pnpm generateBadges -c ./myModule/coverage-summary.json -o ./cool -l vitest -p 'Repo test coverage'
```
### 🔶 Node
You can generate badges from a summary file or raw values in node.
#### 🧿 Generate badges from a summary file
```typescript
import { generateBadges } from 'node-coverage-badges';
// will generate badges from './coverage/coverage-summary.json' in './badges' (default)
await generateBadges();
```
The function optionally accepts two arguments to specify a custom path for the json summary file and the output path:
```typescript
import { generateBadges } from 'node-coverage-badges';
// will generate badges from './myModule/coverage-summary.json' in './cool' using the jest icon.
await generateBadges(
'./myModule/coverage-summary.json',
'./cool',
'jest',
'My badges labels prefix'
);
```
You can also directly import the effect, if you use [Effect](https://effect.website/docs/introduction):
```typescript
import { generateBadgesEffect } from 'node-coverage-badges';
const task = Effect.gen(function* () {
//...
const result = yield* generateBadgesEffect();
});
```
The function signature is the following:
```typescript
const generateBadgesEffect: (
coverageSummaryPath?: string,
outputPath?: string,
logo?: string
labelPrefix?: string
) => Effect.Effect;
```
#### 🧿 Generate badges from raw values
```typescript
import { generateBadgesFromValues } from 'node-coverage-badges';
const rawValues = {
total: {
branches: {
pct: 25,
},
functions: {
pct: 40,
},
lines: {
pct: 30,
},
statements: {
pct: 70,
},
},
};
await generateBadgesFromValues(rawValues, './badges', 'vitest', 'Coverage');
```
Effect signature is the following:
```typescript
const generateBadgesFromValuesEffect: (
summaryValues: CoverageSummaryValue,
outputPath?: string,
logo?: string
labelPrefix?: string
) => Effect.Effect;
```
#### 🧿 Debug
You can pass a boolean as last argument to get generation info:
```typescript
import { generateBadges } from 'node-coverage-badges';
await generateBadges(
'./myModule/coverage-summary.json',
'./cool',
'jest',
'My badges labels prefix'
true
);
```
Which will provide the following kind of output:

## ⚡ Thanks
Big thanks to [Shield](https://github.com/badges/shields) for this awesome tool!