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

run-mocha-cases

v1.1.0

Published

Run an array list of similar mocha cases, making your test cases more readable and maintainable.

Downloads

14

Readme

Build Status

run-mocha-cases

Run an array list of similar mocha cases, making your test cases more readable and maintainable.

Install

$ npm install run-mocha-cases --save-dev

Usage

In your test/test.js:

var run = require('run-mocha-cases');
run(runner).start(cases);

// Default runner which could be override by `case.runner`.
function runner (n){
  if (n < 0) {
    throw new Error('n should not less than 0');
  }
  return n + 1;
}

The cases

We could write test cases in a neet and handy way.

var cases = [

  // The first test case is equivalent to:
  // ```js
  // it('plus', function(){
  //   function runner(n){
  //     if (n < 0) {
  //       throw new Error('n should not less than 0');
  //     }
  //     return n + 1;
  //   }
  //
  //   var result = runner(1);
  //   expect(result).to.equal(2);
  // });
  // ```
  {
    description: 'plus',
    args: 1,
    expect: 2
  },

  {
    // Short cut for `description`
    d: 'shortcut',
    // Shortcut for `args`
    a: 1,
    // Shortcut for `expect`
    // `expect` could be a function,
    // and you could use any assertion method inside it.
    e: function(n){
      require('chai').expect(n).to.equal();
    }
  },

  {
    d: 'throw errors',
    a: -1,
    // or `err`
    error: true
  },

  // This test case will fail if not skipped
  {
    d: 'if `error` is not set to `true`, the test case will fail',
    a: -1
  },

  {
    d: 'async test case',
    a: 1,
    // The shortcut is `r`
    // It will override the default runner which specified by `.run()` 
    runner: function(n){
      // Uses the common `this.async()` style to 
      // turn the runner into an asynchronous method.

      // ATTENSION! `this.async()` should be called outside the `setTimeout`.
      var done = this.async();

      setTimeout(function(){
        done(n + 1);
      }, 10);
    },
    expect: 2
  }, 

  {
    d: 'will use deep equal for object result',
    a: {},
    r: function (obj){
      obj.a = 1;
      return obj;
    },
    e: {
      a: 1
    }
  },

  // {
  //   only: 'only this test case will be run',
  //   a: 1,
  //   e: 2
  // },

  {
    d: 'generate multiple results by using done()',
    a: 1,
    r: function(n){
      var done = this.async();
      done(null, n + 1, n + 2);
    },
    // If there is 3(more than one_ parameter passed to `done`,
    // the `case.e` will be treated as three result.
    e: [null, 2, 3]
  },

  {
    d: 'multiple arguments to be passed into runner',
    a: [1, 2],
    // If the runner has 2(more than one) arguments, 
    // the `case.args` will be passed as 2 parameters instead of an array.
    r: function(n, m){
      return n + m;
    },
    e: 3
  },

  {
    d: 'handle multiple results by using `expect` of function type',
    a: 1,
    r: function(n){
      var done = this.async();
      done(null, n + 1, n + 2);
    },
    e: function(err, first, second){
      var expect = require('chai').expect;
      expect(err).to.equal(null);
      expect(first).to.equal(2);
      expect(second).to.equal(3);
    }
  }
];

run([description] [, runner]).start(cases)

run().description(description).runner(runner).start(cases)

run.description(description).runner(runner).start(cases)

run(description).runner(runner).start(cases)

Creates a runner by the specific description and the default runner function, and start running test cases

  • description string= (optional) defines the mocha description for the describe method. If not set run-mocha-cases will not invode describe method of mocha.
  • runner function()= (optional) defines the default runner function. It can be overridden by cases[i].runner. If each case has a runner function, you start(cases) with the default runner being unset.
  • cases Array.<case> test cases to run.
The simplest situation:
run.start(cases);
Some other usages:
run(runner).start(cases);

.start(cases)

Runs all test cases.

  • cases Array.<Object> The test cases to be run.

.describe(describe)

Specified the mocha's describe method. If you don't know how mocha works, leave this method alone.

Returns this.

.it(it)

Specified the mocha's it method. If you don't know how mocha works, leave this method alone.

Returns this.

License

MIT