Created
August 5, 2014 19:05
-
-
Save cshtdd/55dacf634116dc0a7c6e to your computer and use it in GitHub Desktop.
explain the order in which beforeEach gets called in Jasmine and the test variables scope
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe("jasmine class variables", function() { | |
var foo = 0; | |
beforeEach(function() { | |
foo += 1; | |
}); | |
it("can be assigned in the before each", function() { | |
expect(foo).toEqual(1); | |
}); | |
it("but the class does not get recreated for each it, so remember to cleanup", function() { | |
expect(foo).toEqual(2); | |
}); | |
it("since this is the first describe level foo only gets incremented in one", function() { | |
expect(foo).toEqual(3); | |
}); | |
describe("nested describe", function() { | |
var bar = 0; | |
beforeEach(function() { | |
bar = 1; | |
}); | |
afterEach(function(){ | |
bar = 0; | |
}); | |
it ("can reference parent scope", function(){ | |
expect(foo).toEqual(4); | |
}); | |
it("class variables", function() { | |
expect(bar).toBe(1); | |
}); | |
it("must be reset in the afterEach", function() { | |
expect(bar).toBe(1); | |
}); | |
it ("in the meantime parent scope beforeEach got called", function(){ | |
expect(foo).toEqual(7); | |
}); | |
}); | |
describe("second level desccibe foo gets incremented in two", function(){ | |
beforeEach(function(){ | |
foo += 1; | |
}); | |
it("describes get called a lot", function() { | |
expect(foo).toBe(9); | |
}); | |
it("foreach it all of its parents describes get called", function() { | |
expect(foo).toBe(11); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment