Skip to content

Commit ebc9831

Browse files
committed
Cleanup
1 parent dd2a702 commit ebc9831

25 files changed

+228
-228
lines changed

context-proxy.js

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

fixtures/proxyContext.js

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

lib/contextProxy.js

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/**
2+
* Proxy class for test and suite contexts.
3+
*
4+
* @param {Spec} [spec]
5+
*/
6+
function ContextProxy(spec) {
7+
this.spec = spec;
8+
this.config = {};
9+
}
10+
11+
/**
12+
* Set test timeout.
13+
*
14+
* @param {number} ms
15+
* @return {ContextProxy}
16+
*/
17+
ContextProxy.prototype.timeout = function(ms) {
18+
this.config.timeout = ms;
19+
if (!this.spec) return this;
20+
this.spec.timeout = setTimeout(function() {
21+
var error = new Error('timeout of ' + ms + 'ms exceeded. Ensure the ' +
22+
'done() callback is being called in this test.');
23+
error.stack = null;
24+
throw error;
25+
}, ms);
26+
return this;
27+
};
28+
29+
/**
30+
* Set test slowness threshold.
31+
*
32+
* @param {number} ms
33+
* @return {ContextProxy}
34+
*/
35+
ContextProxy.prototype.slow = function(ms) {
36+
this.config.slow = ms;
37+
return this;
38+
};
39+
40+
/**
41+
* Mark a test as skipped.
42+
*
43+
* @return {ContextProxy}
44+
*/
45+
ContextProxy.prototype.skip = function() {
46+
this.config.skip = true;
47+
return this;
48+
};
49+
50+
/**
51+
* Overrides original context behavior and configuration.
52+
*
53+
* @param {Context} ctx
54+
*/
55+
ContextProxy.prototype.patchContext = function(ctx) {
56+
if (this.spec && this.spec.timeout) {
57+
ctx.timeout(0);
58+
} else if (this.config.timeout) {
59+
ctx.timeout(this.config.timeout);
60+
}
61+
62+
if (this.config.slow) {
63+
ctx.slow(this.config.slow);
64+
}
65+
66+
if (this.config.skip) {
67+
ctx.skip();
68+
}
69+
70+
// Derive correct runnable duration
71+
if (this.spec && ctx.runnable) {
72+
var spec = this.spec;
73+
var originalDuration = ctx.runnable().duration;
74+
delete ctx.runnable().duration;
75+
Object.defineProperty(ctx.runnable(), 'duration', {
76+
get: function () {
77+
return spec.duration || originalDuration;
78+
},
79+
set: function (duration) {
80+
originalDuration = duration;
81+
},
82+
enumerable: true,
83+
configurable: true
84+
});
85+
}
86+
};
87+
88+
module.exports = ContextProxy;

index.js renamed to lib/parallel.js

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
var Promise = require('bluebird');
2-
var domain = require('domain');
3-
var ContextProxy = require('./context-proxy');
4-
var hookTypes = ['before', 'beforeEach', 'afterEach', 'after'];
1+
var Promise = require('bluebird');
2+
var domain = require('domain');
3+
var ContextProxy = require('./contextProxy');
4+
var hookTypes = ['before', 'beforeEach', 'afterEach', 'after'];
55

66
/**
77
* Generates a suite for parallel execution of individual specs. While each
@@ -58,8 +58,8 @@ function _parallel(name, fn, key) {
5858
var restoreUncaught;
5959
var parentHooks;
6060
var run;
61-
var parallel_ctx = new ContextProxy();
62-
fn.call(parallel_ctx);
61+
var parallelCtx = new ContextProxy();
62+
fn.call(parallelCtx);
6363

6464
restoreIt();
6565
restoreHooks();
@@ -103,7 +103,7 @@ function _parallel(name, fn, key) {
103103
};
104104

105105
(key ? describe[key] : describe)(name, function() {
106-
parallel_ctx.patchContext(this);
106+
parallelCtx.patchContext(this);
107107
var parentContext = this;
108108
if (!specs.length) return;
109109

@@ -194,11 +194,9 @@ function patchIt(specs) {
194194
var spec = {
195195
name: name,
196196
getPromise: function() {
197-
return Promise.resolve().then(function () {
198-
var start = new Date();
199-
return createWrapper(fn, spec.ctx)().then(function () {
200-
spec.duration = (new Date()) - start;
201-
});
197+
var start = Date.now();
198+
return createWrapper(fn, spec.ctx)().then(function() {
199+
spec.duration = Date.now() - start;
202200
});
203201
},
204202
duration: 0,

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
"name": "mocha.parallel",
33
"version": "0.12.0",
44
"description": "Run async mocha specs in parallel",
5-
"main": "index.js",
5+
"main": "lib/parallel.js",
66
"scripts": {
7-
"test": "mocha test.js"
7+
"test": "mocha spec/spec.js"
88
},
99
"repository": {
1010
"type": "git",
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var parallel = require('../index.js');
1+
var parallel = require('../../lib/parallel');
22
var assert = require('assert');
33

44
parallel('suite', function() {

spec/fixtures/contextProxy.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
var parallel = require('../../lib/parallel');
2+
var assert = require('assert');
3+
4+
parallel('suite', function () {
5+
this.timeout(3000);
6+
this.slow(2600);
7+
8+
it('test1', function(done) {
9+
this.timeout(100);
10+
setTimeout(done, 500);
11+
});
12+
13+
it('test2', function(done) {
14+
this.skip();
15+
setTimeout(done, 500);
16+
});
17+
18+
it('test3', function(done) {
19+
this.slow(200);
20+
setTimeout(done, 500);
21+
});
22+
23+
it('test4', function(done) {
24+
setTimeout(done, 2500);
25+
});
26+
});
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var parallel = require('../index.js');
1+
var parallel = require('../../lib/parallel');
22

33
parallel('suite', function() {
44
it('test1', function(done) {
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var parallel = require('../index.js');
1+
var parallel = require('../../lib/parallel');
22

33
describe('parent', function() {
44
this.timeout(0);
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var parallel = require('../index.js');
1+
var parallel = require('../../lib/parallel');
22
var Promise = require('bluebird');
33
//Based on issue #13
44

0 commit comments

Comments
 (0)