Skip to content

Commit

Permalink
Allow skip from test context for #332
Browse files Browse the repository at this point in the history
  • Loading branch information
cmbuckley committed Feb 3, 2015
1 parent 1159795 commit 718c411
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 4 deletions.
12 changes: 12 additions & 0 deletions lib/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
*
Expand Down
16 changes: 16 additions & 0 deletions lib/pending.js
Original file line number Diff line number Diff line change
@@ -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;
}
11 changes: 11 additions & 0 deletions lib/runnable.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

var EventEmitter = require('events').EventEmitter
, debug = require('debug')('mocha:runnable')
, Pending = require('./pending')
, milliseconds = require('./ms')
, utils = require('./utils');

Expand Down Expand Up @@ -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.
Expand Down
27 changes: 23 additions & 4 deletions lib/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -446,15 +451,29 @@ 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;
self.runTest(function(err){
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);
}

Expand Down
38 changes: 38 additions & 0 deletions test/acceptance/pending.js
Original file line number Diff line number Diff line change
@@ -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');
})
})

0 comments on commit 718c411

Please sign in to comment.