├── .gitignore ├── LICENSE.md ├── README.md ├── lib ├── index.d.ts └── index.js ├── package.json └── test ├── index.js └── require.test.js /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | build/ 3 | node_modules/ 4 | 5 | npm-debug.log* -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2013 Mikejsdev 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Mocha param 2 | 3 | ## Parameterized tests for Mocha. 4 | 5 | ``` 6 | npm install --save-dev mocha-param 7 | ``` 8 | # New in Version 2.0.0 9 | 10 | - The tested value can now be included in the description for each test. 11 | - Optionally use require('mocha-param') rather than require('mocha-param').itParam; 12 | 13 | 14 | # Basic Usage 15 | 16 | Simply use 'itParam' instead of the standard mocha 'it' function and pass in an array of data. 17 | 18 | ```javascript 19 | var itParam = require('mocha-param'); 20 | // We have used chai as an assertion library but you can use any. 21 | var expect = require('chai').expect; 22 | 23 | // A Simple sync example taking an array as a parameter. 24 | // 'value' is each value in the array 25 | describe("basic mocha test with data", function () { 26 | itParam("test value is a number", [1, 2, 3], function (value) { 27 | expect(value).to.be.a('number'); 28 | }) 29 | }) 30 | ``` 31 | 32 | Result: 33 | 34 | ``` 35 | basic mocha test with data 36 | ✓ test value is a number 37 | ✓ test value is a number 38 | ✓ test value is a number 39 | 40 | 41 | 3 passing (25ms) 42 | ``` 43 | 44 | # Add the values being tested into the test descriptions 45 | 46 | To display the values being passed into your tests, use "${value}" as part of the test description. 47 | 48 | ```javascript 49 | describe("basic mocha test with data", function () { 50 | itParam("test value ${value} is a number", [1, 2, 3], function (value) { 51 | expect(value).to.be.a('number'); 52 | }) 53 | }) 54 | ``` 55 | 56 | Result: 57 | 58 | ``` 59 | basic mocha test with data 60 | ✓ test value 1 is a number 61 | ✓ test value 2 is a number 62 | ✓ test value 3 is a number 63 | 64 | 65 | 3 passing (25ms) 66 | ``` 67 | 68 | # Async 69 | 70 | Standard async mocha tests take a 'done' parameter which is called when execution is finished. 71 | mocha-param works the same. 72 | 73 | ```javascript 74 | // A Simple async example taking an array and calling done() 75 | // 'value' each value in the array 76 | // 'done' is the standard mocha done callback 77 | describe("async mocha test with data", function () { 78 | itParam("test value ${value} is a number", [1, 2, 3], function (done, value) { 79 | expect(value).to.be.a('number'); 80 | done(); 81 | }) 82 | }) 83 | ``` 84 | 85 | Result: 86 | 87 | ``` 88 | async mocha test with data 89 | ✓ test value 1 is a number 90 | ✓ test value 2 is a number 91 | ✓ test value 3 is a number 92 | 93 | 94 | 3 passing (17ms) 95 | ``` 96 | 97 | # Array Objects 98 | 99 | The array can contain anything that you like. Nested values to be displayed in the test description can be accessed with `${value.}` 100 | 101 | ```javascript 102 | var myData = [{ name: 'rob', age: 23 }, { name: 'sally', age: 29 }]; 103 | 104 | describe("test that person objects are older than 20", function () { 105 | itParam("test person ${value.name} (age ${value.age}) in the array", myData, function (person) { 106 | expect(person.age).to.be.greaterThan(20); 107 | }) 108 | }) 109 | ``` 110 | 111 | Result: 112 | 113 | ``` 114 | test that person objects are older than 20 115 | ✓ test person rob (age 23) in the array 116 | ✓ test person sally (age 29) in the array 117 | 118 | 119 | 2 passing (14ms) 120 | ``` 121 | -------------------------------------------------------------------------------- /lib/index.d.ts: -------------------------------------------------------------------------------- 1 | import { Done } from 'mocha' 2 | 3 | export default function itParam(desc: string, data: T[], callback: (value: T) => void): void; 4 | export default function itParam(desc: string, data: T[], callback: (done: Done, value: T) => void): void; 5 | -------------------------------------------------------------------------------- /lib/index.js: -------------------------------------------------------------------------------- 1 | /* 2 | * Wrap calls to mocha "it" function in a forEach loop 3 | */ 4 | 5 | /* 6 | * The library is wrapped in a function (IIFE) that allows 7 | * mocha param to be required simply by using require('mocha-param') 8 | * but ensures backwards compatibility with those still using require('mocha-param').itParam 9 | */ 10 | module.exports = function (desc, data, callback) { 11 | 12 | let wrapper = function (desc, data, callback) { 13 | run(desc, data, callback); 14 | }; 15 | 16 | wrapper.itParam = run; 17 | return wrapper; 18 | }() 19 | 20 | /* 21 | * Call mocha "it" function either sync or async depending 22 | * on whether callback has one or two params 23 | */ 24 | function run(desc, data, callback) { 25 | if (callback.length === 2) { 26 | callItAsync(desc, data, callback) 27 | } else { 28 | callItSync(desc, data, callback) 29 | } 30 | } 31 | 32 | function callItSync(desc, data, callback) { 33 | data.forEach(function (val) { 34 | it(renderTemplate(desc, val), function () { 35 | return callback(val); 36 | }); 37 | }); 38 | 39 | } 40 | 41 | function callItAsync(desc, data, callback) { 42 | data.forEach(function (val) { 43 | it(renderTemplate(desc, val), function (done) { 44 | callback(done, val); 45 | }); 46 | }); 47 | } 48 | 49 | /* 50 | * Add value to description 51 | */ 52 | function renderTemplate(template, value) { 53 | try { 54 | return eval('`' + template + '`;') 55 | } catch (err) { 56 | return template; 57 | } 58 | } -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "mocha-param", 3 | "version": "2.0.1", 4 | "description": "Parameterized tests for Mocha", 5 | "main": "lib/index.js", 6 | "types": "lib/index.d.ts", 7 | "scripts": { 8 | "test": "mocha ./test/*.js" 9 | }, 10 | "keywords": [ 11 | "parameterized", 12 | "test", 13 | "mocha", 14 | "tests" 15 | ], 16 | "author": "mikejsdev", 17 | "license": "MIT", 18 | "repository": { 19 | "type": "git", 20 | "url": "git://github.com/mikejsdev/mocha-param.git" 21 | }, 22 | "devDependencies": { 23 | "mocha": "^3.2.0", 24 | "chai": "^3.5.0" 25 | } 26 | } -------------------------------------------------------------------------------- /test/index.js: -------------------------------------------------------------------------------- 1 | var itParam = require('../lib/index'); 2 | 3 | var expect = require('chai').expect; 4 | 5 | 6 | describe("Calling itParam in a loop sync with a value", function () { 7 | itParam("should all pass", [1, 1, 1], function (value) { 8 | expect(value).to.equal(1); 9 | }); 10 | }); 11 | 12 | describe("Calling itParam with objects works", function () { 13 | itParam("should all pass", [{ data: 1 }, { data: 1 }, { data: 1 }], function (value) { 14 | expect(value.data).to.equal(1); 15 | }); 16 | }); 17 | 18 | describe("Values are printed in description", function () { 19 | itParam("value is: ${value}", [1, 1, 1], function (value) { 20 | expect(value).to.equal(1); 21 | }); 22 | }); 23 | 24 | describe("Calling itParam in a loop sync without a value", function () { 25 | itParam("should all pass", [1, 1, 1], function () { 26 | expect(1).to.equal(1); 27 | }); 28 | }); 29 | 30 | describe("Calling itParam in a loop Async with a done function and a value", function () { 31 | itParam("should all pass", [1, 1, 1], function (done, value) { 32 | expect(value).to.equal(1); 33 | done(); 34 | }); 35 | }); 36 | 37 | describe("Calling itParam in a loop Async with values in description", function () { 38 | itParam("values are: ${value} ", [1, 1, 1], function (done, value) { 39 | expect(value).to.equal(1); 40 | done(); 41 | }); 42 | }); 43 | 44 | describe("Object literal in description should work", function () { 45 | itParam(`should pass`, [1, 1, 1], function (done, value) { 46 | expect(value).to.equal(1); 47 | done(); 48 | }); 49 | }); 50 | 51 | -------------------------------------------------------------------------------- /test/require.test.js: -------------------------------------------------------------------------------- 1 | var itParam = require('../lib/index').itParam; 2 | var expect = require('chai').expect; 3 | 4 | 5 | describe("Calling itParam in a loop sync with a value", function () { 6 | itParam("should all pass", [1, 1, 1], function (value) { 7 | expect(value).to.equal(1); 8 | }); 9 | }); 10 | 11 | describe("Calling itParam with objects works", function () { 12 | itParam("should all pass", [{ data: 1 }, { data: 1 }, { data: 1 }], function (value) { 13 | expect(value.data).to.equal(1); 14 | }); 15 | }); 16 | 17 | describe("Calling itParam in a loop Async with values in description", function () { 18 | itParam("values are: ${value} ", [1, 1, 1], function (done, value) { 19 | expect(value).to.equal(1); 20 | done(); 21 | }); 22 | }); 23 | 24 | 25 | --------------------------------------------------------------------------------