How to get information of Mocha #<Test> type object

故事扮演 提交于 2019-12-13 04:25:10

问题


I am using mocha on browser.

inside function done i have a object named as test

it('', function(done){
                console.log(this.test);
                  done(error);
              });

if we check on log we get this.test a 'Test' type object.

Test {title: "", fn: function, async: 1, sync: false, _timeout: 50000…}

if i console this.test.duration or any variable i find undefined.

i want to extract duration information of this 'Test' type object.

How to do that..??


回答1:


I've read mocha's source but I do not see a way to extract from a test case the information you want while a test is running. The duration field is computed once, when the test ends.

You could still compute how long your tests has been running with:

it('', function(done){
    var started_at = new Date;
    [... do stuff ...]
    console.log("duration so far:" , (new Date - started_at) + "ms");
    [... do more stuff ...]
    done(error);
});



回答2:


You can only access the properties of this.test when the test is actually over and done. So in order to fetch this data you need to call done(); before logging to your console.

it("is a test", function(done) {
    done();
    console.log(this.test.title);
    console.log(this.test.state);
});
>> "is a test"
>> "passed"


来源:https://stackoverflow.com/questions/20328172/how-to-get-information-of-mocha-test-type-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!