【问题标题】:How to conditionally skip a Mocha test in TypeScript如何在 TypeScript 中有条件地跳过 Mocha 测试
【发布时间】:2022-07-14 01:38:37
【问题描述】:

虽然this SO article 介绍了如何在 javascript 中跳过测试。讨论不包括如何在 TypeScript 中做同样的事情。

无效代码示例:

describe.only('Example test suite',() => {  
                                                     
  before(async () => {                               
    if(true) {                                       
      console.log('Unexpected condition. Test results may be invalid. Skipping tests.');  
      this.skip();                                                                                      
    }                                                                                                      
  });                                                                                                      
                                                                                                           
  it('it will do something',async () => {                                                                  
    console.log('This should not run.');                                                                   
  });                                                                                                      
                           
});

结果:

error TS2532: Object is possibly 'undefined'.

【问题讨论】:

    标签: typescript testing mocha.js skip


    【解决方案1】:

    我使用 Neovim 的 CocNvim 插件对 mocha 类型定义进行了一些内联自省,发现以下解决方案有效:

    describe.only('Example test suite',function(this:Mocha.Suite) {  
      const suite = this;                                  
                                                         
      before(async () => {                               
        if(true) {                                       
          console.log('Unexpected condition. Test results may be invalid. Skipping tests.');      
          suite.ctx.skip();                              
        }                                                
      });                                                
                                                         
      it('it will process rows',async () => {            
        console.log('This will not run.');               
      });                                                  
    });
    

    注意在描述处理程序中有意使用 function() 而不是箭头函数。这是因为箭头函数不能定义它们的 this:type。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-12
      • 2019-08-27
      • 1970-01-01
      • 1970-01-01
      • 2020-10-28
      相关资源
      最近更新 更多