Skip to content

Instantly share code, notes, and snippets.

@cshtdd
Created August 5, 2014 19:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cshtdd/55dacf634116dc0a7c6e to your computer and use it in GitHub Desktop.
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
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