同步运行mocha测试循环

时间:2014-12-02 17:33:06

标签: node.js mocha async.js

我在这样的数组上运行mocha测试循环:

it('each should have permissions defined for each role', function(done) {
    async.forEach(_permissions, function(permission, callback) {
        log.debug(permission.permissions.should.be.an.instanceOf(Array).and.not.be.empty);
        callback(permission.permissions.should.be.an.instanceOf(Array).and.not.be.empty);
    },
    function(result) {
        log.debug(result);
        done();
    });
});

当我将其注销时,即使我正在使用done()回调,它似乎也是异步运行的。是这样的吗?我的测试是否仍然成功执行,还是仅执行第一个测试然后返回成功的结果?

这是控制台输出:

[Tue Dec 02 2014 09:24:07 GMT-0800 (PST)] DEBUG { obj: [ 'permissions!' ], params: { operator: 'to be empty' }, negate: false }
    ✓ each should have permissions defined for each role // <- here's the test result
[Tue Dec 02 2014 09:24:07 GMT-0800 (PST)] DEBUG { obj: [ 'permissions!' ], params: { operator: 'to be empty' }, negate: false }
[Tue Dec 02 2014 09:24:07 GMT-0800 (PST)] DEBUG { obj: [ 'permissions!' ], params: { operator: 'to be empty' }, negate: false }
[Tue Dec 02 2014 09:24:07 GMT-0800 (PST)] DEBUG { obj: [ 'permissions!' ], params: { operator: 'to be empty' }, negate: false }

1 个答案:

答案 0 :(得分:1)

使用eachSeries,这也让我过去了。 forEach是并行版本。

像这样使用:

it('each should have permissions defined for each role', function(done) {
    async.eachSeries(_permissions, function(permission, callback) {
        permission.permissions.should.be.an.instanceOf(Array).and.not.be.empty;
        callback();
    },
    function(err) {
        done();
    });
});