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

Tests stop after uncaught first exception. #172

Closed
tosbaha opened this issue Jun 24, 2018 · 8 comments
Closed

Tests stop after uncaught first exception. #172

tosbaha opened this issue Jun 24, 2018 · 8 comments

Comments

@tosbaha
Copy link

tosbaha commented Jun 24, 2018

I have multiple test files. I want to run them in parallel programmatically. If one of the test files fail with Uncaught exception, testing stops. I checked the source code and saw that you are using process.exit after Uncaught exception I tried to set bail:false but it didn't change anything.

TLDR; I want to run all test files in parallel and get a report at the end even if one the tests are failed/crashed.

@1999
Copy link
Collaborator

1999 commented Jun 24, 2018

@tosbaha thank you for posting the issue. That sounds weird, I will take a look soon.

@1999
Copy link
Collaborator

1999 commented Jun 25, 2018

The newly published mocha-parallel-tests=2.0.3 has the fix.

@tosbaha
Copy link
Author

tosbaha commented Jun 25, 2018

Thanks for such a fast fix. I really appreciate it. It works for my purposes but, it doesn't work for following scenario. Put following code const abcd = require('thisdoesntexist'); in one of the test, and again Uncaught exception kills the process.

@1999
Copy link
Collaborator

1999 commented Jun 25, 2018

@tosbaha not a problem. Unfortunately I can't reproduce your issue: in my case I can see that the test finishes with an "end" event:

describe('suite', function () {
    it('throw exception immediately', function () {
        throw new Error('foo');
    });

    // eslint-disable-next-line no-unused-vars
    it('throw exception in setTimeout', function (done) {
        setTimeout(() => {
            throw new Error('foo');
        }, 100);
    });

    it('should pass', () => {
        // eslint-disable-next-line no-unused-vars
        const a = 2;
    });

    it('throw exception in promise', () => {
        return new Promise(() => {
            throw new Error('foo');
        });
    });

    it('require unknown package', () => {
        require('packagedoesnotexist');
    });

    it('should pass', () => {
        // eslint-disable-next-line no-unused-vars
        const a = 2;
    });
});

image

Can you create a gist/repo so that I could reproduce it?

@tosbaha
Copy link
Author

tosbaha commented Jun 25, 2018

For me it is giving me an error. Here is my test setup.

1.test.js

const abcd = require('thisdoesntexist');

describe('First Test:', function() {
    it('should crash', function (done) {
        done();
    });

});

2.test.js

describe('Second Test', function() {

    it('case 2', function (done) {
        setTimeout(() => {
           throw new Error('foo');
            }, 100);
        });
});

3.test.js

describe('Third Test:', function() {

    it('should work', function (done) {
        console.log('Third Test OK');
        done();
    });

});

4.test.js

describe('Fourth Test:', function() {

    it('should work too', function (done) {
        console.log('Fourth Test OK');
        done();
    });

});

Testing Code

const  Mocha = require('mocha-parallel-tests').default;
const glob = require('glob-promise');

const mocha = new Mocha({
    bail:false,
    reporter: 'mochawesome',
    reporterOptions: {
        reportTitle: 'My Tests',
        reportPageTitle: 'Health Check',
        json: true,
        html: true,
        code: false,
        //    quiet: true
    }
});

class HealthCheck {

    async getMocha() {
        const specs = await glob('./testfolder/*.js', {absolute: true});
        console.log(specs);
        specs.map(spec => {
            mocha.addFile(spec);
        });
        mocha.suite.timeout(60000);
        return mocha;
    }

    async checkIt() {

        return new Promise(async (resolve) => {
            const mocha = await this.getMocha();
            const runner = await mocha.run();

            let testError = null;
            runner.on('end', () => {
                console.log('Final end!');

                if (testError) console.error(testError.err);
                resolve();
            });

        });

    }
}

(async() => {
    try {
        const health = new HealthCheck();
        const result = await health.checkIt();
        console.log('Result: ' + result);
    } catch (e) {
        console.log('Error: ' + e);
    }
})();

PS: I tested with or without mochawesome. It always crashes and stop processing other test files.

@1999
Copy link
Collaborator

1999 commented Jun 25, 2018

This is pretty expected: if you run your code with mocha it will throw exception here as well. At least that happens to me on mocha@5. So mocha-parallel-tests does nothing new here 🤷‍♂️

image

Neat pick: in your code const runner = await mocha.run() await is not needed: mocha.run() immediately returns a runner object.

@tosbaha
Copy link
Author

tosbaha commented Jun 25, 2018

Thanks again for the feedback. I thought maybe there was a way to catch all kind of errors. Domain maybe? PS: Thanks for your comment about for mocha.run() Keep up the good work. 👍

@1999
Copy link
Collaborator

1999 commented Jun 26, 2018

Cheers, mate 😎
Domains are deprecated, so not sure. Ideally these kind of errors are fatal which means that ideally you need to fix them asap. Otherwise you'll have a feeling like "of my tests is failing so I need to fix it". There's also mocha's --allow-uncaught option, I actually didn't play with it so maybe it can help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants