Get currently executed describe/test name

余生长醉 提交于 2020-05-26 11:41:06

问题


is it possible with jest(jasmine) to ge 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 or at least with jest.

e.g.

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

Thanks


回答1:


const testParam = 'any text you need';
describe(`${testParam}`, () => {
  test('mind the backtick', () => {
    console.log(`Currently executing: ${testParam}`);
  });
});



回答2:


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



回答3:


you can try:

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


来源:https://stackoverflow.com/questions/48168385/get-currently-executed-describe-test-name

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!