摩卡开始平行描述

我希望能够把所有在摩卡的描述语句都平行推开。 有人可以帮我弄清楚如何做到这一点?

你不能直接用摩卡做这件事,因为它创build了一个()callback列表并按顺序调用它们。 摩卡平行testing可以做到这一点,如果你愿意将你的描述移动到单独的.js文件。 为了说服你自己,把它安装在某个地方,并用一个简短的句柄调用它,这样每次都会报告:

 laptop:/tmp/delme$ npm install mocha-parallel-tests laptop:/tmp/delme$ cd node_modules/mocha-parallel-tests laptop:/tmp/delme/node_modules/mocha-parallel-tests$ ./bin/mocha-parallel-tests test/parallel/tests --timeout 10000 --slow 100 

你会看到它运行了三个(非常简单)的testing套件,花费的时间最长。

如果你的testing不依赖于早期testing的副作用,你可以使它们全都是asynchronous的。 一个简单的方法是在描述之前花费一些时间来启动这些东西,并使用常规的摩卡装置来评估它。 在这里,我创build了一系列的promise,需要一段时间才能解决,然后再次遍历testing,在.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); }); } 

摩卡并不支持您正在尝试开箱即用的function。 它依次运行testing。 这在处理未处理的exception时有很大的优势:Mocha可以确定它在testing中发生了,它正在运行。 所以把这个例外归因于当前的testing。 支持并行testing当然是可能的,但是这会使摩卡变得相当复杂。

我倾向于赞同大卫的评论 。 我不会这样做。 在摩卡通常运行的层面上,并行性在我看来并不是特别理想。 我以前使用过testing并行性的地方是运行端到端套件的级别。 例如,在Windows 8.1中运行一个针对Firefox的套件,同时在Linux中运行与Chrome相同的套件。