Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jasmine: why beforeEach() works in nested describe but beforeAll() doesn't?

I am struggling to understand why my code won't work and why the tests fail when I use a simple beforeAll() instead of a beforeEach() in a nested describe suite of tests? Here is a small example to outline my problem:

describe("myService", function() {
  // Basic services
  // Some variables 

  beforeEach(module('app'));   // Invoke the module
  beforeEach(function(){

    // Inject the services
    inject(function(_myService_) {
      myService = _myService_;
    });
  });

  /************************ Add more unit tests here ************************/

  describe("myFunction", function() {
    describe("calls function with one set of input paramenters", function() {

      //beforeAll(function() {
      beforeEach(function() { // <-- Why this works but beforeAll doesn't???
        // Call myFunction with different parameters
        result = myService.myFunction(parametersType_1);
      });

      it("should do tests on result (the return from the function)", function() {
      });
    });

  describe("calls function with other set of input paramenters", function() {

    //beforeAll(function() {
    beforeEach(function() { // <-- Why this works but beforeAll doesn't???
      // Call myFunction with different parameters
      result = myService.myFunction(parametersType_2);
    });

    it("should do tests on result (the return from the function)", function() {
    });
  });
}); 
like image 464
effort Avatar asked Jun 02 '16 14:06

effort


2 Answers

Change the section where you are injecting the service to a beforeAll instead of a beforeEach:

beforeAll(function(){

    // Inject the services
    inject(function(_myService_) {
    myService = _myService_;
});

The beforeEach in the outer describe won't fire before each of the nested describe sections, it fires before each "it" within the describes. Because the inner beforeAll gets fired before the beforeEach in the outer describe, you are trying to use the service before it has been injected.

So for example:

describe("outer describe", function() {

 beforeAll(function() {
    console.log("A");   
 });

 beforeEach(function() {
    console.log("B");   
 });

 describe("inner describe", function() {
    beforeAll(function() {
        console.log("C");
    });

    beforeEach(function() {
        console.log("D");
    });

    it("test", function() {
    })'
 });

});

Will execute in the order: A, C, B, D

like image 93
Andrew Avatar answered Oct 25 '22 16:10

Andrew


I just wanted to improve @Andrew answer with images and outputs.

The resource of beforeAll-Each : http://breazeal.com/blog/jasmineBefore.html

Plunker link is: https://plnkr.co/plunk/wARTBcGQJitmOALs

describe('outer describe', function () {
  beforeAll(function () {
    console.log('A: outer desc->before-All');
  });

  beforeEach(function () {
    console.log('B: outer desc 1->before-Each');
  });

  afterAll(function () {
    console.log('AA: outer desc->after-All');
  });

  afterEach(function () {
    console.log('BB: outer desc 1->after-Each');
  });

  describe('inner describe 1', function () {
    beforeAll(function () {
      console.log('C: inner desc 1->before-All');
    });

    beforeEach(function () {
      console.log('D: inner desc 1->before-Each');
    });

    afterAll(function () {
      console.log('CC: inner desc 1->after-All');
    });

    afterEach(function () {
      console.log('DD: inner desc 1->after-Each');
    });

    it('test1', function () {
      console.log('inner desc 1 -> test 1');
      expect(false, 'olmadı');
    });

    it('test2', function () {
      console.log('inner desc 1 -> test 2');
      expect(false, 'olmadı');
    });
  });

  describe('inner describe2', function () {
    beforeAll(function () {
      console.log('E: inner desc 2->before-All');
    });

    beforeEach(function () {
      console.log('F: inner desc 2->before-Each');
    });

    afterAll(function () {
      console.log('EE: inner desc 2->after-All');
    });

    afterEach(function () {
      console.log('FF: inner desc 2->after-Each');
    });

    it('test1', function () {
      console.log('inner desc 2 -> test 1');
      expect(false, 'olmadı');
    });

    it('test2', function () {
      console.log('inner desc 2 -> test 2');
      expect(false, 'olmadı');
    });
  });
});

enter image description here

like image 20
uzay95 Avatar answered Oct 25 '22 17:10

uzay95