简体   繁体   中英

Mocha run async tests in parallel with done() callback

I'm running some asynchronous tests with Mocha, but some future tests can't be executed until previous ones are completed. For this, I can simply use done() callback to run them synchronously:

describe('first operations', function() {
    it('should do something', function(done) {
        longOperation(function(err) {
            done();
        });
    });
    it('should do something', function(done) {
        longOperation(function(err) {
            done();
        });
    });
});

describe('second operations', function() {
    it('should do something', function(done) {
        longOperation(function(err) {
            done();
        });
    });
    it('should do something', function(done) {
        longOperation(function(err) {
            done();
        });
    });
});

Doing so though, slows down the entire operation because I'm stuck running each it() block synchronously. I'd like to run the inner tests asynchronously, and each describe block synchronously, but the done() callback doesn't seem to work that way (at least, using async ).

Am I doing something wrong, or is that simply not supported? If not, is there a way I can get around this to accomplish my goal?

describe('first operations', function(done) {
    async.parallel([
        function(callback) {
            it('should do something', function() {
                longOperation(function(err) {
                    callback();
                });
            });
        },
        function(callback) {
            it('should do something', function() {
                longOperation(function(err) {
                    callback();
                });
            });
        }
    ], function(err) {
        done();
    });
});

describe('second operations', function(done) {
    async.parallel([
        function(callback) {
            it('should do something', function() {
                longOperation(function(err) {
                    callback();
                });
            });
        },
        function(callback) {
            it('should do something', function() {
                longOperation(function(err) {
                    callback();
                });
            });
        }
    ], function(err) {
        done();
    });
});

mocha-parallel-tests seems to have been created to fill this need. Here's a description from the project site:

Normally tests written with mocha run sequentially. This happens so because each test suite should not depend on another. But if you are running tests which take a lot of time (for example tests with Selenium Webdriver) waiting for so much time is impossible.

I believe this will run all tests in parallel though. To limit the describe blocks to run sequentially you could put them in individual files and then run mocha-parallel-tests separately on these files.

context.describe() doesn't seem to be an instance of Runnable . context.it() , on the other hand seems to be creating an instance of Runnable . It looks like only instances of Runnable receive a callback .

So no, it looks like you cannot run describe() blocks serially while running enclosed it() blocks asynchronously.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM