简体   繁体   English

茉莉花-如何设置describe块的超时时间?

[英]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 , and it can take an optional single argument that should be called when the async work is complete. 调用beforeAllafterAllbeforeEachafterEach ,并且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.

相关问题 如何不为茉莉花中的特定Describe()块在BeforeAll中运行某些代码 - How not to run certain codes in BeforeAll for a specific Describe() block in Jasmine 如何抽象和重用茉莉花中的describe()块? - how to abstract and re-use a describe() block in jasmine? 运行jasmine测试时,我怎么知道我是否在一个描述块中,在每个块之前还是阻塞? - When running jasmine tests, how can I know if I am in a describe block, beforeEach block or it block? 在Jasmine中为多个相关文件使用describe块 - Using a describe block for multiple related files in Jasmine 量角器Jasmine描述嵌套在它块内的块 - Protractor Jasmine describe blocks nested within an it block 无法为茉莉花设置超时 - Can't set timeout for jasmine 有没有办法计算jasmine describe块中的测试数量? - Is there a way to get the count for the number of tests in a jasmine describe block? 如何在上一个描述中出现错误时跳到下一个描述()? 茉莉花测试 - how to skip to next describe() upon error in previous describe? jasmine test 如何在 jasmine 之后的每个 function 中调用 describe 或者它 function - How to call describe or it function in afterEach function in jasmine 描述块内的 jest.setTimeOut() 是否仅将超时应用于描述块内的测试 - Does jest.setTimeOut() inside a describe block apply the timeout only to tests inside the describe block
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM