samedi 8 juillet 2017

setTimeout() outside of a runs block?

In the jasmine doc, the section regarding testing asynchronous operations states following code.

describe("Asynchronous specs", function() {
  var value, flag;

  it("should support async execution of test preparation and expectations", function() {

    runs(function() {
      flag = false;
      value = 0;

      setTimeout(function() {
        flag = true;
      }, 500);
    });


    waitsFor(function() {
      value++;
      return flag;
    }, "The Value should be incremented", 750);


    runs(function() {
      expect(value).toBeGreaterThan(0);
    });

  });

});

Here inside of the first runs block, there is a setTimeout() function. I understand that it is a asynchronous function so it will not block the code and script will continue executing codes below it.

My question is why implement setTimeout() function and other variables inside of a runs block as above without implementing them outside a runs block as follows

describe("Asynchronous specs", function() {
  var value, flag;

  it("should support async execution of test preparation and expectations", function() {

    flag = false;
    value = 0;

    setTimeout(function() {
      flag = true;
    }, 500);


    waitsFor(function() {
      value++;
      return flag;
    }, "The Value should be incremented", 750);


    runs(function() {
      expect(value).toBeGreaterThan(0);
    });

  });

});

Aucun commentaire:

Enregistrer un commentaire