Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I retrieve the current test's name within a Mocha test?

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

How can I do this (with Mocha BDD)?

like image 542
lairtech Avatar asked Aug 18 '13 22:08

lairtech


People also ask

How do I run a specific test file in Mocha?

If you want to run all the test cases which are inside one describe section, then you can also write only to describe as well. describe. only('<Description of the tests under this section>', function() { // ... }); If you have multiple test files & you wanted to run only one of then you can follow the below command.

What are pending tests in Mocha?

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.

Can Mocha rerun tests?

Retrying Mocha testsMocha 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.

Do Mocha tests run sequentially?

According to it, tests are run synchronously. This only shows that ordered code is run in order. That doesn't happen by accident.


2 Answers

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 
like image 87
Louis Avatar answered Oct 14 '22 09:10

Louis


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

Example:

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

Using Mocha 3.4.1.

like image 35
Andrew Homeyer Avatar answered Oct 14 '22 10:10

Andrew Homeyer