Get currently executed describe/test name
Asked Answered
G

5

41

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

Ghyll answered 9/1, 2018 at 12:36 Comment(2)
This is an exact duplicate of Get the current test/spec name in Jest, which albeit newer, has higher quality answers, sourced from the Jest GitHub repo.Wendiewendin
Does this answer your question? Get the current test/spec name in JestMichaeu
P
66

From this thread:

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

Worked for me.

Pellicle answered 27/8, 2020 at 23:25 Comment(2)
Instead of repeating an answer from a duplicate question, better to mark this question as a duplicate.Wendiewendin
I believe the referenced question is actually a duplicate of this one.Differential
P
4

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}`);
  }
});
Patsypatt answered 7/2, 2018 at 13:0 Comment(0)
N
0

you can try:

let spec = test('Error missing body', (done) => {
  console.log('Currently executing: ' +  spec.getFullName());
  done();
});
Nephrosis answered 7/2, 2018 at 15:8 Comment(2)
If I am not mistaken, you are checking object which isn't created yet.Vespiary
Use spec.description instead of spec.getFullName()Cessionary
P
0

You can try this. Add this in conf file for beforeTest and afterTest hooks. This worked for me:

beforeTest: function (test, context) {
    console.log("Execution Started for - "+test.fullName);
},
Ploce answered 1/6, 2023 at 5:33 Comment(0)
K
-8
const testParam = 'any text you need';
describe(`${testParam}`, () => {
  test('mind the backtick', () => {
    console.log(`Currently executing: ${testParam}`);
  });
});
Kuban answered 4/2, 2018 at 14:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.