Mocha: Get current test's name of test generate on loop?

Created on 12 Nov 2017  路  2Comments  路  Source: mochajs/mocha

This code works: I used the variable testTitle in beforeEach function.

const list = [
    { title: 'login', uri: '/login', should: 'Login' },
    { title: 'signup', uri: '/signup', should: 'Create'}
];

let testTitle = '';

beforeEach(function() {
    testTitle = this.currentTest.fullTitle();
});

describe('navegation', () => {
    list.forEach(function(item) {
        it(item.title, (done, obj) => {

            // ------------------------------------
            // Not work it
            // console.log(this.test.fullTitle()); 
            // ------------------------------------

            request(server)
                .get(item.uri)
                .expect(constant.STATUS.OK)
                .then(res => {
                    res.text.should.include(item.should);
                    done();
                })
                .catch(err => {
                    console.log(testTitle + ': ' + err);
                    return done(err);
                });
        });
    });
});

Question:
How I can improve my code for used this.test.fullTitle() directly on loop and avoid I used the variable testTitle and beforeEach function?

Thanks.

question

Most helpful comment

Your it has to use function instead of => to get access to Mocha's this object; this.test.fullTitle() should work with function.

All 2 comments

Your it has to use function instead of => to get access to Mocha's this object; this.test.fullTitle() should work with function.

Thanks @ScottFreeCode!

Was this page helpful?
0 / 5 - 0 ratings