☕️ Classic, reliable, trusted test framework for Node.js and the browser
Allow skip from test context for #332
Chris Buckley718c411a2cae
diff --git a/lib/context.js b/lib/context.jsindex c983b6eb..3885218d 100644--- a/lib/context.js+++ b/lib/context.js@@ -67,6 +67,18 @@ Context.prototype.slow = function(ms){return this;};+/**+ * Mark a test as skipped.+ *+ * @return {Context} self+ * @api private+ */++Context.prototype.skip = function(){+ this.runnable().skip();+ return this;+};+/*** Inspect the context void of `._runnable`.*diff --git a/lib/pending.js b/lib/pending.jsnew file mode 100644index 00000000..265ec73c--- /dev/null+++ b/lib/pending.js@@ -0,0 +1,16 @@++/**+ * Expose `Pending`.+ */++module.exports = Pending;++/**+ * Initialize a new `Pending` error with the given message.+ *+ * @param {String} message+ */++function Pending(message) {+ this.message = message;+}diff --git a/lib/runnable.js b/lib/runnable.jsindex 9b216abb..7ce2f5cf 100644--- a/lib/runnable.js+++ b/lib/runnable.js@@ -4,6 +4,7 @@var EventEmitter = require('events').EventEmitter, debug = require('debug')('mocha:runnable')+ , Pending = require('./pending'), milliseconds = require('./ms'), utils = require('./utils');@@ -104,6 +105,16 @@ Runnable.prototype.enableTimeouts = function(enabled){return this;};+/**+ * Halt and mark as pending.+ *+ * @api private+ */++Runnable.prototype.skip = function(){+ throw new Pending();+};+/*** Return the full title generated by recursively* concatenating the parent's full title.diff --git a/lib/runner.js b/lib/runner.jsindex 0c9cc551..1c006ca2 100644--- a/lib/runner.js+++ b/lib/runner.js@@ -4,6 +4,7 @@var EventEmitter = require('events').EventEmitter, debug = require('debug')('mocha:runner')+ , Pending = require('./pending'), Test = require('./test'), utils = require('./utils'), filter = utils.filter@@ -261,10 +262,14 @@ Runner.prototype.hook = function(name, fn){var testError = hook.error();if (testError) self.fail(self.test, testError);if (err) {- self.failHook(hook, err);+ if (err instanceof Pending) {+ suite.pending = true;+ } else {+ self.failHook(hook, err);- // stop executing hooks, notify callee of hook err- return fn(err);+ // stop executing hooks, notify callee of hook err+ return fn(err);+ }}self.emit('hook end', hook);delete hook.ctx.currentTest;@@ -446,6 +451,11 @@ Runner.prototype.runTests = function(suite, fn){self.emit('test', self.test = test);self.hookDown('beforeEach', function(err, errSuite){+ if (suite.pending) {+ self.emit('pending', test);+ self.emit('test end', test);+ return next();+ }if (err) return hookErr(err, errSuite, false);self.currentRunnable = self.test;@@ -453,8 +463,17 @@ Runner.prototype.runTests = function(suite, fn){test = self.test;if (err) {- self.fail(test, err);+ if (err instanceof Pending) {+ self.emit('pending', test);+ } else {+ self.fail(test, err);+ }self.emit('test end', test);++ if (err instanceof Pending) {+ return next();+ }+return self.hookUp('afterEach', next);}diff --git a/test/acceptance/pending.js b/test/acceptance/pending.jsindex 4ef96370..b40b3c03 100644--- a/test/acceptance/pending.js+++ b/test/acceptance/pending.js@@ -1,3 +1,41 @@describe('pending', function(){it('should be allowed')})++describe('skip in test', function(){+ it('should skip immediately', function(){+ this.skip();+ throw new Error('never thrown');+ })++ it('should run other tests in the suite', function(){+ })+})++describe('skip in before', function(){+ before(function(){+ this.skip();+ })++ it('should never run this test', function(){+ throw new Error('never thrown');+ })++ it('should never run this test', function(){+ throw new Error('never thrown');+ })+})++describe('skip in beforeEach', function(){+ beforeEach(function(){+ this.skip();+ })++ it('should never run this test', function(){+ throw new Error('never thrown');+ })++ it('should never run this test', function(){+ throw new Error('never thrown');+ })+})