簡體   English   中英

僅在先前的異步測試通過后,如何運行Mocha測試?

[英]How do I run a mocha test only after the prior asynchronous test has passed?

使用mocha javascript測試框架,我希望能夠僅在先前定義的測試通過后才能執行多個測試(全部異步)。

我不想將這些測試相互嵌套。

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

使用--bail選項。 確保您至少使用了摩卡咖啡0.14.0。 (我已經在較舊的版本中嘗試過,但是沒有成功。)

首先,僅在上一個測試完成后,您無需做任何事情就可以進行摩卡測試。 默認情況下,這就是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失敗后,摩卡咖啡將立即停止。

如果您的測試設置正確,僅測試一小部分業務邏輯,則可以異步運行測試,但它們不應阻止其他測試。 完成測試的方法是執行以下操作:

describe("BBController", function() {
    it("should save", function(done) {
       // handle logic
       // handle assertion or other test
       done(); //let mocha know test is complete - results are added to test list
    });
    it("should delete", function(done) {
       // handle logic
       // handle assertion or other test
       done(); //let mocha know test is complete - results are added to test list
    });
});

再次,沒有測試應該等待另一個測試通過,如果您遇到此問題,則應考慮改善依賴注入或使用before方法准備測試的方法

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM