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

Fix 1604: Add support for async skip() #1618

Closed
wants to merge 1 commit into from
Closed
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
29 changes: 23 additions & 6 deletions lib/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,15 @@ Runner.prototype.runSuite = function(suite, fn) {
* @api private
*/
Runner.prototype.uncaught = function(err) {
if (err) {
var runnable = this.currentRunnable;
var pending;

if (err instanceof Pending) {
if (runnable.type === 'test') {
runnable.pending = true;
}
pending = true;
} else if (err) {
debug('uncaught exception %s', err !== function() {
return this;
}.call(err) ? err : (err.message || err));
Expand All @@ -669,8 +677,6 @@ Runner.prototype.uncaught = function(err) {
}
err.uncaught = true;

var runnable = this.currentRunnable;

if (!runnable) {
runnable = new Runnable('Uncaught error outside test suite');
runnable.parent = this.suite;
Expand All @@ -693,7 +699,16 @@ Runner.prototype.uncaught = function(err) {
if (runnable.state) {
return;
}
this.fail(runnable, err);

if (pending && runnable.type === 'test') {
// Async skip() call from test
this.emit('pending', runnable);
} else if (pending) {
// Async skip() call from hook
runnable.callback(err);
} else {
this.fail(runnable, err);
}

// recover from test
if (runnable.type === 'test') {
Expand All @@ -717,8 +732,10 @@ Runner.prototype.uncaught = function(err) {
return this.nextSuite(errSuite);
}

// bail
this.emit('end');
// bail on hooks if not skipped
if (!pending) {
this.emit('end');
}
};

/**
Expand Down
17 changes: 17 additions & 0 deletions test/integration/fixtures/pending/skip.async.before.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
describe('skip in asynchronous before', function() {
before(function(done){
var hook = this;
setTimeout(function() {
hook.skip();
throw new Error('never thrown');
}, 0);
});

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

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

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

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

it('should run other tests in the suite', function() {});
});
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.async.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();
});
});
});
});
});