Skip to content
activity

GitHub Action

Vitest Badge Action

v1.0.0 Latest version

Vitest Badge Action

activity

Vitest Badge Action

This action parses the json summary created by Vitest, and creates a badge based on the results

Installation

Copy and paste the following snippet into your .yml file.

              

- name: Vitest Badge Action

uses: wjervis7/[email protected]

Learn more about this action in wjervis7/vitest-badge-action

Choose a version

Vitest Badge Action

Validate Build Lines Lines Lines Lines

This action will parse the json summary created by Vitest, and create a status badge based on the results, and optionally upload to a Gist.

Usage

This action requres you to use Vitest to create a coverage report, with the json-summary reporter.

You can configure the reporter in vitest.config.ts file, like this

import { defineConfig } from 'vitest/config'

export default defineConfig({
    test {
      coverage {
        reporter [json-summary]
      }
    }
});

You must execute vitest --coverage, in a step, prior to this action.

Permissions

You must provide a PAT, with write permission, to your Gist, if you want to upload the badge.

Inputs

parameter description default required
result-type Type of result to make badge for. Can be 'lines', 'statements', 'functions', or 'branches'. true
vitest-config-path Path to the vitest config file. vitest.config.ts true
summary-path Path to the json summary file. ./coverage/coverage-summary.json true
badge-text Text to display on badge. Tests true
badge-pass-color An array (comma separated) with hex (without #) or named colors of the badge value background, when coverage is at or above the threshold. More than one creates gradient background. 31c653 true
badge-fail-color An array (comma separated) with hex (without #) or named colors of the badge value background, when coverage is below the threshold. More than one creates gradient background. 800000 true
badge-neutral-color An array (comma separated) with hex (without #) or named colors of the badge value background, when coverage results were not found. More than one creates gradient background. 696969 true
badge-path Path to save the temporary badge to. badge.svg true
upload-badge Indicate if badge should be uploaded to Gist. true false
gist-token PAT for writing to gist. false
gist-url Url to Gist. false

Example Workflows

Example 1

Runs step, and uploads the badge to Gist, if target branch is main.

---
name: Example1

on:
  pull_request:
    branches:
      - main
      - dev

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout Code
        uses: actions/checkout@v3

      - name: Use Node.js
        uses: actions/setup-node@v3
        with:
          node-version: "20"

      - name: Install dependencies
        run: npm ci

      - name: Run Unit Tests
        run: npx vitest --coverage # or run npm script

      - name: Publish Results Badge
        uses: wjervis7/[email protected]
        if: success() || failure() # run whether steps succeed or not
        with:
          result-type: lines
          gist-token: ${{ secrets.GIST_TOKEN }} # if you want to upload badge to gist
          gist-url: https://gist.github.com/{org/user}/{gist_id}
          upload-badge: ${{ github.base_ref == 'refs/heads/main' }}

Example 2

Runs step, and commits the badge to Git

---
name: Example2

on:
  pull_request:
    branches:
      - main
      - dev

jobs:
  build:
    runs-on: ubuntu-latest

    permissions:
      contents: write # needed to commit the changes

    steps:
      - name: Checkout Code
        uses: actions/checkout@v3

      - name: Use Node.js
        uses: actions/setup-node@v3
        with:
          node-version: "20"

      - name: Install dependencies
        run: npm ci

      - name: Run Unit Tests
        run: npx vitest --coverage # or run npm script

      - name: Publish Results Badge
        uses: wjervis7/[email protected]
        if: success() || failure() # run whether steps succeed or not
        with:
          result-type: statements
          upload-badge: false          

      - name: Commit changes
        uses: stefanzweifel/git-auto-commit-action@v4
        with:
          file_pattern: './badge.svg'