Skip to content

armgit / mocha

Public Imported from GitHub
0

☕️ Classic, reliable, trusted test framework for Node.js and the browser

Fail when test resolution method is overspecified

Mike Pennisi12ffe0e9d330
diff --git a/lib/runnable.js b/lib/runnable.js
index 5b9ed231..49f52c7d 100644
--- a/lib/runnable.js
+++ b/lib/runnable.js
@@ -351,7 +351,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);
}
@@ -362,6 +362,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();
});
}
diff --git a/lib/utils.js b/lib/utils.js
index 1205587e..dfe591c1 100644
--- a/lib/utils.js
+++ b/lib/utils.js
@@ -749,3 +749,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';
+};
diff --git a/test/acceptance/overspecified-async.js b/test/acceptance/overspecified-async.js
new file mode 100644
index 00000000..28449203
--- /dev/null
+++ b/test/acceptance/overspecified-async.js
@@ -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() {} };
+ });
+});
diff --git a/test/utils.js b/test/utils.js
index c169f486..501ea7ab 100644
--- a/test/utils.js
+++ b/test/utils.js
@@ -183,4 +183,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;
+ });
+ });
});