javascript - 如何仅在先前的异步测试通过后才运行 mocha 测试?

标签 javascript testing mocha.js

使用 mocha javascript 测试框架,我希望能够让多个测试(全部异步)仅在先前定义的测试通过后执行。

我不想将这些测试相互嵌套。

describe("BBController", function() {
    it("should save", function(done) {});
    it("should delete", function(done) {});
})

最佳答案

使用 --bail 选项。确保你至少使用 mocha 0.14.0。 (我用旧版本尝试过但没有成功。)

首先,mocha 无需执行任何操作即可在前一个测试完成后运行测试。这就是 mocha 默认的工作方式。将其保存到 test.js:

describe("test", function () {
    this.timeout(5 * 1000); // Tests time out in 5 seconds.

    it("first", function (done) {
        console.log("first: do nothing");
        done();
    });

    it("second", function (done) {
        console.log("second is executing");
        // This test will take 2.5 seconds.
        setTimeout(function () {
            done();
        }, 2.5 * 1000);
    });

    it("third", function (done) {
        console.log("third is executing");
        // This test will time out.
    });

    it("fourth", function (done) {
        console.log("fourth: do nothing");
        done();
    });
});

然后执行:

mocha -R spec test.js

您将不会看到第四个测试开始,直到:

  1. 第一次和第二次测试完成。
  2. 第三次测试已超时。

现在,运行它:

mocha -R spec --bail test.js

一旦测试 3 失败,Mocha 将停止。

关于javascript - 如何仅在先前的异步测试通过后才运行 mocha 测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20244773/

相关文章:

javascript - 折叠不适用于 Bootstrap 的移动导航栏

javascript - Jest 返回一个表未找到错误与 sqlite

reactjs - 如何在 React-Redux 中使用 Mocha、Chai 和 Enzyme 测试方法和回调

node.js - 在运行测试之前, Mocha 似乎不会等待 promise 链完成

javascript - 数据表 - rows().data().each() 函数不起作用

javascript - CSS 图像换行问题 - 取决于浏览器缩放级别

javascript - knockout JS : Template binding Issue in IE8

testing - 如何测试使用 'redux-api-middleware' 和 [CALL_API] 的 redux 操作?

c++ - 发送到应用程序的 CTRL-C 的单元测试

node.js - MongoClient.connect不执行回调函数