在mocha的afterEach函数中获取testing名称

让我首先说我对node.js和mocha很新颖。 我开始使用tdd方法,我试图从beforeEach和afterEach函数中开始或者刚刚完成的testing,但是我没有运气(我主要对afterEach感兴趣)。 至less我无法想象一个干净利落的做法。 我唯一能想到的就是把testing和套件保存在一个variables中,然后在afterEach()上做一些匹配,看看哪个testing已经完成。

理想情况下,它说“testing名称”,我想有一些像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'); } } 

你用this.currentTest.title得到当前testing的this.currentTest.title

 afterEach(function(){ console.log(this.currentTest.title) }) 

我发现我使用this.currentTest.fullTitle()多于this.currentTest.title – 我更喜欢具有describe名称。

如果你有嵌套的描述块,出于某种原因,你想分开标题部分,你可以在beforeEachafterEach方法中做以下的beforeEach

 function titles(test) { console.log(test.title) if (test.parent) { titles(test.parent) } } titles(this.currentTest)