Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow skip() in async test context; closes #2334 #2335

Merged
merged 2 commits into from
Jun 28, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
});
});
});
});
});