How to get information of Mocha # type objec

2019-09-19 06:50发布

问题:

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"