Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jasmine: How to get name of current test

Is there a way of getting the name of the currently running test?

Some (heavily simplified) code may help explain. I want to avoid the duplication of "test1" / "test2" in the calls to performTest:

describe("My test category", function () {      function performTest(uniqueName, speed) {         var result = functionUnderTest(uniqueName, speed);         expect(result).toBeTruthy();     }      it("test1", function () {         performTest("test1", "fast");     });      it("test2", function () {         performTest("test2", "slow");     }); }); 

UPDATE I see the information I need is in:

jasmine.currentEnv_.currentSpec.description 

or probably better:

jasmine.getEnv().currentSpec.description 
like image 914
GarethOwen Avatar asked Oct 05 '12 08:10

GarethOwen


People also ask

How do you name Jasmine test?

Speak Human. Label your test suites ( describe blocks) and specs ( it blocks) in a way that clearly conveys the intention of each unit test. Note that the name of each test is the title of its it preceded by all its parent describe names. Favor assertive verbs and avoid ones like "should."

What is done () in Jasmine?

Using the done() Method in Your Jasmine-driven Asynchronous JavaScript Tests. Jasmine. Async is an add-on library for Jasmine that provides additional functionality to do asynchronous testing. Modeled after Mocha's async test support, it brings the done() function to the Jasmine unit testing environment.

How do you catch error in Jasmine?

var throwMeAnError = function() { //throw new Error(); }; describe("Different Methods of Expect Block",function() { var exp = 25; it("Hey this will throw an Error ", function() { expect(throwMeAnError). toThrow(); }); });

Which specifies where Jasmine looks for test files?

spec_dir : specifies where Jasmine looks for test files.


2 Answers

jasmine.getEnv().currentSpec.description 
like image 99
GarethOwen Avatar answered Oct 11 '22 16:10

GarethOwen


It's not pretty (introduces a global variable) but you can do it with a custom reporter:

// current-spec-reporter.js  global.currentSpec = null;  class CurrentSpecReporter {    specStarted(spec) {     global.currentSpec = spec;   }    specDone() {     global.currentSpec = null;   }  }  module.exports = CurrentSpecReporter; 

Add it to jasmine when you add your other reporters...

const CurrentSpecReporter = require('./current-spec-reporter.js'); // ... jasmine.getEnv().addReporter(new CurrentSpecReporter());  

Then extract the test name during your test/setup as needed...

  it('Should have an accessible description', () => {     expect(global.currentSpec.description).toBe('Should have an accessible description');   }  

At last, see a sample of currentSpec structure:

{   "id":"spec15",   "description":"test1",   "fullName":"My test category test1",   "failedExpectations":[],   "passedExpectations":[],   "deprecationWarnings":[],   "pendingReason":"",   "duration":null,   "properties":null } 
like image 42
Pace Avatar answered Oct 11 '22 17:10

Pace