Skip to content

Commit 68e334d

Browse files
committed
0.1.0
1 parent 23136d2 commit 68e334d

File tree

7 files changed

+99
-133
lines changed

7 files changed

+99
-133
lines changed

README.md

Lines changed: 30 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -14,87 +14,59 @@ npm install --save mocha.parallel
1414

1515
``` javascript
1616
/**
17-
* Builds the test suite dynamically to allow for parallel execution of the
18-
* individual specs. While each spec is ran in parallel, specs resolve in
19-
* series, leading to deterministic output. Expects an array of names,
20-
* arguments, and a function that returns a Promise.
17+
* Generates a suite for parallel execution of individual specs. While each
18+
* spec is ran in parallel, specs resolve in series, leading to deterministic
19+
* output. Compatible with both callbacks and promises. Supports before/after
20+
* hooks, but not afterEach/beforeEach hooks, nor nested suites.
2121
*
2222
* @example
23-
* parallel(['test1', 'test2'], [[1, 2], [2, 4]], function(x, expected) {
24-
* return Promise.delay(100).then(function() {
25-
* assert.equal(x + x, res);
23+
* parallel('setTimeout', function() {
24+
* it('test1', function(done) {
25+
* setTimeout(done, 500);
26+
* });
27+
* it('test2', function(done) {
28+
* setTimeout(done, 500);
2629
* });
2730
* });
2831
*
29-
* @param {string[]} names Names to assign the specs
30-
* @param {*[]} args Arguments to pass to the function
31-
* @param {} A function returning a promise
32+
* @param {string} name
33+
* @param {function} fn
3234
*/
3335
```
3436

3537
## Examples
3638

37-
Rather than taking 3.5s, the specs below run in parallel, completing in just
38-
over 900ms.
39+
Rather than taking 1.5s, the specs below run in parallel, completing in just
40+
over 500ms.
3941

4042
``` javascript
4143
var parallel = require('../index.js');
4244
var Promise = require('bluebird');
4345

44-
describe('delays', function() {
45-
var args = [500, 600, 700, 800, 900];
46-
parallel(args, args, function(arg) {
47-
return Promise.delay(arg);
46+
parallel('delays', function() {
47+
it('test1', function(done) {
48+
setTimeout(done, 500);
4849
});
49-
});
50-
```
51-
52-
```
53-
delays
54-
✓ 500 (497ms)
55-
✓ 600 (99ms)
56-
✓ 700 (98ms)
57-
✓ 800 (99ms)
58-
✓ 900 (99ms)
59-
60-
61-
5 passing (902ms)
62-
```
6350

64-
An example of passing multiple arguments
65-
66-
``` javascript
67-
var parallel = require('../index.js');
68-
var Promise = require('bluebird');
69-
var assert = require('assert');
70-
71-
describe('multiplications', function() {
72-
var args = [
73-
[1, 1, 1],
74-
[2, 2, 4],
75-
[3, 3, 9],
76-
[4, 4, 16]
77-
];
78-
79-
var names = args.map(function(val) {
80-
return val[0] + ' + ' + val[1] + ' = ' + val[2];
51+
it('test2', function(done) {
52+
setTimeout(done, 500);
8153
});
8254

83-
parallel(names, args, function(x, y, res) {
84-
return Promise.delay(100).then(function() {
85-
assert.equal(x * y, res)
86-
});
55+
it('test3', function() {
56+
return Promise.delay(500);
8757
});
8858
});
8959
```
9060

9161
```
92-
multiplications
93-
✓ 1 + 1 = 1 (101ms)
94-
✓ 2 + 2 = 4
95-
✓ 3 + 3 = 9
96-
✓ 4 + 4 = 16
62+
✓ test1 (506ms)
63+
✓ test2
64+
✓ test3
9765
98-
99-
4 passing (110ms)
66+
3 passing (516ms)
10067
```
68+
69+
## Caveats
70+
71+
Debugging parallel execution can be more difficult as exceptions may be thrown
72+
from any of the running specs.

fixtures/delay.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
var parallel = require('../index.js');
22
var Promise = require('bluebird');
33

4-
describe('delays', function() {
5-
var args = [500, 600, 700, 800, 900];
6-
parallel(args, args, function(arg) {
7-
return Promise.delay(arg);
4+
parallel('delays', function() {
5+
it('test1', function(done) {
6+
setTimeout(done, 500);
7+
});
8+
9+
it('test2', function(done) {
10+
setTimeout(done, 500);
11+
});
12+
13+
it('test3', function() {
14+
return Promise.delay(500);
815
});
916
});

fixtures/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
var path = require('path');
22

33
var absolutePaths = {};
4-
var fixtures = ['delay', 'multipleArgs'];
4+
var fixtures = ['delay'];
55

66
fixtures.forEach(function(fixture) {
77
absolutePaths[fixture] = path.resolve(__dirname, fixture + '.js');

fixtures/multipleArgs.js

Lines changed: 0 additions & 22 deletions
This file was deleted.

index.js

Lines changed: 45 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,55 @@
1+
var Promise = require('bluebird');
2+
13
/**
2-
* Builds the test suite dynamically to allow for parallel execution of the
3-
* individual specs. While each spec is ran in parallel, specs resolve in
4-
* series, leading to deterministic output. Expects an array of names,
5-
* arguments, and a function that returns a Promise.
4+
* Generates a suite for parallel execution of individual specs. While each
5+
* spec is ran in parallel, specs resolve in series, leading to deterministic
6+
* output. Compatible with both callbacks and promises. Supports before/after
7+
* hooks, but not afterEach/beforeEach hooks, nor nested suites.
68
*
79
* @example
8-
* parallel(['test1', 'test2'], [[1, 2], [2, 4]], function(x, expected) {
9-
* return Promise.delay(100).then(function() {
10-
* assert.equal(x + x, res);
10+
* parallel('setTimeout', function() {
11+
* it('test1', function(done) {
12+
* setTimeout(done, 500);
13+
* });
14+
* it('test2', function(done) {
15+
* setTimeout(done, 500);
1116
* });
1217
* });
1318
*
14-
* @param {string[]} names Names to assign the specs
15-
* @param {*[]} args Arguments to pass to the function
16-
* @param {} A function returning a promise
19+
* @param {string} name
20+
* @param {function} fn
1721
*/
18-
module.exports = function parallel(names, args, fn) {
19-
args.map(function(arg) {
20-
if (arg instanceof Array) {
21-
return fn.apply(fn, arg);
22-
} else {
23-
return fn(arg);
24-
}
25-
}).forEach(function(promise, i) {
26-
it(names[i], function() {
27-
return promise;
22+
module.exports = function parallel(name, fn) {
23+
var specs = [];
24+
var hooks = {};
25+
var original = it;
26+
27+
it = function it(name, fn) {
28+
var promise = new Promise(function(resolve, reject) {
29+
// Use timeout to prioritize hook execution
30+
setTimeout(function() {
31+
var res = fn(function(err) {
32+
if (err) return reject(err);
33+
resolve();
34+
});
35+
36+
// Using promises rather than callbacks
37+
if (res && res.then) resolve(res);
38+
});
39+
});
40+
41+
specs.push({
42+
name: name,
43+
promise: promise
44+
});
45+
};
46+
47+
describe(name, fn);
48+
it = original;
49+
50+
specs.forEach(function(spec) {
51+
it(spec.name, function() {
52+
return spec.promise;
2853
});
2954
});
3055
};

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "mocha.parallel",
3-
"version": "0.0.1",
3+
"version": "0.1.0",
44
"description": "Run async mocha specs in parallel",
55
"main": "index.js",
66
"scripts": {
@@ -24,7 +24,9 @@
2424
},
2525
"homepage": "https://github.com/danielstjules/mocha.parallel",
2626
"devDependencies": {
27-
"bluebird": "^2.9.34",
2827
"mocha": "^2.2.5"
28+
},
29+
"dependencies": {
30+
"bluebird": "^2.9.34"
2931
}
3032
}

test.js

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,37 +11,19 @@ describe('parallel', function() {
1111
if (err) return done(err);
1212

1313
assert(!stderr.length);
14-
assert(stdout.indexOf('5 passing') !== -1);
14+
assert(stdout.indexOf('3 passing') !== -1);
1515

16-
['500', '600', '700', '800', '900'].forEach(function(delay) {
17-
var line = '✓ ' + delay;
16+
[
17+
'✓ test1',
18+
'✓ test2',
19+
'✓ test3'
20+
].forEach(function(line) {
1821
assert(stdout.indexOf(line) !== -1);
1922
});
2023

2124
// Specs should run in under 1s
22-
var timeStr = stdout.split('5 passing (')[1].split(')')[0];
23-
assert(parseInt(timeStr, 10) < 1000);
24-
25-
done();
26-
});
27-
});
28-
29-
it('can pass multiple arguments', function(done) {
30-
var cmd = './node_modules/.bin/mocha ' + fixtures.multipleArgs;
31-
exec(cmd, function(err, stdout, stderr) {
32-
if (err) return done(err);
33-
34-
assert(!stderr.length);
35-
assert(stdout.indexOf('4 passing') !== -1);
36-
37-
[
38-
'✓ 1 + 1 = 1',
39-
'✓ 2 + 2 = 4',
40-
'✓ 3 + 3 = 9',
41-
'✓ 4 + 4 = 16'
42-
].forEach(function(line) {
43-
assert(stdout.indexOf(line) !== -1);
44-
})
25+
var timeStr = stdout.match(/passing \((\d+)ms\)/)[1];
26+
assert(parseInt(timeStr, 10) < 600);
4527

4628
done();
4729
});

0 commit comments

Comments
 (0)