Mocha-Each - Parameterized Test for Mocha
This module provides a way to write simple parameterized tests in Mocha.
Installation
npm install --save-dev mocha-each
Usage
Basic
The function mocha-each provides takes parameters as an array and returns a parameterized test function. You can define test cases for each parameter by the function. All the test cases you defined are executed even if one or more cases fail during the test.
// test.jsconst assert = ;const forEach = ; { return + ;} ;
Result:
At describe level
Similarly, it works on describe level
// test.jsconst assert = ;const forEach = ; { return - ;} ;
Asynchronous code
When testing asynchronous code, you need to add a callback as a last argument of
the test function and call it. The callback is same as the one which Mocha's it
gives.
See Mocha - asynchronous code for the detail.
;
Exclusive or inclusive tests
You can call .only() and .skip() in the same way as Mocha's it
.
// Run only these parameterized tests.it; // Ignore these parameterized tests.it;
Note:
When you use the .only()
, mocha-each creates a nameless test suite by describe()
to define exclusive parameterized tests because we can't call .only()
multiple times
(see Mocha docs).
API
forEach(parameters).it(title, testCase)
Array
parameters: An array of parameters. Each parameter will be applied to testCase
as its arguments.
String
or Function
title: A title of the test case. You can define each title as a string or function.
String title
When it is a string, you can use placeholders which will be replaced by the parameters. mocha-each uses sprintf-js to replace placeholders.
example:
;// =>// greets "Good morning" at 9:00// greets "Hello" at 12:00// greets "Good evening" at 21:00
Function title
When it is a function, it takes each parameter and index as its arguments like:
title(p[0], p[1], .., p[n - 1], index);
Function
testCase: A test function which will be called for each parameter. The context of the function
is same as Mocha's it
so that you can use configuration methods like timeout.
;
Supported interfaces
Though Mocha provides several interfaces, currently only the BDD interface is supported.
Tips
You can define the name of mocha-each function as you like when requiring it. Here's a example which uses more BDD-like name.
const withThese = ; ;
License
MIT