30

追加のログを記録するには、現在のテストの説明を出力できる必要があります。

これを行うにはどうすればよいですか (Mocha BDD を使用)。

4

5 に答える 5

50

へのコールバック内に直接いる場合は、 のタイトルにまたはをdescribe使用して、 の階層的なタイトル(祖先のタイトル + このタイトル) を取得できます。へのコールバック内にいる場合は、それぞれまたはを使用できます。そう:this.titledescribethis.fullTitle()describeitthis.test.titlethis.test.fullTitle()

describe("top", function() {
    console.log(this.title);
    console.log(this.fullTitle());

    it("test", function () {
        console.log(this.test.title);
        console.log(this.test.fullTitle());
    });
});

上記のconsole.logステートメントは次を出力します。

top
top
test
top test

ネスティングに応じてタイトルがどのように変化するかを示す完全な例を次に示します。

function dump () {
    console.log("running: (fullTitle)", this.test.fullTitle(), "(title)",
                this.test.title);
}

function directDump() {
    console.log("running (direct): (fullTitle)", this.fullTitle(), "(title)",
                this.title);
}

describe("top", function () {
    directDump.call(this);
    it("test 1", dump);
    it("test 2", dump);
    describe("level 1", function () {
        directDump.call(this);
        it("test 1", dump);
        it("test 2", dump);
    });
});

console.logステートメントは次を出力します。

running (direct): (fullTitle) top (title) top
running (direct): (fullTitle) top level 1 (title) level 1
running: (fullTitle) top test 1 (title) test 1
running: (fullTitle) top test 2 (title) test 2
running: (fullTitle) top level 1 test 1 (title) test 1
running: (fullTitle) top level 1 test 2 (title) test 2
于 2014-10-23T18:35:45.683 に答える
11

の中からbeforeEach、試してみてくださいthis.currentTest.title

例:

beforeEach(function(){
  console.log(this.currentTest.title); 
})

モカ使用3.4.1

于 2017-09-05T22:48:38.183 に答える