npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mocha-badge-generator

v0.11.0

Published

Mocha reporter which outputs badge with the number of tests passed and failed.

Downloads

253

Readme

Mocha Badge Generator

Test Build Status codecov contributions welcome

https://nodei.co/npm/mocha-badge-generator.png?downloads=true&downloadRank=true&stars=true

No need for 3rd party to generate badge for your tests. You can generate it locally by running your test script.

Mocha Badge Generator is a Mocha reporter which outputs a badge (SVG or PNG file) with the number of tests passed and failed which you can embed in your readme file.

Install

npm install mocha-badge-generator --save-dev

Usage

In your package.json, add reporter to your test script.

{
    "scripts": {
        "test": "mocha --reporter mocha-badge-generator",
    }
}

Default output file is test/badge.svg.

Configuration

You can change the output by defining Mocha --reporter-options or environment variables in your test script.

| --reporter-option | Env variable | Default | Description | |---------------------|--------------| :------: |-------------| | badge_subject | MOCHA_BADGE_GEN_SUBJECT | Tests | The text that appears the left side of the badge. | | badge_ok_color | MOCHA_BADGE_GEN_OK_COLOR | 44cc11 (brightgreen) | The color when all tests pass. Colors may be a 6-digit hex code or a named CSS color. If upgrading from <= 0.3.0, please note that if you were not relying on the default colors, the CSS named colors may differ from the badge-up builtin colors we were using previously; use the corresponding hex code (without the #), and convert to 6 digits (e.g., 4C1 to 44CC11). May now also be followed by a comma and s{ffffff} to add a different stroke color. | | badge_ko_color | MOCHA_BADGE_GEN_KO_COLOR | e05d44 (red) | The color when at least 1 test fail. See above for possible colors (and a note about upgrading from <= 0.3.0). | | badge_output | MOCHA_BADGE_GEN_OUTPUT | ./test/badge.svg | Path of the output file. | | badge_format | MOCHA_BADGE_GEN_FORMAT | svg | Output file format. Possible values are "svg" and "png". However, please note that for format "png", while versions <= 0.3.0 bundled svg2png, you must now add svg2png yourself (e.g., to your dependencies or devDependencies). | | badge_template | MOCHA_BADGE_GEN_TEMPLATE | "${passes}/${total}" | ES6 template for formatting the results; will be passed passes, failures, total, duration, speeds (with fast, medium, and slow property counts). | | badge_threshold | MOCHA_BADGE_GEN_THRESHOLD | 0 | Number of acceptable failures (such that if exceeded, badge_ko_color will be used in place of badge_ok_color). | | badge_slow_threshold | MOCHA_BADGE_GEN_SLOW_THRESHOLD | N/A | Indicates a maximum number of slow tests beyond which the tests will be considered a failure (such that if the amount is exceeded, badge_ko_color will be used in place of badge_ok_color). No default as only checked if present. | | badge_duration_threshold | MOCHA_BADGE_GEN_DURATION_THRESHOLD | N/A | Indicates a maximum duration in milliseconds beyond which the tests will be considered a failure (such that if the duration is exceeded, badge_ko_color will be used in place of badge_ok_color). No default as only checked if present. |

Sample config for changing output to PNG.

{
    "scripts": {
        "test": "mocha --reporter mocha-badge-generator --reporter-options=badge_format=png,badge_output=badge.png",
    }
}

Alternatively, you may use environmental variables:

{
    "scripts": {
        "test": "MOCHA_BADGE_GEN_FORMAT=png MOCHA_BADGE_GEN_OUTPUT=badge.png mocha --reporter mocha-badge-generator",
    }
}

Adding to your README

![Test](test/badge.svg)

If you want the badge to show in npm, use the following format.

![Test](https://raw.githubusercontent.com/ianpogi5/mocha-badge-generator/master/test/badge.svg?sanitize=true)

Change ianpogi5/mocha-badge-generator to your own github repo.

Other methods

While the main method may be sufficient, certain environments (such as Cypress) may call for creating a badge based on already-generated JSON test file results (e.g., when merging multiple test results into a single file).

makeBadge

const {makeBadge} = require('mocha-badge-generator/makeBadge');
makeBadge({
    // REQUIRED
    passes,
    failures,
    // OPTIONAL
    options: {
        // See above for expected values
        badge_subject,
        badge_ok_color,
        badge_ok_color,
        badge_output,
        badge_format,
        badge_template,
        badge_threshold,
        badge_slow_threshold,
        badge_duration_threshold
    }
});

makeBadgeFromJSONFile

You can make a badge out of a Mocha JSON reporter report (as a JSON file).

const {makeBadgeFromJSONFile} = require('mocha-badge-generator/makeBadge');
makeBadgeFromJSONFile({
    // REQUIRED
    // This JSON file expects the structure: `{stats: {passes, failures}}`
    file: '/path/to/JSON/file'
    // Options
    // Milliseconds which will be considered "slow" (and by Mocha's algorithm,
    //  any more than half of this will still be considered "medium"); this
    //  is not available in test reports even when individual tests add
    //  their own calls to `slow`, so reporting on `speeds` may only be
    //  useful if your tests have no idiosyncratic speed expectations.
    //  When using `makeBadge` (i.e., using with Mocha while it is running),
    //  however, there is no such limitation, and no need to specify `slow`
    //  as an option to `makeBadge` (just supply it as usual to Mocha).
    slow: 75,
    // See above for expected values
    badge_subject,
    badge_ok_color,
    badge_ok_color,
    badge_output,
    badge_format,
    badge_template,
    badge_threshold,
    badge_slow_threshold,
    badge_duration_threshold
});

CLI

The makeBadgeFromJSONFile functionality is exposed from the command line:

cli.svg