簡體   English   中英

如何在“之前”鈎子中獲取Mocha測試名稱?

[英]How to get Mocha test name in the “before” hook?

我試圖before鈎子中獲取當前的describe名稱,如下所示:

describe('increasing 3 times', function() {

  before(function() {
    console.log('test name');
  });

  ...

});

我基本上想要檢索前鈎子中'增加3倍'的字符串。

如何實現這一目標?

謝謝!

這是代碼,說明了如何做到這一點:

describe("top", function () {
    before(function () {
        console.log("full title:", this.test.fullTitle());
        console.log("parent title:", this.test.parent.title);
    });

    it("test 1", function () {});
});

使用spec報告器運行,這將輸出:

full title: top "before all" hook
parent title: top
    ✓ test 1 


  1 passing (4ms)

當摩卡打電話給你傳遞的功能,它的各種功能( describebeforeit等)的值, this是一個Context對象。 該對象的一個​​字段名為test 這有點用詞不當,因為它可以指向別的東西,而不是實際的測試。 在類似於before的鈎子的情況下,它指向為before調用創建的當前Hook對象。 在此對象上調用fullTitle()將獲得對象的層次結構名稱:對象自己的名稱前面是包含它的測試套件( describe )的名稱。 Hook對象還有一個parent字段,指向包含鈎子的套件。 套件有一個title字段,這是第一個被傳遞來describe參數。

暫無
暫無

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

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