您无法直接使用mocha进行此操作,因为它会创建it()回调的列表并按顺序调用它们。 如果您愿意将自己的描述移到单独的.js文件中,mocha-
parallel-tests可以执行此操作。为了说服自己,请将其安装在某个地方,然后用–low缓慢调用它,以便每次都报告:
laptop:/tmp/delme$ npm install mocha-parallel-testslaptop:/tmp/delme$ cd node_modules/mocha-parallel-testslaptop:/tmp/delme/node_modules/mocha-parallel-tests$ ./bin/mocha-parallel-tests test/parallel/tests --timeout 10000 --slow 100
您将看到它运行了最长的时间运行了三个(非常简单)的测试套件。
如果您的测试不依赖于早期测试的副作用,则可以使它们全部异步。一种简单的方法是在描述之前启动需要一段时间的东西,然后使用常规的摩卡咖啡设备对其进行评估。在这里,我创建了一堆承诺,需要一段时间才能解决,然后再次遍历测试,并在.then()函数中检查其结果:
var expect = require("chai").expect;var SlowTests = [ { name: "a" , time: 250 }, { name: "b" , time: 500 }, { name: "c" , time: 750 }, { name: "d" , time:1000 }, { name: "e" , time:1250 }, { name: "f" , time:1500 }];SlowTests.forEach(function (test) { test.promise = takeAWhile(test.time);});describe("SlowTests", function () { // mocha defaults to 2s timeout. change to 5s with: this.timeout(5000); SlowTests.forEach(function (test) { it("should pass '" + test.name + "' in around "+ test.time +" mseconds.", function (done) { test.promise.then(function (res) {expect(res).to.be.equal(test.time);done(); }).catch(function (err) {done(err); }); }); });});function takeAWhile (time) { return new Promise(function (resolve, reject) { setTimeout(function () { resolve(time); }, time); });}(将其另存为
foo.js并使用调用
mocha foo.js。)
meta
我不同意测试应该主要是同步的说法。编译前后比较容易,但是很少有一项测试会使所有其余测试无效。所有不鼓励异步测试的做法都是不鼓励对网络任务进行广泛的测试。



