Skip to content

Commit a54bef9

Browse files
committed
Fix #30: correctly report duration for synchronous specs
1 parent 559bedf commit a54bef9

File tree

3 files changed

+61
-5
lines changed

3 files changed

+61
-5
lines changed

lib/parallel.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -219,8 +219,8 @@ function patchIt(specs) {
219219
name: name,
220220
getPromise: function() {
221221
var start = Date.now();
222-
return createWrapper(fn, spec.ctx)().then(function() {
223-
spec.duration = Date.now() - start;
222+
return createWrapper(fn, spec.ctx)().then(function(duration) {
223+
spec.duration = duration;
224224
});
225225
},
226226
duration: 0,
@@ -283,7 +283,8 @@ function patchHooks(hooks) {
283283

284284
/**
285285
* Returns a wrapper for a given runnable's fn, including specs or hooks.
286-
* Optionally binds the function handler to the passed context.
286+
* Optionally binds the function handler to the passed context. Resolves
287+
* with the duration of the fn.
287288
*
288289
* @param {function} fn
289290
* @param {function} [ctx]
@@ -292,9 +293,11 @@ function patchHooks(hooks) {
292293
function createWrapper(fn, ctx) {
293294
return function() {
294295
return new Promise(function(resolve, reject) {
296+
var start = Date.now();
297+
295298
var cb = function(err) {
296299
if (err) return reject(err);
297-
resolve();
300+
resolve(Date.now() - start);
298301
};
299302

300303
// Wrap generator functions
@@ -306,7 +309,9 @@ function createWrapper(fn, ctx) {
306309

307310
// Synchronous spec, or using promises rather than callbacks
308311
if (!fn.length || (res && res.then)) {
309-
resolve(res);
312+
Promise.resolve(res).then(function() {
313+
resolve(Date.now() - start);
314+
});
310315
}
311316
});
312317
};

spec/fixtures/syncTime.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
var parallel = require('../../lib/parallel');
2+
3+
/**
4+
* This fixture exists in reference to issue #30. The following test suite
5+
* previously reported:
6+
*
7+
* suite
8+
* ✓ a (110ms)
9+
* ✓ b (109ms)
10+
* ✓ c (52ms)
11+
*
12+
* but now correctly reports:
13+
*
14+
* suite
15+
* ✓ a
16+
* ✓ b (60ms)
17+
* ✓ c (46ms)
18+
*/
19+
parallel('suite', function() {
20+
this.slow(10);
21+
22+
it('a', function(done) {
23+
done();
24+
});
25+
26+
it('b', function(done) {
27+
var a;
28+
for (var i = 0; i <= 100000000; i++){
29+
a = i;
30+
}
31+
done();
32+
});
33+
34+
it('c', function(done) {
35+
var a;
36+
for (var i = 0; i <= 100000000; i++){
37+
a = i;
38+
}
39+
done();
40+
});
41+
});

spec/spec.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,16 @@ describe('parallel', function() {
269269
done();
270270
});
271271
});
272+
273+
it.only('correctly reports duration for synchronous tests', function(done) {
274+
run(fixtures.syncTime, function(err, stdout, stderr) {
275+
if (err) return done(err);
276+
277+
assert(!stderr.length);
278+
assert.equal(stdout.indexOf('a ('), -1);
279+
done();
280+
});
281+
});
272282
});
273283

274284
/**

0 commit comments

Comments
 (0)