unroll-it
v1.0.0
Published
Helper for writing data driven / parametrized tests with Mocha
Downloads
4
Readme
Unroll It
Simple helper function for writing data-driven / parametrized unit tests in Mocha.
Introduction
Mocha does not have explicit support writing parametrized tests. Instead it suggests using imperative code to dynamically generate test cases.
This module provides a helper function which is a wrapper around the it()
function for declaratively creating data-driven tests. The end result is the
same as in the example in Mocha's
documentation but the code
is shorter and hopefully easier to read. My experience has been that having an
explicit method for creating data-driven tests tends to promote their use in a
codebase.
Installation
npm install unroll-it
Usage
unroll-it exports a function that is used in place of the "it" function to create a test which is run once for each case in an array of test cases. The syntax is:
unroll(description, testFunction, testCases);
Each test case is an object which is passed to the testFunction
. The properties
in the test case may also be referenced in the description
.
Here is a simple synchronous test:
var unroll = require('unroll-it');
describe('upperCase', () => {
unroll('should return #output given #input', (testCase) => {
assert.equal(upperCase(testCase.input), testCase.output);
},[{
input: 'john',
output: 'JOHN',
},{
input: 'mark',
output: 'MARK',
}]);
});
The "#output" and "#input" placeholders will be replaced with the corresponding values from the test case.
Asynchronous tests
Similar to the it()
function in Mocha, asynchronous tests can be written
either by returning a Promise
from the test function or by accepting a
done()
callback as the first argument to the test function which is called
when the test completes.