bomba6
bomba6

Reputation: 579

Jasmine2: get current spec name

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.

Upvotes: 20

Views: 9216

Answers (6)

Mushtaq Jameel
Mushtaq Jameel

Reputation: 7083

This worked for me in jasmine 3.5+

I know this is a relatively old question but found something which worked for me

describe('Desc1',() => {
   
    afterEach(() => {
      const myReporter = {
        specDone: (result) => {
          console.log('Spec FullName: ' + result.fullName);
          console.log('Spec Result: ' + result.status);
        }
      };
      jasmine.getEnv().addReporter(myReporter);
    });
})

Credit for the solution : https://groups.google.com/g/jasmine-js/c/qqOk6Nh7m4c/m/Nyovy2EjAgAJ

Upvotes: 0

Amarendra Kothuru
Amarendra Kothuru

Reputation: 11

    var currentSpecName = describe('Test1', function() {
        var currentStepName = it("Step1", function(){
           console.log(currentStepName.description); // Prints It Name
           console.log(currentSpecName.getFullName()); //Prints Describe Name
        });
    });

Upvotes: 0

Joseph Law
Joseph Law

Reputation: 91

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);

Upvotes: 9

Ian
Ian

Reputation: 34489

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.

Upvotes: 6

giri-sh
giri-sh

Reputation: 6962

As far as Jasmine 2 is concerned currentSpec is discontinued on purpose. However there is a custom plugin/library developed that is based on jasmine reporter plugin which you can use. Here's the Link. Hope it helps with your requirement.

Its very simple to use, install the package with npm command -

npm install -g jasmine-test-container-support

Get the test container support by writing below lines before your describe or test suite -

var JasmineTestContainerSupport = window.JasmineTestContainerSupport || require('jasmine-test-container-support');

JasmineTestContainerSupport.extend(jasmine);

Later use the test container in your spec's to get its description -

var specDesc = jasmine.getEnv().getTestContainer();

Hope this helps.

Upvotes: 2

warun26
warun26

Reputation: 453

This is probably a bit late but you can get the suite name outside the spec. Please try the following code:

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

Upvotes: -2

Related Questions