38

For additional logging, I need to be able to print the current test's description.

How can I do this (with Mocha BDD)?

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
lairtech
  • 2,337
  • 4
  • 25
  • 38

5 Answers5

56

If you are directly inside a callback to describe, you can use this.title for the title of the describe or this.fullTitle() to get the hierarchical title of the describe (ancestors' titles + the title of this one). If you are inside a callback to it you can use this.test.title or this.test.fullTitle() respectively. So:

describe("top", function() {
    console.log(this.title);
    console.log(this.fullTitle());

    it("test", function () {
        console.log(this.test.title);
        console.log(this.test.fullTitle());
    });
});

The console.log statements above will output:

top
top
test
top test

Here's a fuller example that shows how the titles change depending on nesting:

function dump () {
    console.log("running: (fullTitle)", this.test.fullTitle(), "(title)",
                this.test.title);
}

function directDump() {
    console.log("running (direct): (fullTitle)", this.fullTitle(), "(title)",
                this.title);
}

describe("top", function () {
    directDump.call(this);
    it("test 1", dump);
    it("test 2", dump);
    describe("level 1", function () {
        directDump.call(this);
        it("test 1", dump);
        it("test 2", dump);
    });
});

The console.log statements will output:

running (direct): (fullTitle) top (title) top
running (direct): (fullTitle) top level 1 (title) level 1
running: (fullTitle) top test 1 (title) test 1
running: (fullTitle) top test 2 (title) test 2
running: (fullTitle) top level 1 test 1 (title) test 1
running: (fullTitle) top level 1 test 2 (title) test 2
Louis
  • 146,715
  • 28
  • 274
  • 320
  • 1
    Could you point to Mocha's documentation where API like `this.test.fullTitle( )` is documented? It would be great for the public to know ,explore and find answers to questions like this and more. – Yiling Nov 12 '15 at 22:43
  • Unfortunately, the documentation in this area is lacking. If you are concerned about the stability of this API, what I can say is that reporters use this to produce their reports so if the Mocha devs just changed it, they'd break third party reporters. – Louis Nov 13 '15 at 00:39
  • 2
    A little late, but this might help: https://github.com/mochajs/mocha/blob/master/lib/test.js – Carson Reinke Aug 01 '16 at 18:28
  • 3
    Note, this only works when using `it('name', function () {` syntax. `this` is undefined when using arrow functions `() => {` – James Sep 23 '21 at 13:59
15

From within a beforeEach, try this.currentTest.title.

Example:

beforeEach(function(){
  console.log(this.currentTest.title); 
})

Using Mocha 3.4.1.

Andrew Homeyer
  • 7,937
  • 5
  • 33
  • 26
  • 1
    I have tried this with Mocha ^9.2.1 but I get `Cannot read properties of undefined (reading 'test')`. When I log `this.currentTest` to the console, it returns `undefined` – user9847788 Mar 08 '22 at 21:25
5

For mocha "^5.1.0" you can use console.log(this.ctx.test.title);

Ken
  • 365
  • 2
  • 7
-3

Inside any test method

it('test method name'), function()  { var testName= this.test.title; }

and you may use :

afterEach(function(){
    console.log(this.currentTest.title); //displays test title for each test method      
});
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
Amado Saladino
  • 2,114
  • 23
  • 25
-10

Here you go:

console.log(this.title);
Dan Kohn
  • 33,811
  • 9
  • 84
  • 100