Mocha Badge Generator
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.
npm install mocha-badge-generator --save-dev
package.json, add reporter to your test script.
Default output file is
You can change the output by defining Mocha
--reporter-options or environment variables in your test script.
|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_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
||ES6 template for formatting the results; will be passed
|badge_threshold||MOCHA_BADGE_GEN_THRESHOLD||0||Number of acceptable failures (such that if exceeded,
|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_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,
Sample config for changing output to PNG.
Alternatively, you may use environmental variables:
Adding to your README
If you want the badge to show in npm, use the following format.
ianpogi5/mocha-badge-generator to your own github repo.
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).
const makeBadge = ;;
You can make a badge out of a Mocha JSON reporter report (as a JSON file).
const makeBadgeFromJSONFile = ;;
makeBadgeFromJSONFile functionality is exposed from the command line: