mardi 29 décembre 2015

jasmine beforeAll affected by sibling describe

I was under the impression that the beforeAll-function would run once for the describe it was inside. However it seems that sibling describes can affect the beforeAll.

the following tests can be found here

describe("outer Describe", function(){
    var testArray;
    beforeEach(function(){
      testArray = [];
    });
    describe("First Describe", function(){
      beforeEach(function(){
        testArray.push({name: "foo"});
      });

      it("has an item", function(){
        expect(testArray.length).toBe(1);//passing
      });
    });

    describe("Second describe", function(){
      var arrIsEmptyInBeforeAll;
      var arrIsEmptyInBeforeEach;
      beforeAll(function(){
        arrIsEmptyInBeforeAll = testArray.length === 0;
        console.log("testArray should be empty:");
        console.log(testArray);//logs array with one item
      });

      beforeEach(function(){
        arrIsEmptyInBeforeEach = testArray.length === 0;
      });

      it("the arr was empty in before all", function(){
        expect(arrIsEmpty).toBeTruthy(); //This is failing
      });

      it("the arr was empty in beforeEach", function(){
        expect(arrIsEmptyInBeforeEach).toBeTruthy();//Passing
      })
    })
  });

I would expect that the beforeAll inside the "Second Describe" would have an empty testArray, because the "outer Describe" has a beforeEach that initializes it as an empty array. However, in the beforeAll of "Second Describe", the testArray has the one item added in "First Describe"'s beforeEach.

Does this make sense? If so, could someone explain how beforeAll is supposed to work.

Aucun commentaire:

Enregistrer un commentaire