揭开序幕摩卡并行描述 [英] Kicking off mocha describes in parallel

查看:69
本文介绍了揭开序幕摩卡并行描述的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够同时启动我在Mocha中的所有describe语句.有人可以帮我弄清楚该怎么做吗?

I want to be able to have all my describe statements in Mocha get kicked off in parallel. Can someone help me figure out how to do that?

推荐答案

您不能直接使用mocha进行此操作,因为它会创建it()回调的列表并按顺序调用它们. 如果您愿意移动自己的描述, mocha-parallel-tests 可以执行此操作分成单独的.js文件.为了说服自己,请将其安装在某个地方,然后用一小段--slow调用它,以便每次生成报告:

You can't do this directly with mocha because it creates a list of it() callbacks and invokes them in order. mocha-parallel-tests can do this if you're willing to move your describes into separate .js files. To convince yourself, install it somewhere and invoke it with a short --slow so it reports each time:

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

您会看到它运行了最长的时间才运行了三个(非常简单)的测试套件.

You will see that it ran three (very simple) tests suites in the time it took to run the longest.

如果您的测试不依赖于早期测试的副作用,则可以使它们全部异步. 一种简单的方法是在描述之前启动需要花费一段时间的东西,并使用常规的摩卡咖啡设备对其进行评估.在这里,我创建了一堆承诺,需要一段时间才能解决,然后再次遍历测试,并在.then()函数中检查其结果:

If your tests don't depend on side-effects of earlier tests, you can make them all asynchronous. A simple way to do this is to initiate the stuff that takes a while before the describe and use the regular mocha apparatus to evaluate it. Here, I create a bunch of promises which take a while to resolve and then iterate through the tests again, examining their results in a .then() function:

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调用.)

(Save this as foo.js and invoke with mocha foo.js.)

我不同意测试应该主要是同步的说法.编译前和编译后比较容易,但是很少有一项测试会使所有其余测试无效.不鼓励异步测试的所有措施就是不鼓励对网络任务进行广泛的测试.

Meta I disagree with the assertion that tests should be primarily be synchronous. before and after pragmas are easier but it's rare that one test invalidates all remaining tests. All discouraging asynchronous tests does is discourage extensive testing of network tasks.

这篇关于揭开序幕摩卡并行描述的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆