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

Asynchronous error handling #1769

Closed
ajaks328 opened this issue Jun 29, 2015 · 6 comments
Closed

Asynchronous error handling #1769

ajaks328 opened this issue Jun 29, 2015 · 6 comments
Labels
type: bug a defect, confirmed by a maintainer

Comments

@ajaks328
Copy link
Contributor

This issue seems like a missing piece of this fixed issue - #1043

The issue and fix referenced in the above link is working as expected when using the hooks in sync mode. When I attempt to use the hooks in async mode (with done callback), sibling suites do not get executed.

If I have two sibling suites with async beforeEach, afterEach and tests - an async error thrown in the afterEach of the first suite will fail all tests in that suite (as expected) and also will stop execution of the second suite which does not seem expected.

Sample Code:

describe('Multiple Suites Test', function() {

describe('Suite 1', function() {

    beforeEach(function(done){
        console.log('Suite1:beforeEach');
        done();
    });

    afterEach(function(){
        console.log('Suite1:afterEach');

// Code to throw Error in async mode
// setTimeout(function(){
// throw new Error('Error in afterEach of Suite1!!');
// },1000);
throw new Error('Error in afterEach of Suite1!!');
});

    it('Suite1-Test1', function(done) {
        console.log('Suite1:Test1');
        done();
    });

    it('Suite1-Test2', function(done) {
        console.log('Suite1:Test2');
        done();
    });

});

describe('Suite 2', function() {

    beforeEach(function(done){
        console.log('Suite2:beforeEach');
        done();
    });

    afterEach(function(done){
        console.log('Suite2:afterEach');
        done();
    });

    it('Suite2-Test1', function(done) {
        console.log('Suite2:Test1');
        done();
    });

    it('Suite2-Test2', function(done) {
        console.log('Suite2:Test2');
        done();
    });

});

});

When the above file is executed you see the below output as expected:
Multiple Suites Test
Suite 1
Suite1:beforeEach
Suite1:Test1
✓ Suite1-Test1
Suite1:afterEach
1) "after each" hook
Suite 2
Suite2:beforeEach
Suite2:Test1
✓ Suite2-Test1
Suite2:afterEach
Suite2:beforeEach
Suite2:Test2
✓ Suite2-Test2
Suite2:afterEach

3 passing (14ms)
1 failing

  1. Multiple Suites Test Suite 1 "after each" hook:
    Error: Error in afterEach of Suite1!!
    at Context. (simpleTests.js:12:19)

If the same code is run with commented code enabled and the first afterEach in async mode, the output looks like below,

Multiple Suites Test
Suite 1
Suite1:beforeEach
Suite1:Test1
✓ Suite1-Test1
Suite1:afterEach
1) "after each" hook

1 passing (1s)
1 failing

  1. Multiple Suites Test Suite 1 "after each" hook:
    Uncaught Error: Error in afterEach of Suite1!!
    at null._onTimeout (simpleTests.js:14:23)

The second suite is completely ignored.

@boneskull
Copy link
Contributor

this sounds like it needs fixing.

@boneskull boneskull added status: accepting prs Mocha can use your help with this one! type: bug a defect, confirmed by a maintainer labels Jul 5, 2015
@ajaks328
Copy link
Contributor Author

@boneskull , could you enable write access for me on this project so I can make push a branch and make a PR for this issue?

@boneskull
Copy link
Contributor

@ajaykodali that's not how it works. 😄 You fork, then branch, then create a PR from your fork into mocha.

@ajaks328
Copy link
Contributor Author

@boneskull , ah :) cool. Will do that. Sorry about the confusion.

@ajaks328
Copy link
Contributor Author

@boneskull done. Here is the PR for this issue - #1802. :)

@danielstjules
Copy link
Contributor

Closed via #1802

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug a defect, confirmed by a maintainer
Projects
None yet
Development

No branches or pull requests

3 participants