【问题标题】:Get currently executed describe/test name获取当前执行的描述/测试名称
【发布时间】:2018-06-18 11:59:31
【问题描述】:

是否可以使用 Jest (Jasmine) 获取当前执行的测试名称或在测试内部进行描述?

使用 Jasmine: How to get name of current test 不再有效,至少在 Jest 中是这样。

例如

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

谢谢

【问题讨论】:

标签: jasmine jestjs


【解决方案1】:

来自this thread

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

为我工作。

【讨论】:

  • 与其重复一个重复问题的答案,不如将此问题标记为重复。
  • 我相信引用的问题实际上是这个问题的重复。
【解决方案2】:

测试应该只包含测试的基本代码:Arrange / Act / Assert,因此在此引入此类代码不是一个好习惯。但是如果你想记录当前正在运行的测试,你可以使用 custom_reporter API:https://jasmine.github.io/2.1/custom_reporter.html

通过添加以下代码,您可以获得与预期相同的结果:

jasmine.getEnv().addReporter({
  specStarted: function(result) {
    console.log(`Spec name: ${result.fullName}, description: ${result.description}`);
  }
});

【讨论】:

    【解决方案3】:

    你可以试试:

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

    【讨论】:

    • 如果我没记错的话,您正在检查尚未创建的对象。
    • 使用spec.description 而不是spec.getFullName()
    【解决方案4】:
    const testParam = 'any text you need';
    describe(`${testParam}`, () => {
      test('mind the backtick', () => {
        console.log(`Currently executing: ${testParam}`);
      });
    });
    

    【讨论】:

      猜你喜欢
      • 2023-03-22
      • 1970-01-01
      • 2010-10-03
      • 2010-10-01
      • 2012-09-26
      • 1970-01-01
      • 2019-01-18
      • 2014-05-08
      • 2011-03-11
      相关资源
      最近更新 更多