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

set allowUncaught on hooks #3669

Merged
merged 3 commits into from
Mar 6, 2019
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
2 changes: 2 additions & 0 deletions lib/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,8 @@ Runner.prototype.hook = function(name, fn) {
hook.ctx.currentTest = self.test;
}

hook.allowUncaught = self.allowUncaught;

self.emit(constants.EVENT_HOOK_BEGIN, hook);

if (!hook.listeners('error').length) {
Expand Down
73 changes: 71 additions & 2 deletions test/unit/runner.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ var Test = Mocha.Test;
var Runnable = Mocha.Runnable;
var Hook = Mocha.Hook;
var noop = Mocha.utils.noop;
var EVENT_HOOK_BEGIN = Runner.constants.EVENT_HOOK_BEGIN;
var EVENT_TEST_FAIL = Runner.constants.EVENT_TEST_FAIL;
var EVENT_TEST_RETRY = Runner.constants.EVENT_TEST_RETRY;
var EVENT_RUN_END = Runner.constants.EVENT_RUN_END;
Expand Down Expand Up @@ -443,7 +444,7 @@ describe('Runner', function() {
});

describe('allowUncaught', function() {
it('should allow unhandled errors to propagate through', function(done) {
it('should allow unhandled errors to propagate through', function() {
var newRunner = new Runner(suite);
newRunner.allowUncaught = true;
newRunner.test = new Test('failing test', function() {
Expand All @@ -453,7 +454,75 @@ describe('Runner', function() {
newRunner.runTest();
}
expect(fail, 'to throw', 'allow unhandled errors');
done();
});

it('should not allow unhandled errors in sync hooks to propagate through', function(done) {
suite.beforeEach(function() {
throw new Error();
});
var runner = new Runner(suite);
runner.allowUncaught = false;

// We are monkey patching here with runner.once and a hook.run wrapper to effectively
// capture thrown errors within the event loop phase where Runner.immediately executes
runner.once(EVENT_HOOK_BEGIN, function(hook) {
var _run = hook.run;
hook.run = function(fn) {
function throwError() {
_run.call(hook, fn);
}
expect(throwError, 'not to throw');
done();
};
});

runner.hook('beforeEach', noop);
});
juergba marked this conversation as resolved.
Show resolved Hide resolved

it('should allow unhandled errors in sync hooks to propagate through', function(done) {
suite.beforeEach(function() {
throw new Error('allow unhandled errors in sync hooks');
});
var runner = new Runner(suite);
runner.allowUncaught = true;

runner.once(EVENT_HOOK_BEGIN, function(hook) {
var _run = hook.run;
hook.run = function(fn) {
function throwError() {
_run.call(hook, fn);
}
var expected = 'allow unhandled errors in sync hooks';
expect(throwError, 'to throw', expected);
done();
};
});

runner.hook('beforeEach', noop);
});

it('async - should allow unhandled errors in hooks to propagate through', function(done) {
// the `done` argument, although unused, it triggers the async path
// see this.async in the Runnable constructor
suite.beforeEach(function(done) {
throw new Error('allow unhandled errors in async hooks');
});
var runner = new Runner(suite);
runner.allowUncaught = true;

runner.once(EVENT_HOOK_BEGIN, function(hook) {
var _run = hook.run;
hook.run = function(fn) {
function throwError() {
_run.call(hook, fn);
}
var expected = 'allow unhandled errors in async hooks';
expect(throwError, 'to throw', expected);
done();
};
});

runner.hook('beforeEach', noop);
});
});

Expand Down