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

Sets the allowUncaught parameter for hooks. #2303

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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 @@ -287,6 +287,8 @@ Runner.prototype.hook = function(name, fn) {

hook.ctx.currentTest = self.test;

hook.allowUncaught = self.allowUncaught;

self.emit('hook', hook);

if (!hook.listeners('error').length) {
Expand Down
24 changes: 24 additions & 0 deletions test/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,17 @@ describe('Runner', function(){
});

describe('allowUncaught', function() {
var immediately;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we monkeypatching Runner.immediately()?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was done in order to run the hook synchronously.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need to do that?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need it to catch the exception in the test method, instead of catching it on a global error handler.


before(function() {
immediately = Runner.immediately;
Runner.immediately = function(fn) { fn(); };
});

after(function() {
Runner.immediately = immediately;
});

it('should allow unhandled errors to propagate through', function(done) {
var newRunner = new Runner(suite);
newRunner.allowUncaught = true;
Expand All @@ -327,6 +338,19 @@ describe('Runner', function(){
fail.should.throw('allow unhandled errors');
done();
});

it('should allow unhandled errors in hooks to propagate through', function(done) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this doesn't appear to be async, so you don't need done.

Ideally, I'd like to see both sync and async tests against this behavior. Can you please add an async version?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I will correct soon and I will add the asynchronous version

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@boneskull I'm sorry for delay.

I can not think of a good way, how to check the exception handling asynchronously. Perhaps I not fully understand the problem. Please give me an advice on how to do it correctly.

var runner = new Runner(suite);
runner.allowUncaught = true;
suite.beforeEach(function(){
throw new Error('allow unhandled errors in hooks');
});
function fail() {
runner.hook('beforeEach', function() {});
}
fail.should.throw('allow unhandled errors in hooks');
done();
});
});

describe('stackTrace', function() {
Expand Down