[英]Jasmine - How to set a timeout for a describe block?
I have a class which is poorly written, and get initialized only after a short timeout. 我有一个写得不好的类,并且仅在短暂的超时后才初始化。 (It's dependent in a 3rd party loading, so it's pointless to offer modifying the code. thank you) Nevertheless, it needs to be unit tested.
(它依赖于第三方加载,因此提供修改代码毫无意义。谢谢)。但是,它需要进行单元测试。
What I have right now is the following structure: 我现在拥有的是以下结构:
describe('my tests', function() {
var timeoutPromise = function () {
/... return promise .../
}
it('test1', function (done) {
timeoutPromise.then(function() {
expect(...);
done();
});
});
it('test2', function (done) {
timeoutPromise.then(function() {
expect(...);
done();
});
});
});
That way I make sure all tests run after a few ms timeout. 这样,我确保所有测试都会在几毫秒的超时后运行。 Is there is way to make the whole describe block run only after the timeout?
有没有办法使整个describe块仅在超时后运行? Something like
就像是
describe('my tests', function(done) {
it(...);
it(...);
done();
}
There's no describe()
level done
callback. 没有
describe()
级done
回调。 From the Jasmine 2.5 docs : 来自Jasmine 2.5 docs :
Calls to
beforeAll
,afterAll
,beforeEach
,afterEach
, andit
can take an optional single argument that should be called when the async work is complete.调用
beforeAll
,afterAll
,beforeEach
,afterEach
,并且it
可以异步工作完成时应该叫一个可选的一个参数。
However, you can do one time initialization in a beforeEach()
on the same level as the it()
blocks they belong to. 但是,您可以在
beforeEach()
中与它们所属的it()
块处于同一级别上进行一次初始化。
The following code will fullfill your requirements. 以下代码将满足您的要求。 It is inline commented:
内联评论:
describe('your tests', function() {
var timeoutPromise = new Promise(function(resolve, reject){
setTimeout(resolve, 4000);
});
// execute timeoutPromise and wait for it to resolve ...
beforeEach(function(done) {
timeoutPromise.then(function() {
// timeout resolved, test initialization in this block done
done();
});
});
// for all following it()s the beforeEach() needs to complete
it('test1', function () {
expect(...);
});
it('test2', function () {
expect(...);
});
// ...
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.