From a2041e90a620307400216b3c61acddc8e7b426f5 Mon Sep 17 00:00:00 2001 From: Christian Williams Date: Fri, 4 Jun 2010 14:41:16 -0400 Subject: [PATCH] Multiple befores/afters in a single describe should be executed in order (as declared for befores, in reverse for afters). --- spec/suites/SpecRunningSpec.js | 59 +++++++++++++++++++++++++++++++++- src/Runner.js | 4 +-- src/Suite.js | 4 +-- 3 files changed, 62 insertions(+), 5 deletions(-) diff --git a/spec/suites/SpecRunningSpec.js b/spec/suites/SpecRunningSpec.js index 49a986d..67f5229 100644 --- a/spec/suites/SpecRunningSpec.js +++ b/spec/suites/SpecRunningSpec.js @@ -861,7 +861,7 @@ describe("jasmine spec running", function () { expect(suiteResults.getItems()[2].getItems()[0].passed()).toEqual(true, "testAfterExecutesSafely 3rd suite spec should pass"); }); - it("testNestedDescribes", function() { + it("should permit nested describes", function() { var actions = []; env.beforeEach(function () { @@ -953,6 +953,63 @@ describe("jasmine spec running", function () { expect(actions).toEqual(expected); }); + it("should run multiple befores and afters in the order they are declared", function() { + var actions = []; + + env.beforeEach(function () { + actions.push('runner beforeEach1'); + }); + + env.afterEach(function () { + actions.push('runner afterEach1'); + }); + + env.beforeEach(function () { + actions.push('runner beforeEach2'); + }); + + env.afterEach(function () { + actions.push('runner afterEach2'); + }); + + env.describe('Something', function() { + env.beforeEach(function() { + actions.push('beforeEach1'); + }); + + env.afterEach(function() { + actions.push('afterEach1'); + }); + + env.beforeEach(function() { + actions.push('beforeEach2'); + }); + + env.afterEach(function() { + actions.push('afterEach2'); + }); + + env.it('does it 1', function() { + actions.push('outer it 1'); + }); + }); + + env.execute(); + + var expected = [ + "runner beforeEach1", + "runner beforeEach2", + "beforeEach1", + "beforeEach2", + "outer it 1", + "afterEach2", + "afterEach1", + "runner afterEach2", + "runner afterEach1" + ]; + expect(actions).toEqual(expected); + }); + it("builds up nested names", function() { var nestedSpec; env.describe('Test Subject', function() { diff --git a/src/Runner.js b/src/Runner.js index ee850d7..dc8c0be 100644 --- a/src/Runner.js +++ b/src/Runner.js @@ -25,12 +25,12 @@ jasmine.Runner.prototype.execute = function() { jasmine.Runner.prototype.beforeEach = function(beforeEachFunction) { beforeEachFunction.typeName = 'beforeEach'; - this.before_.push(beforeEachFunction); + this.before_.unshift(beforeEachFunction); }; jasmine.Runner.prototype.afterEach = function(afterEachFunction) { afterEachFunction.typeName = 'afterEach'; - this.after_.push(afterEachFunction); + this.after_.unshift(afterEachFunction); }; diff --git a/src/Suite.js b/src/Suite.js index 6d7698a..4ea1de3 100644 --- a/src/Suite.js +++ b/src/Suite.js @@ -37,12 +37,12 @@ jasmine.Suite.prototype.finish = function(onComplete) { jasmine.Suite.prototype.beforeEach = function(beforeEachFunction) { beforeEachFunction.typeName = 'beforeEach'; - this.before_.push(beforeEachFunction); + this.before_.unshift(beforeEachFunction); }; jasmine.Suite.prototype.afterEach = function(afterEachFunction) { afterEachFunction.typeName = 'afterEach'; - this.after_.push(afterEachFunction); + this.after_.unshift(afterEachFunction); }; jasmine.Suite.prototype.results = function() {