gpt4 book ai didi

node.js - Mocha 与 done() 回调并行运行异步测试

转载 作者:行者123 更新时间:2023-12-02 21:15:19 25 4
gpt4 key购买 nike

我正在使用 Mocha 运行一些异步测试,但在之前的测试完成之前, future 的一些测试无法执行。为此,我可以简单地使用 done() 回调来同步运行它们:

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();
});
});
});

这样做会减慢整个操作的速度,因为我必须同步运行每个 it() block 。我想异步运行内部测试,并且每个描述 block 同步,但是 done() 回调似乎不能以这种方式工作(至少,使用 async)。

我做错了什么,还是根本不支持?如果没有,有什么办法可以解决这个问题来实现我的目标吗?

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似乎是为了满足这种需求而创建的。以下是项目网站的描述:

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.

我相信这将并行运行所有测试。要限制描述 block 按顺序运行,您可以将它们放入单独的文件中,然后在这些文件上单独运行 mocha-parallel-tests

关于node.js - Mocha 与 done() 回调并行运行异步测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31126317/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com