Giter VIP home page Giter VIP logo

node-run-mocha-cases's Introduction

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

node-run-mocha-cases's People

Watchers

Kael avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.