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 🙏

© 2026 – Pkg Stats / Ryan Hefner

mocha-cases

v0.3.0

Published

A tiny mocha test case runner. Suited for simple input to output validation tests.

Readme

mocha-cases

A tiny mocha test case runner. Suited for simple input to output validation tests.

MIT npm version David Dependency Badge Build Status

NPM NPM

Install

npm install mocha-cases

Usage

One case one value

var test = require('mocha-cases');

var cases = [{
  name: 'should {value.text} equal to {expected.text}', // name of the test,
                                                        //   a string, supports nested value interpolation
                                                        //   or a function, takes test value, expected value and options, and returns a string
  value: { text: 'input value' },                       // input value
  expected: { text: 'expected output value' },          // expected output value
  error: RangeError,                                    // expected error value, instance or class
  runner: function (value, options) {},                 // runner specific to this case
  options: {},                                          // options specific to this case
  only: false,                                          // run this case only?
  skip: false,                                          // skip this case?
  errback: false                                        // is the runner using an errback (callback)?
}, {
  name: 'case 2...',
  ...
}];

var options = {
  errback: true,                                        // is all test defaults to errback?
  prefix: ''                                             // prefix to test names
};

function runner(value, options, done) {                 // errback runner takes a `done` callback
  setTimeout(function () {
    done(null, 'expected output value');
  }, 10);
}

describe('module: mocha-cases', function () {
  describe('feature: cases', function () {
    test(cases, runner, options);
  });
});

Default pass through runner will be used if no runner provided at all.

One case vs. multiple values vs. one expected

You can use an array of values with a single expected value:

describe('prime number', function () {
  test({
    name: 'given prime number {value}, isPrime() returns true',
    values: [2, 3, 5, 7, 11, 13],
    expected: true
  }, isPrime);
});

One case vs. multiple values vs. multiple expected

You can use an array of values and an array of expected values, to pair multiple given values and expected values:

describe('prime number', function () {
  test({
    name: 'given prime number {value}, isPrime() returns true, false otherwise',
    values:   [2,    3,    4,     5,    6,     7,    8,     9],
    expected: [true, true, false, true, false, true, false, false],
    runner: isPrime
  });
});

Or, you can use cases to specify multiple cases:

describe('prime number', function () {
  test({
    name: 'isPrime({value}) should be {expected}',
    cases: [{
      value: 2,
      expected: true
    }, {
      value: 3,
      expected: true
    }, {
      value: 4,
      expected: false
    }, {
      value: 5,
      expected: true
    }, {
      value: 6,
      expected: false
    }, {
      value: 7,
      expected: true
    }, {
      value: 8,
      expected: false
    }, {
      value: 9,
      expected: false
    }],
    runner: isPrime
  });
});

If your values are simple enougth, you may want to simplify them with a pair of value / expected value for each case:

describe('prime number', function () {
  test({
    name: 'isPrime({value}) should be {expected}',
    cases: [
      [2, true],
      [3, true],
      [4, false],
      [5, true],
      [6, false],
      [7, true],
      [8, false],
      [9, false]
    ],
    runner: isPrime
  });
});

Test

$ npm test

Alternatives

Change Logs

  • 2019/02/22 - 0.3.0

    • Feature: Allow escaping the brace characters { and } with \\.
    • Feature: Allow entries of cases be an object.
  • 2018/01/23 - 0.2.1

    • Feature: Accept function for test name
    • Feature: Add default pass through runner
  • 2018/01/14 - 0.2.0

    • NPM: Bump version, update readme and publish to npm.js.
  • 2017/12/07 - 0.1.11

    • Feature: New format for adding test-cases.
  • 2016/01/08 - 0.1.10

    • Feature: Allow error to be an Error instance, a class or a normal value.
    • Feature: Allow test case negate errback option that enabled by overall options.
  • 2016/01/07 - 0.1.9

    • Feature: Deprecate the async option. For sync/async runner that returning value, i.e. primitive value, promise, stream or observable, you don't have to add any option. For async runner that use errback (callback), you need to add errback option.
  • 2016/01/06 - 0.1.8

    • Feature: Replace chai-as-promised with async-done. Now async runner can use callback or return promise, stream or observable.
  • 2015/12/24 - 0.1.6

    • NPM: Update npm settings.
  • 2015/12/16 - 0.1.5

    • Bug Fix: Fix error when expected values array contains falsy value.
  • 2015/12/07 - 0.1.4

    • NPM: Move mocha from "dependencies" to "peerDependencies".
  • 2015/12/03 - 0.1.3

    • Feature: Allow multiple values in one case using "values" keyword.
  • 2015/12/03 - 0.1.1

    • Feature: Make runner optional, or can be defined either in global options or case options.
    • Feature: Allow value interpolation in test name.
  • 2015/11/23 - 0.1.0

    • First release.

License

MIT

Author

Amobiz

Contributor

Ivan Sosnin