Skip to content

Commit

Permalink
Fail when test resolution method is overspecified
Browse files Browse the repository at this point in the history
Users may register `Runnable`s as asynchronous in one of two ways:

- Via callback (by defining the body function to have an arity of one)
- Via promise (by returning a Promise object from the body function)

When both a callback function is specified *and* a Promise object is
returned, the `Runnable`'s resolution condition is ambiguous.
Practically speaking, users are most likely to make this mistake as they
transition between asynchronous styles.

Currently, Mocha silently prefers the callback amd ignores the Promise
object. Update the implementation of the `Runnable` class to fail
immediately when the test resolution method is over-specified in this
way.
  • Loading branch information
jugglinmike authored and boneskull committed Jul 2, 2016
1 parent a228578 commit 71669d3
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/runnable.js
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ Runnable.prototype.run = function(fn) {
}

function callFnAsync(fn) {
fn.call(ctx, function(err) {
var result = fn.call(ctx, function(err) {
if (err instanceof Error || toString.call(err) === '[object Error]') {
return done(err);
}
Expand All @@ -361,6 +361,10 @@ Runnable.prototype.run = function(fn) {
}
return done(new Error('done() invoked with non-Error: ' + err));
}
if (result && utils.isPromise(result)) {
return done(new Error('Asynchronous resolution method is overspecified. Specify a callback *or* return a Promise, not both.'));
}

done();
});
}
Expand Down
10 changes: 10 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -748,3 +748,13 @@ exports.stackTraceFilter = function() {
return stack.join('\n');
};
};

/**
* Crude, but effective.
* @api
* @param {*} value
* @returns {boolean} Whether or not `value` is a Promise
*/
exports.isPromise = function isPromise(value) {
return typeof value === 'object' && typeof value.then === 'function';
};
8 changes: 8 additions & 0 deletions test/acceptance/overspecified-async.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
describe('overspecified asynchronous resolution method', function() {
it('should fail when multiple methods are used', function(done) {
setTimeout(done, 0);

// uncomment
// return { then: function() {} };
});
});
14 changes: 14 additions & 0 deletions test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,4 +182,18 @@ describe('utils', function() {
});
});
});

describe('.isPromise', function() {
it('should return true if the value is Promise-ish', function() {
utils.isPromise({then: function() {}}).should.be.true;
});

it('should return false if the value is not an object', function() {
utils.isPromise(1).should.be.false;
});

it('should return false if the value is an object w/o a "then" function', function() {
utils.isPromise({}).should.be.false;
});
});
});

0 comments on commit 71669d3

Please sign in to comment.