Skip to content

armgit / mocha

Public Imported from GitHub
0

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

add --async-only support

Aaron Heckmann11808af10897
diff --git a/Makefile b/Makefile
index 12ec21db..a35499c8 100644
--- a/Makefile
+++ b/Makefile
@@ -91,6 +91,12 @@ test-bail:
--bail \
test/acceptance/misc/bail
+test-async-only:
+ @./bin/mocha \
+ --reporter $(REPORTER) \
+ --async-only \
+ test/acceptance/misc/asyncOnly
+
non-tty:
@./bin/mocha \
--reporter dot \
diff --git a/bin/_mocha b/bin/_mocha
index bffb88af..446fca5d 100755
--- a/bin/_mocha
+++ b/bin/_mocha
@@ -70,6 +70,7 @@ program
.option('-G, --growl', 'enable growl notification support')
.option('-d, --debug', "enable node's debugger, synonym for node --debug")
.option('-b, --bail', "bail after first test failure")
+ .option('-A, --async-only', "force all tests to take a callback (async)")
.option('--recursive', 'include sub directories')
.option('--debug-brk', "enable node's debugger breaking on the first line")
.option('--globals <names>', 'allow the given comma-delimited global [names]', list, [])
@@ -232,6 +233,10 @@ if (program.ignoreLeaks) mocha.ignoreLeaks();
if (program.growl) mocha.growl();
+// --async-only
+
+if (program.asyncOnly) mocha.asyncOnly();
+
// --globals
mocha.globals(globals);
diff --git a/lib/mocha.js b/lib/mocha.js
index 1e28d09e..4604f6cd 100644
--- a/lib/mocha.js
+++ b/lib/mocha.js
@@ -266,6 +266,18 @@ Mocha.prototype.slow = function(slow){
return this;
};
+/**
+ * Makes all tests async (accepting a callback)
+ *
+ * @return {Mocha}
+ * @api public
+ */
+
+Mocha.prototype.asyncOnly = function(){
+ this.options.asyncOnly = true;
+ return this;
+};
+
/**
* Run tests and invoke `fn()` when complete.
*
@@ -281,6 +293,7 @@ Mocha.prototype.run = function(fn){
var runner = new exports.Runner(suite);
var reporter = new this._reporter(runner);
runner.ignoreLeaks = options.ignoreLeaks;
+ runner.asyncOnly = options.asyncOnly;
if (options.grep) runner.grep(options.grep, options.invert);
if (options.globals) runner.globals(options.globals);
if (options.growl) this._growl(runner, reporter);
diff --git a/lib/runnable.js b/lib/runnable.js
index 087f94e0..9c4c6f6a 100644
--- a/lib/runnable.js
+++ b/lib/runnable.js
@@ -186,6 +186,11 @@ Runnable.prototype.run = function(fn){
// async
if (this.async) {
+
+ if (this.asyncOnly && !(this.fn && this.fn.length)) {
+ return done(new Error('asyncOnly option in use without declaring `done()`'));
+ }
+
try {
this.fn.call(ctx, function(err){
if (err instanceof Error) return done(err);
@@ -197,7 +202,7 @@ Runnable.prototype.run = function(fn){
}
return;
}
-
+
// sync
try {
if (!this.pending) this.fn.call(ctx);
diff --git a/lib/runner.js b/lib/runner.js
index fdb2565e..20994433 100644
--- a/lib/runner.js
+++ b/lib/runner.js
@@ -332,6 +332,9 @@ Runner.prototype.runTest = function(fn){
var test = this.test
, self = this;
+ if (this.asyncOnly)
+ test.async = test.asyncOnly = true;
+
try {
test.on('error', function(err){
self.fail(test, err);
diff --git a/test/acceptance/misc/asyncOnly.js b/test/acceptance/misc/asyncOnly.js
new file mode 100644
index 00000000..94f7f6de
--- /dev/null
+++ b/test/acceptance/misc/asyncOnly.js
@@ -0,0 +1,9 @@
+
+describe('asyncOnly', function(){
+ it('should display an error', function(){
+
+ })
+ it('should pass', function(done){
+ done();
+ })
+})