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

∥☆過路亽.° 提交于 2019-12-03 06:53:27

问题


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 the new style.

For the waits, in most cases it seems like you can write beforeEach() and afterEach() carefully for the same effect.

What is the best way to reproduce the runs() which simply executes the contained functions sequentially?

My first try:

runs(function() {
  expect(true).toBe(true);
}

becomes

(function() {
  expect(true).toBe(true);
})()

回答1:


It is possible to use a setTimeout in your it() block.

it("is asynchronous", function(done) {
  var isItDone = false;
  $.ajax('/some/url').success(function() { isItDone = true; });

  setTimeout(function(){
    expect(isItDone).toBeTrue();
    done(); // call this to finish off the it block
  }, 500);

});

However, I found that that slowed down my test suite dramatically so I created my own extension which recreates the polling functionality that waitsFor provided.

https://gist.github.com/abreckner/110e28897d42126a3bb9




回答2:


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
});


来源:https://stackoverflow.com/questions/20963038/jasmine-2-0-refactoring-out-1-3s-runs-and-waitsfor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!