An open API service indexing awesome lists of open source software.

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

Awesome Lists containing this project

README

          

# node-coverage-badges

[![Open in Visual Studio Code](https://img.shields.io/static/v1?logo=visualstudiocode&label=&message=Open%20in%20Visual%20Studio%20Code&labelColor=2c2c32&color=007acc&logoColor=007acc)](https://github.dev/jpb06/node-coverage-badges)
![Github workflow](https://img.shields.io/github/actions/workflow/status/jpb06/node-coverage-badges/tests-scan.yml?branch=main&logo=github-actions&label=last%20workflow)
[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=jpb06_node-coverage-badges&metric=alert_status)](https://sonarcloud.io/summary/new_code?id=jpb06_node-coverage-badges)
[![Maintainability Rating](https://sonarcloud.io/api/project_badges/measure?project=jpb06_node-coverage-badges&metric=sqale_rating)](https://sonarcloud.io/dashboard?id=jpb06_node-coverage-badges)
[![Security Rating](https://sonarcloud.io/api/project_badges/measure?project=jpb06_node-coverage-badges&metric=security_rating)](https://sonarcloud.io/dashboard?id=jpb06_node-coverage-badges)
[![Reliability Rating](https://sonarcloud.io/api/project_badges/measure?project=jpb06_node-coverage-badges&metric=reliability_rating)](https://sonarcloud.io/dashboard?id=jpb06_node-coverage-badges)
[![Coverage](https://sonarcloud.io/api/project_badges/measure?project=jpb06_node-coverage-badges&metric=coverage)](https://sonarcloud.io/dashboard?id=jpb06_node-coverage-badges)
![Coverage](./badges/coverage-total.svg)
[![Lines of Code](https://sonarcloud.io/api/project_badges/measure?project=jpb06_node-coverage-badges&metric=ncloc)](https://sonarcloud.io/summary/new_code?id=jpb06_node-coverage-badges)
[![Technical Debt](https://sonarcloud.io/api/project_badges/measure?project=jpb06_node-coverage-badges&metric=sqale_index)](https://sonarcloud.io/summary/new_code?id=jpb06_node-coverage-badges)
[![Code Smells](https://sonarcloud.io/api/project_badges/measure?project=jpb06_node-coverage-badges&metric=code_smells)](https://sonarcloud.io/dashboard?id=jpb06_node-coverage-badges)
[![Bugs](https://sonarcloud.io/api/project_badges/measure?project=jpb06_node-coverage-badges&metric=bugs)](https://sonarcloud.io/summary/new_code?id=jpb06_node-coverage-badges)
[![Vulnerabilities](https://sonarcloud.io/api/project_badges/measure?project=jpb06_node-coverage-badges&metric=vulnerabilities)](https://sonarcloud.io/summary/new_code?id=jpb06_node-coverage-badges)
[![Duplicated Lines (%)](https://sonarcloud.io/api/project_badges/measure?project=jpb06_node-coverage-badges&metric=duplicated_lines_density)](https://sonarcloud.io/dashboard?id=jpb06_node-coverage-badges)
![npm bundle size](https://img.shields.io/bundlephobia/min/node-coverage-badges)
![Last commit](https://img.shields.io/github/last-commit/jpb06/node-coverage-badges?logo=git)

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 |
| ----------------------------------------------- | ---------------------------------------------- |
| ![Branches](./badges/coverage-branches.svg) | Percentage of DD-paths followed during tests |
| ![Functions](./badges/coverage-functions.svg) | Percentage of functions executed within tests |
| ![Lines](./badges/coverage-lines.svg) | Percentage of lines covered by tests |
| ![Statements](./badges/coverage-statements.svg) | Percentage of statements executed within tests |
| ![Tests coverage](./badges/coverage-total.svg) | 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:

![Debug](./assets/debug.png)

## ⚡ Thanks

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