2016-09-06 46 views
0

如何在模块开始工作前在摩卡断言?如果我不想这样做让我知道。目前,使用promises,如果我使用catch块发生错误,我会添加一个assert来使before块失败。我想要的是失败的描述块,但我得到两个可能的结果。 1.我的测试套件完全崩溃,2.我必须等待每个测试的每次超时,因为before块失败。如何在模块开始工作之前让摩卡断言?

before(function (done) { 
    promise() 
     .then(function() { 
      done(); 
     }) 
     .catch(function (error) { 
      assert(!error); 
      done(); 
     }); 
}); 

我甚至试过这个,想着,也许这个事情从来没有叫过。

before(function (done) { 
    promise() 
     .then(function() { 
      //no done here 
     }) 
     .catch(function (error) { 
      assert(!error); 
     }); 
     .finally(function() { 
      done(); 
     }); 
}); 

到目前为止,以避免崩溃和等待,并使其工作,我已经做到了这一点:

var beforeError; 
before(function (done) { 
    promise() 
     .then(function() { 
      done(); 
     }) 
     .catch(function (error) { 
      beforeError = error; 
      done(); 
     }); 
}); 

it('test something', function() { 
    assert(beforeError, 'Before block failed with error.'); 
}); 

我如果有一个更好的方式去了解这个真的很好奇,这样,如果我之前/之前/之后/之后每个块失败,它不会导致我等待年龄或我的套件崩溃!感谢S/O社区! :)

+1

只需调用完成的错误。 '完成(ERR)' –

回答

2

我不能说你使用的done回调,但摩卡3.0现在支持promises in before hooks。如果我写这个,我会让返回的承诺抛出自己的错误,这会在不打断套件的情况下使前钩失败。

before(function() { 
    return promise(<async behavior here>); 
});