diff --git a/lib/jasmine.js b/lib/jasmine.js index 913a777..ada4cd0 100644 --- a/lib/jasmine.js +++ b/lib/jasmine.js @@ -1689,7 +1689,6 @@ jasmine.Spec.prototype.finish = function(onComplete) { for (var i = 0; i < this.afterCallbacks.length; i++) { this.afterCallbacks[i](); } - this.safeExecuteAfters(); this.removeAllSpies(); this.finishCallback(); if (onComplete) { @@ -1701,7 +1700,6 @@ jasmine.Spec.prototype.after = function(doAfter) { this.afterCallbacks.unshift(doAfter); }; - jasmine.Spec.prototype.execute = function(onComplete) { var spec = this; if (!spec.env.specFilter(spec)) { @@ -1714,7 +1712,7 @@ jasmine.Spec.prototype.execute = function(onComplete) { spec.env.currentSpec = spec; spec.env.currentlyRunningTests = true; - spec.safeExecuteBefores(); + spec.addBeforesAndAftersToQueue(); spec.queue.start(function () { spec.finish(onComplete); @@ -1722,34 +1720,17 @@ jasmine.Spec.prototype.execute = function(onComplete) { spec.env.currentlyRunningTests = false; }; -jasmine.Spec.prototype.safeExecuteBefores = function() { +jasmine.Spec.prototype.addBeforesAndAftersToQueue = function() { for (var suite = this.suite; suite; suite = suite.parentSuite) { if (suite.beforeQueue) { for (var i = 0; i < suite.beforeQueue.length; i++) this.queue.addBefore(new jasmine.Block(this.env, suite.beforeQueue[i], this)); } - } -}; - -jasmine.Spec.prototype.safeExecuteAfters = function() { - var afters = []; - for (var suite = this.suite; suite; suite = suite.parentSuite) { if (suite.afterQueue) { - for (var i = 0; i < suite.afterQueue.length; i++) - afters.unshift(suite.afterQueue[i]); + for (var j = 0; j < suite.afterQueue.length; j++) + this.queue.add(new jasmine.Block(this.env, suite.afterQueue[j], this)); } } - while (afters.length) { - this.safeExecuteBeforeOrAfter(afters.pop()); - } -}; - -jasmine.Spec.prototype.safeExecuteBeforeOrAfter = function(func) { - try { - func.apply(this); - } catch (e) { - this.results.addResult(new jasmine.ExpectationResult(false, func.typeName + '() fail: ' + jasmine.util.formatException(e), null)); - } }; jasmine.Spec.prototype.explodes = function() { diff --git a/spec/suites/SpecRunningTest.js b/spec/suites/SpecRunningTest.js index 2a3b583..101b873 100644 --- a/spec/suites/SpecRunningTest.js +++ b/spec/suites/SpecRunningTest.js @@ -630,6 +630,51 @@ describe("jasmine spec running", function () { expect(foo).toEqual(3); }); + it("#afterEach should be able to eval runs and waits blocks", function () { + var foo = 0; + var firstSpecHasRun = false; + var secondSpecHasRun = false; + var suiteWithAfter = env.describe('one suite with a before', function () { + this.afterEach(function () { + this.waits(500); + this.runs(function () { + foo++; + }); + this.waits(500); + }); + + env.it('should be the first spec', function () { + firstSpecHasRun = true; + }); + + env.it('should be a spec', function () { + secondSpecHasRun = true; + foo++; + }); + + }); + + expect(firstSpecHasRun).toEqual(false); + expect(secondSpecHasRun).toEqual(false); + expect(foo).toEqual(0); + + suiteWithAfter.execute(); + + expect(firstSpecHasRun).toEqual(true); + expect(secondSpecHasRun).toEqual(false); + expect(foo).toEqual(0); + + fakeTimer.tick(500); + + expect(foo).toEqual(1); + expect(secondSpecHasRun).toEqual(false); + fakeTimer.tick(500); + + expect(foo).toEqual(2); + expect(secondSpecHasRun).toEqual(true); + + }); + it("testBeforeExecutesSafely", function() { var report = ""; var suite = env.describe('before fails on first test, passes on second', function() { diff --git a/src/Spec.js b/src/Spec.js index 4f0ac94..cd7e1b7 100644 --- a/src/Spec.js +++ b/src/Spec.js @@ -100,7 +100,6 @@ jasmine.Spec.prototype.finish = function(onComplete) { for (var i = 0; i < this.afterCallbacks.length; i++) { this.afterCallbacks[i](); } - this.safeExecuteAfters(); this.removeAllSpies(); this.finishCallback(); if (onComplete) { @@ -112,7 +111,6 @@ jasmine.Spec.prototype.after = function(doAfter) { this.afterCallbacks.unshift(doAfter); }; - jasmine.Spec.prototype.execute = function(onComplete) { var spec = this; if (!spec.env.specFilter(spec)) { @@ -125,7 +123,7 @@ jasmine.Spec.prototype.execute = function(onComplete) { spec.env.currentSpec = spec; spec.env.currentlyRunningTests = true; - spec.safeExecuteBefores(); + spec.addBeforesAndAftersToQueue(); spec.queue.start(function () { spec.finish(onComplete); @@ -133,34 +131,17 @@ jasmine.Spec.prototype.execute = function(onComplete) { spec.env.currentlyRunningTests = false; }; -jasmine.Spec.prototype.safeExecuteBefores = function() { +jasmine.Spec.prototype.addBeforesAndAftersToQueue = function() { for (var suite = this.suite; suite; suite = suite.parentSuite) { if (suite.beforeQueue) { for (var i = 0; i < suite.beforeQueue.length; i++) this.queue.addBefore(new jasmine.Block(this.env, suite.beforeQueue[i], this)); } - } -}; - -jasmine.Spec.prototype.safeExecuteAfters = function() { - var afters = []; - for (var suite = this.suite; suite; suite = suite.parentSuite) { if (suite.afterQueue) { - for (var i = 0; i < suite.afterQueue.length; i++) - afters.unshift(suite.afterQueue[i]); + for (var j = 0; j < suite.afterQueue.length; j++) + this.queue.add(new jasmine.Block(this.env, suite.afterQueue[j], this)); } } - while (afters.length) { - this.safeExecuteBeforeOrAfter(afters.pop()); - } -}; - -jasmine.Spec.prototype.safeExecuteBeforeOrAfter = function(func) { - try { - func.apply(this); - } catch (e) { - this.results.addResult(new jasmine.ExpectationResult(false, func.typeName + '() fail: ' + jasmine.util.formatException(e), null)); - } }; jasmine.Spec.prototype.explodes = function() {