[英]How to get asserts in mocha before blocks to work?
How to get asserts in mocha before blocks to work? 如何在块生效之前在mocha中获取断言? If I am not suppose to do this let me know.
如果我不想这样做,请告诉我。 Currently, using promises, if I get an error using an catch block, I'll add an assert to fail the before block.
目前,使用promises,如果我使用catch块得到错误,我将添加一个assert以使before块失败。 WHat I want is it to fail the describe block, but instead I get two possible outcomes.
我想要的是它会使描述块失败,但我会得到两个可能的结果。 1. My test suite completely crashes, 2. I have to wait for each timeout to hit for each test because the before block failed.
1.我的测试套件完全崩溃,2。我必须等待每个测试的每个超时,因为前一个块失败了。
before(function (done) {
promise()
.then(function () {
done();
})
.catch(function (error) {
assert(!error);
done();
});
});
I even tried this, thinking, maybe the done was never called. 我甚至尝试过这个,想一想,也许从未调用过。
before(function (done) {
promise()
.then(function () {
//no done here
})
.catch(function (error) {
assert(!error);
});
.finally(function () {
done();
});
});
So far to avoid crashing and waiting, and to make it work, I have done this: 到目前为止,为了避免崩溃和等待,并使其工作,我做到了这一点:
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.');
});
I am really curious if there is a better way to go about this so that if my before/beforeEach/after/afterEach blocks fail, it doesn't cause me to wait ages or my suite to crash! 我真的很好奇,如果有更好的方法可以解决这个问题,那么如果我的before / beforeEach / after / afterEach块失败,它不会导致我等待多年或我的套件崩溃! Thanks S/O community!
谢谢S / O社区! :)
:)
I can't speak to your use of the done
callback, but mocha 3.0 supports promises in before hooks now. 我不能说你使用
done
回调,但是mocha 3.0现在支持在钩子之前的承诺 。 Were I to write this, I would let the returned promise throw its own error, which will fail the before hook without breaking the suite. 如果我写这个,我会让返回的promise抛出自己的错误,这会在不破坏套件的情况下使before hook失败。
before(function () {
return promise(<async behavior here>);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.