在mocha中获取afterEach函数中的测试名称

Zee*_*Zee 17 mocha.js node.js

首先让我说我对node.js和mocha很新.它只是让我失望.我开始使用tdd方法,我正在尝试从beforeEach和afterEach函数中开始或刚刚完成的测试,但我没有运气.(我最感兴趣的是afterEach).至少我无法想出一个干净利落的方式.我唯一能想到的是将测试和套件保存在变量中,然后在afterEach()上进行一些匹配以查看哪个测试已经完成.

理想情况下,它说'test name'我希望有类似suite.test.name的东西

suite('my test suite', function() {
    beforeEach(function () {
        console.log('test name');
    });
    test('first test', function (done) {
        var testarray = ['1', '3', '5', '7'];
        testarray.forEach(function(num, index) {
            console.log('num: ' + num + ' index: ' + index);
        }, 
        done());
    });
    afterEach(){
        console.log('test name');
    }
}
Run Code Online (Sandbox Code Playgroud)

And*_*rle 22

您将获得当前测试的名称 this.currentTest.title

afterEach(function(){
    console.log(this.currentTest.title)
})
Run Code Online (Sandbox Code Playgroud)

  • 你确定吗?我正在使用mocha 1.12并获得"每个"之后的钩子:TypeError:无法读取undefined的属性'title' (4认同)
  • 回答我自己的问题,它是it()中的this.test.title,而不是this.currentTest.title. (2认同)

Mic*_*cah 12

我发现我使用的this.currentTest.fullTitle()不仅仅是this.currentTest.title- 我更喜欢这些describe名字.