Jasmine 2.0: refactoring out 1.3's runs() and waitsFor()

前端 未结 2 2052
情书的邮戳
情书的邮戳 2021-02-02 11:55

The recently released Jasmine 2.0 removes the waits functions and the runs() from the Async Jasmine 1.3.

I have old 1.3 tests I\'d like to transition to th

2条回答
  •  天涯浪人
    2021-02-02 12:01

    In jasmine 1.3 and previous, the runs and waits/waitsFor should have only been necessary if you had some asynchronous code that you needed to wait until it was done before doing the next part of the test. In that case you would have something like:

    it("is asynchronous", function() {
        var isItDone = false;
        runs(function() {
            $.ajax('/some/url').success(function() { isItDone = true; });
        });
    
        waitsFor(function() {
            return isItDone;
        });
    
        runs(function() {
            // this won't run until the waitsFor returns true
        });
    });
    

    Jasmine 2.0 moved to using a done callback for beforeEach, it, and afterEach if they do something asynchronous that you need to wait for.

    beforeEach(function(done) {
        $.ajax('/some/url').success(done);
    });
    
    it("is asynchronous", function() {
        // this won't run until the done callback is invoked from the beforeEach
    });
    

提交回复
热议问题