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 = ;startcases; // Default runner which could be override by `case.runner`.{ if n < 0 throw 'n should not less than 0'; return n + 1;}
cases
The 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. { to; } 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()` { // 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; ; } expect: 2 d: 'will use deep equal for object result' a: {} { obja = 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 { var done = this; ; } // 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. { return n + m; } e: 3 d: 'handle multiple results by using `expect` of function type' a: 1 { var done = this; ; } { var expect = expect; to; to; to; } ;
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 thedescribe
method. If not setrun-mocha-cases
will not invodedescribe
method of mocha. - runner
function()=
(optional) defines the default runner function. It can be overridden bycases[i].runner
. If each case has a runner function, youstart(cases)
with the defaultrunner
being unset. - cases
Array.<case>
test cases to run.
The simplest situation:
runstartcases;
Some other usages:
startcases;
.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