Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get currently executed describe/test name

Tags:

jestjs

jasmine

Is it possible with Jest (Jasmine) to get the currently executed name of the test or describe inside the test?

Using Jasmine: How to get name of current test is not working anymore, at least with Jest.

e.g.

test('Error missing body', (done) => {
  console.log('Currently executing: ' + REFERENCE_TO_TEST_NAME);
  done();
});

Thanks

like image 508
MortalFool Avatar asked Jan 09 '18 12:01

MortalFool


2 Answers

From this thread:

console.log(expect.getState().currentTestName);

Worked for me.

like image 57
Matthew Souther Avatar answered Nov 17 '22 08:11

Matthew Souther


The tests are supposed to contain only the basic code for your test: Arrange / Act / Assert, so it's not a good practice to introduce this kind of code at this place. But if you want to log the currently running test, you can use the custom_reporter API: https://jasmine.github.io/2.1/custom_reporter.html

You can get the same result that you expect by adding this code:

jasmine.getEnv().addReporter({
  specStarted: function(result) {
    console.log(`Spec name: ${result.fullName}, description: ${result.description}`);
  }
});
like image 32
Elias Platek Avatar answered Nov 17 '22 10:11

Elias Platek