Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jasmine2: get current spec name

Tags:

In Jasmine 1.3, we had this option to the get current spec and suite names:

describe("name for describe", function () {
    it("name for it", function () {
        console.log(this.suite.getFullName()); // would print "name for describe"
        console.log(this.description); // would print "name for it"
    });
});

This does not longer work in Jasmine 2.x. Anyone knows how to fetch those?

Thanks.

like image 414
bomba6 Avatar asked Mar 18 '15 09:03

bomba6


2 Answers

I add a new jasmine reporter, then get the spec name without define N variable on each spec. Hope can help, thanks.

var reporterCurrentSpec = {
     specStarted: function(result) {
         this.name = result.fullName;
     }
 };
jasmine.getEnv().addReporter(reporterCurrentSpec);
like image 193
Joseph Law Avatar answered Sep 24 '22 01:09

Joseph Law


The reason this no longer works is because this is not the test. You can introduce a subtle change to your declarations however that fix it. Instead of just doing:

it("name for it", function() {});

Define the it as a variable:

var spec = it("name for it", function() {
   console.log(spec.description); // prints "name for it"
});

This requires no plug-ins and works with standard Jasmine.

like image 6
Ian Avatar answered Sep 23 '22 01:09

Ian