Allow waits/runs in spec.after. spec.after runs in between spec end and afterEach.

This commit is contained in:
ragaskar 2009-08-04 19:24:00 -07:00
parent 8b998749f3
commit 56c26c5b55
3 changed files with 118 additions and 10 deletions

View File

@ -1686,9 +1686,6 @@ jasmine.Spec.prototype.finishCallback = function() {
}; };
jasmine.Spec.prototype.finish = function(onComplete) { jasmine.Spec.prototype.finish = function(onComplete) {
for (var i = 0; i < this.afterCallbacks.length; i++) {
this.afterCallbacks[i]();
}
this.removeAllSpies(); this.removeAllSpies();
this.finishCallback(); this.finishCallback();
if (onComplete) { if (onComplete) {
@ -1696,8 +1693,13 @@ jasmine.Spec.prototype.finish = function(onComplete) {
} }
}; };
jasmine.Spec.prototype.after = function(doAfter) { jasmine.Spec.prototype.after = function(doAfter, test) {
if (this.queue.isRunning()) {
this.queue.add(new jasmine.Block(this.env, doAfter, this));
} else {
this.afterCallbacks.unshift(doAfter); this.afterCallbacks.unshift(doAfter);
}
}; };
jasmine.Spec.prototype.execute = function(onComplete) { jasmine.Spec.prototype.execute = function(onComplete) {
@ -1726,6 +1728,11 @@ jasmine.Spec.prototype.addBeforesAndAftersToQueue = function() {
for (var i = 0; i < suite.beforeQueue.length; i++) for (var i = 0; i < suite.beforeQueue.length; i++)
this.queue.addBefore(new jasmine.Block(this.env, suite.beforeQueue[i], this)); this.queue.addBefore(new jasmine.Block(this.env, suite.beforeQueue[i], this));
} }
}
for (i = 0; i < this.afterCallbacks.length; i++) {
this.queue.add(new jasmine.Block(this.env, this.afterCallbacks[i], this));
}
for (suite = this.suite; suite; suite = suite.parentSuite) {
if (suite.afterQueue) { if (suite.afterQueue) {
for (var j = 0; j < suite.afterQueue.length; j++) for (var j = 0; j < suite.afterQueue.length; j++)
this.queue.add(new jasmine.Block(this.env, suite.afterQueue[j], this)); this.queue.add(new jasmine.Block(this.env, suite.afterQueue[j], this));

View File

@ -675,6 +675,100 @@ describe("jasmine spec running", function () {
}); });
it("Spec#after should be able to eval runs and waits blocks", function () {
var runsBeforeAfter = false;
var firstSpecHasRun = false;
var secondSpecHasRun = false;
var afterHasRun = false;
var suiteWithAfter = env.describe('one suite with a before', function () {
env.it('should be the first spec', function () {
firstSpecHasRun = true;
this.after(function () {
this.waits(500);
this.runs(function () {
afterHasRun = true;
});
this.waits(500);
}, true);
this.waits(500);
this.runs(function () {
runsBeforeAfter = true;
});
});
env.it('should be a spec', function () {
secondSpecHasRun = true;
});
});
expect(firstSpecHasRun).toEqual(false);
expect(runsBeforeAfter).toEqual(false);
expect(afterHasRun).toEqual(false);
expect(secondSpecHasRun).toEqual(false);
suiteWithAfter.execute();
expect(firstSpecHasRun).toEqual(true);
expect(runsBeforeAfter).toEqual(false);
expect(afterHasRun).toEqual(false);
expect(secondSpecHasRun).toEqual(false);
fakeTimer.tick(500);
expect(firstSpecHasRun).toEqual(true);
expect(runsBeforeAfter).toEqual(true);
expect(afterHasRun).toEqual(false);
expect(secondSpecHasRun).toEqual(false);
fakeTimer.tick(500);
expect(firstSpecHasRun).toEqual(true);
expect(runsBeforeAfter).toEqual(true);
expect(afterHasRun).toEqual(true);
expect(secondSpecHasRun).toEqual(false);
fakeTimer.tick(500);
expect(firstSpecHasRun).toEqual(true);
expect(runsBeforeAfter).toEqual(true);
expect(afterHasRun).toEqual(true);
expect(secondSpecHasRun).toEqual(true);
});
it("handles waits", function () {
var firstSpecHasRun = false;
var secondSpecHasRun = false;
var suiteWithAfter = env.describe('one suite with a before', function () {
env.it('should be the first spec', function () {
this.waits(500);
this.runs(function () {
firstSpecHasRun = true;
});
});
env.it('should be a spec', function () {
secondSpecHasRun = true;
});
});
expect(firstSpecHasRun).toEqual(false);
expect(secondSpecHasRun).toEqual(false);
suiteWithAfter.execute();
expect(firstSpecHasRun).toEqual(false);
expect(secondSpecHasRun).toEqual(false);
fakeTimer.tick(500);
expect(firstSpecHasRun).toEqual(true);
expect(secondSpecHasRun).toEqual(true);
});
it("testBeforeExecutesSafely", function() { it("testBeforeExecutesSafely", function() {
var report = ""; var report = "";
var suite = env.describe('before fails on first test, passes on second', function() { var suite = env.describe('before fails on first test, passes on second', function() {

View File

@ -97,9 +97,6 @@ jasmine.Spec.prototype.finishCallback = function() {
}; };
jasmine.Spec.prototype.finish = function(onComplete) { jasmine.Spec.prototype.finish = function(onComplete) {
for (var i = 0; i < this.afterCallbacks.length; i++) {
this.afterCallbacks[i]();
}
this.removeAllSpies(); this.removeAllSpies();
this.finishCallback(); this.finishCallback();
if (onComplete) { if (onComplete) {
@ -107,8 +104,13 @@ jasmine.Spec.prototype.finish = function(onComplete) {
} }
}; };
jasmine.Spec.prototype.after = function(doAfter) { jasmine.Spec.prototype.after = function(doAfter, test) {
if (this.queue.isRunning()) {
this.queue.add(new jasmine.Block(this.env, doAfter, this));
} else {
this.afterCallbacks.unshift(doAfter); this.afterCallbacks.unshift(doAfter);
}
}; };
jasmine.Spec.prototype.execute = function(onComplete) { jasmine.Spec.prototype.execute = function(onComplete) {
@ -137,6 +139,11 @@ jasmine.Spec.prototype.addBeforesAndAftersToQueue = function() {
for (var i = 0; i < suite.beforeQueue.length; i++) for (var i = 0; i < suite.beforeQueue.length; i++)
this.queue.addBefore(new jasmine.Block(this.env, suite.beforeQueue[i], this)); this.queue.addBefore(new jasmine.Block(this.env, suite.beforeQueue[i], this));
} }
}
for (i = 0; i < this.afterCallbacks.length; i++) {
this.queue.add(new jasmine.Block(this.env, this.afterCallbacks[i], this));
}
for (suite = this.suite; suite; suite = suite.parentSuite) {
if (suite.afterQueue) { if (suite.afterQueue) {
for (var j = 0; j < suite.afterQueue.length; j++) for (var j = 0; j < suite.afterQueue.length; j++)
this.queue.add(new jasmine.Block(this.env, suite.afterQueue[j], this)); this.queue.add(new jasmine.Block(this.env, suite.afterQueue[j], this));