Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the test name in the afterEach function in mocha

Let me start by saying I am quite new to node.js and mocha.It just breaks my head. I started using the tdd approach and I am trying to get the test that will start or has just finished from within the beforeEach and afterEach functions, but I've had no luck.(I am mostly interested in afterEach). At least I couldn't figure a neat way of doing it. The only thing I could think of was keeping the tests and the suite in a variable and then on afterEach() just do some matching to see which test has finished.

Ideally where it says 'test name' I want to have something like 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');
    }
}
like image 387
Zee Avatar asked Jul 27 '13 00:07

Zee


People also ask

How do I specify a test file in mocha?

The nice way to do this is to add a "test" npm script in package. json that calls mocha with the right arguments. This way your package. json also describes your test structure.

Can Mocha rerun tests?

Retrying Mocha tests Mocha provides a this. retries() function that allows you specify the number of times a failed test can be retried. For each retry, Mocha reruns the beforeEach() and afterEach() Hooks but not the before() and after() Hooks.

What is a pending test in mocha technically?

A pending test in many test framework is test that the runner decided to not run. Sometime it's because the test is flagged to be skipped. Sometime because the test is a just a placeholder for a TODO. For Mocha, the documentation says that a pending test is a test without any callback.


1 Answers

You get the name of the current test with this.currentTest.title

afterEach(function(){
    console.log(this.currentTest.title)
})
like image 113
Andreas Köberle Avatar answered Nov 13 '22 05:11

Andreas Köberle