Skip to content

Commit

Permalink
allow skip() in async test context; closes mochajs#2334
Browse files Browse the repository at this point in the history
  • Loading branch information
boneskull committed Jun 27, 2016
1 parent 0fe8133 commit dd74788
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/runnable.js
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,10 @@ Runnable.prototype.run = function(fn) {
return callFnAsync(this.fn);
}
try {
// allows skip() to be used in an explicit async context
this.skip = function () {
done(new Pending());
};
callFnAsync(this.fn);
} catch (err) {
done(utils.getError(err));
Expand Down
16 changes: 16 additions & 0 deletions test/integration/fixtures/pending/skip.async.before.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
describe('skip in before', function() {
before(function(done) {
var self = this;
setTimeout(function() {
self.skip();
}, 50);
});

it('should never run this test', function() {
throw new Error('never thrown');
});

it('should never run this test', function() {
throw new Error('never thrown');
});
});
16 changes: 16 additions & 0 deletions test/integration/fixtures/pending/skip.async.beforeEach.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
describe('skip in beforeEach', function() {
beforeEach(function(done) {
var self = this;
setTimeout(function() {
self.skip();
}, 50);
});

it('should never run this test', function() {
throw new Error('never thrown');
});

it('should never run this test', function() {
throw new Error('never thrown');
});
});
12 changes: 12 additions & 0 deletions test/integration/fixtures/pending/skip.async.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
describe('skip in test', function() {
it('should skip async', function(done) {
var self = this;
setTimeout(function() {
self.skip();
}, 50);
});

it('should run other tests in the suite', function() {
// Do nothing
});
});
43 changes: 43 additions & 0 deletions test/integration/pending.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,47 @@ describe('pending', function() {
});
});
});

describe('asynchronous skip()', function() {
this.timeout(1000);

describe('in spec', function() {
it('should immediately skip the spec and run all others', function(done) {
run('pending/skip.async.spec.js', args, function(err, res) {
assert(!err);
assert.equal(res.stats.pending, 1);
assert.equal(res.stats.passes, 1);
assert.equal(res.stats.failures, 0);
assert.equal(res.code, 0);
done();
});
});
});

describe('in before', function() {
it('should skip all suite specs', function(done) {
run('pending/skip.async.before.js', args, function(err, res) {
assert(!err);
assert.equal(res.stats.pending, 2);
assert.equal(res.stats.passes, 0);
assert.equal(res.stats.failures, 0);
assert.equal(res.code, 0);
done();
});
});
});

describe('in beforeEach', function() {
it('should skip all suite specs', function(done) {
run('pending/skip.sync.beforeEach.js', args, function(err, res) {
assert(!err);
assert.equal(res.stats.pending, 2);
assert.equal(res.stats.passes, 0);
assert.equal(res.stats.failures, 0);
assert.equal(res.code, 0);
done();
});
});
});
});
});

0 comments on commit dd74788

Please sign in to comment.