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() {
});
});
});
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
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ı');
});
});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With