|
1 | 1 | # mocha.parallel |
2 | | -Run async mocha specs in parallel |
| 2 | + |
| 3 | +Run async mocha specs in parallel. |
| 4 | + |
| 5 | +``` |
| 6 | +/** |
| 7 | + * Builds the test suite dynamically to allow for parallel execution of the |
| 8 | + * individual specs. While each spec is ran in parallel, specs resolve in |
| 9 | + * series, leading to deterministic output. Expects an array of names, |
| 10 | + * arguments, and a function that returns a Promise. |
| 11 | + * |
| 12 | + * @example |
| 13 | + * parallel(['test1', 'test2'], [[1, 2], [2, 4]], function(x, expected) { |
| 14 | + * return Promise.delay(100).then(function() { |
| 15 | + * assert.equal(x + x, res); |
| 16 | + * }); |
| 17 | + * }); |
| 18 | + * |
| 19 | + * @param {string[]} names Names to assign the specs |
| 20 | + * @param {*[]} args Arguments to pass to the function |
| 21 | + * @param {} A function returning a promise |
| 22 | + */ |
| 23 | +``` |
| 24 | + |
| 25 | +## Examples |
| 26 | + |
| 27 | +Rather than taking 3.5s, the specs below run in parallel, completing in just |
| 28 | +over 900ms. |
| 29 | + |
| 30 | +``` javascript |
| 31 | +var parallel = require('../index.js'); |
| 32 | +var Promise = require('bluebird'); |
| 33 | + |
| 34 | +describe('delays', function() { |
| 35 | + var args = [500, 600, 700, 800, 900]; |
| 36 | + parallel(args, args, function(arg) { |
| 37 | + return Promise.delay(arg); |
| 38 | + }); |
| 39 | +}); |
| 40 | +``` |
| 41 | + |
| 42 | +``` |
| 43 | + delays |
| 44 | + ✓ 500 (497ms) |
| 45 | + ✓ 600 (99ms) |
| 46 | + ✓ 700 (98ms) |
| 47 | + ✓ 800 (99ms) |
| 48 | + ✓ 900 (99ms) |
| 49 | +
|
| 50 | +
|
| 51 | + 5 passing (902ms) |
| 52 | +``` |
| 53 | + |
| 54 | +An example of passing multiple arguments |
| 55 | + |
| 56 | +``` javascript |
| 57 | +var parallel = require('../index.js'); |
| 58 | +var Promise = require('bluebird'); |
| 59 | +var assert = require('assert'); |
| 60 | + |
| 61 | +describe('multiplications', function() { |
| 62 | + var args = [ |
| 63 | + [1, 1, 1], |
| 64 | + [2, 2, 4], |
| 65 | + [3, 3, 9], |
| 66 | + [4, 4, 16] |
| 67 | + ]; |
| 68 | + |
| 69 | + var names = args.map(function(val) { |
| 70 | + return val[0] + ' + ' + val[1] + ' = ' + val[2]; |
| 71 | + }); |
| 72 | + |
| 73 | + parallel(names, args, function(x, y, res) { |
| 74 | + return Promise.delay(100).then(function() { |
| 75 | + assert.equal(x * y, res) |
| 76 | + }); |
| 77 | + }); |
| 78 | +}); |
| 79 | +``` |
| 80 | + |
| 81 | +``` |
| 82 | + multiplications |
| 83 | + ✓ 1 + 1 = 1 (101ms) |
| 84 | + ✓ 2 + 2 = 4 |
| 85 | + ✓ 3 + 3 = 9 |
| 86 | + ✓ 4 + 4 = 16 |
| 87 | +
|
| 88 | +
|
| 89 | + 4 passing (110ms) |
| 90 | +``` |
0 commit comments