runner-wide beforeEach, afterEach support

This commit is contained in:
ragaskar 2009-09-28 16:23:21 -07:00
parent 2588368231
commit a394b564f4
26 changed files with 1659 additions and 1436 deletions

View File

@ -268,6 +268,25 @@ A suite can have a beforeEach declaration. It takes a function that is run befor
}; };
}); });
A runner can also have beforeEach declarations. Runner beforeEach functions are executed before every spec in all suites, and execute BEFORE suite beforeEach functions. For example:
var runnerWideFoo = [];
beforeEach(function () {
runnerWideFoo.push('runner');
});
describe('some suite', function () {
beforeEach(function () {
runnerWideFoo.push('suite');
}
it('should equal bar', function () {
expect(runnerWideFoo).toEqual(['runner', 'suite']);
};
});
#### afterEach #### afterEach
Similarly, there is an afterEach declaration. It takes a function that is run after each spec. For example: Similarly, there is an afterEach declaration. It takes a function that is run after each spec. For example:
@ -284,7 +303,30 @@ Similarly, there is an afterEach declaration. It takes a function that is run a
}; };
it('should equal 0 after', function () { it('should equal 0 after', function () {
expect(suiteWideFoo).toEqual(0); expect(suiteWideFoo).toEqual(0);
};
});
A runner can also have an afterEach declarations. Runner afterEach functions are executed after every spec in all suites, and execute AFTER suite afterEach functions. For example:
var runnerWideFoo = [];
afterEach(function () {
runnerWideFoo.push('runner');
});
describe('some suite', function () {
afterEach(function () {
runnerWideFoo.push('suite');
}
it('should be empty', function () {
expect(runnerWideFoo).toEqual([]);
};
it('should be populated after', function () {
expect(runnerWideFoo).toEqual(['suite', 'runner']);
}; };
}); });

View File

@ -258,7 +258,7 @@ ul.inheritsList
</div> </div>
<div class="fineprint" style="clear:both"> <div class="fineprint" style="clear:both">
Documentation generated by <a href="http://www.jsdoctoolkit.org/" target="_blankt">JsDoc Toolkit</a> 2.1.0 on Mon Sep 28 2009 11:12:11 GMT-0700 (PDT) Documentation generated by <a href="http://www.jsdoctoolkit.org/" target="_blankt">JsDoc Toolkit</a> 2.1.0 on Mon Sep 28 2009 16:21:33 GMT-0700 (PDT)
</div> </div>
</body> </body>
</html> </html>

View File

@ -300,7 +300,7 @@ ul.inheritsList
</div> </div>
<div class="fineprint" style="clear:both"> <div class="fineprint" style="clear:both">
Documentation generated by <a href="http://www.jsdoctoolkit.org/" target="_blankt">JsDoc Toolkit</a> 2.1.0 on Mon Sep 28 2009 11:12:11 GMT-0700 (PDT) Documentation generated by <a href="http://www.jsdoctoolkit.org/" target="_blankt">JsDoc Toolkit</a> 2.1.0 on Mon Sep 28 2009 16:21:33 GMT-0700 (PDT)
</div> </div>
</body> </body>
</html> </html>

View File

@ -908,7 +908,7 @@ A convenience method that allows existing specs to be disabled temporarily durin
<!-- ============================== footer ================================= --> <!-- ============================== footer ================================= -->
<div class="fineprint" style="clear:both"> <div class="fineprint" style="clear:both">
Documentation generated by <a href="http://www.jsdoctoolkit.org/" target="_blank">JsDoc Toolkit</a> 2.1.0 on Mon Sep 28 2009 11:12:11 GMT-0700 (PDT) Documentation generated by <a href="http://www.jsdoctoolkit.org/" target="_blank">JsDoc Toolkit</a> 2.1.0 on Mon Sep 28 2009 16:21:32 GMT-0700 (PDT)
</div> </div>
</body> </body>
</html> </html>

View File

@ -339,7 +339,7 @@ ul.inheritsList
<!-- ============================== footer ================================= --> <!-- ============================== footer ================================= -->
<div class="fineprint" style="clear:both"> <div class="fineprint" style="clear:both">
Documentation generated by <a href="http://www.jsdoctoolkit.org/" target="_blank">JsDoc Toolkit</a> 2.1.0 on Mon Sep 28 2009 11:12:11 GMT-0700 (PDT) Documentation generated by <a href="http://www.jsdoctoolkit.org/" target="_blank">JsDoc Toolkit</a> 2.1.0 on Mon Sep 28 2009 16:21:32 GMT-0700 (PDT)
</div> </div>
</body> </body>
</html> </html>

View File

@ -507,7 +507,7 @@ ul.inheritsList
<!-- ============================== footer ================================= --> <!-- ============================== footer ================================= -->
<div class="fineprint" style="clear:both"> <div class="fineprint" style="clear:both">
Documentation generated by <a href="http://www.jsdoctoolkit.org/" target="_blank">JsDoc Toolkit</a> 2.1.0 on Mon Sep 28 2009 11:12:11 GMT-0700 (PDT) Documentation generated by <a href="http://www.jsdoctoolkit.org/" target="_blank">JsDoc Toolkit</a> 2.1.0 on Mon Sep 28 2009 16:21:32 GMT-0700 (PDT)
</div> </div>
</body> </body>
</html> </html>

View File

@ -316,7 +316,7 @@ ul.inheritsList
<!-- ============================== footer ================================= --> <!-- ============================== footer ================================= -->
<div class="fineprint" style="clear:both"> <div class="fineprint" style="clear:both">
Documentation generated by <a href="http://www.jsdoctoolkit.org/" target="_blank">JsDoc Toolkit</a> 2.1.0 on Mon Sep 28 2009 11:12:11 GMT-0700 (PDT) Documentation generated by <a href="http://www.jsdoctoolkit.org/" target="_blank">JsDoc Toolkit</a> 2.1.0 on Mon Sep 28 2009 16:21:32 GMT-0700 (PDT)
</div> </div>
</body> </body>
</html> </html>

View File

@ -316,7 +316,7 @@ ul.inheritsList
<!-- ============================== footer ================================= --> <!-- ============================== footer ================================= -->
<div class="fineprint" style="clear:both"> <div class="fineprint" style="clear:both">
Documentation generated by <a href="http://www.jsdoctoolkit.org/" target="_blank">JsDoc Toolkit</a> 2.1.0 on Mon Sep 28 2009 11:12:11 GMT-0700 (PDT) Documentation generated by <a href="http://www.jsdoctoolkit.org/" target="_blank">JsDoc Toolkit</a> 2.1.0 on Mon Sep 28 2009 16:21:32 GMT-0700 (PDT)
</div> </div>
</body> </body>
</html> </html>

View File

@ -700,7 +700,7 @@ ul.inheritsList
<!-- ============================== footer ================================= --> <!-- ============================== footer ================================= -->
<div class="fineprint" style="clear:both"> <div class="fineprint" style="clear:both">
Documentation generated by <a href="http://www.jsdoctoolkit.org/" target="_blank">JsDoc Toolkit</a> 2.1.0 on Mon Sep 28 2009 11:12:11 GMT-0700 (PDT) Documentation generated by <a href="http://www.jsdoctoolkit.org/" target="_blank">JsDoc Toolkit</a> 2.1.0 on Mon Sep 28 2009 16:21:32 GMT-0700 (PDT)
</div> </div>
</body> </body>
</html> </html>

View File

@ -316,7 +316,7 @@ ul.inheritsList
<!-- ============================== footer ================================= --> <!-- ============================== footer ================================= -->
<div class="fineprint" style="clear:both"> <div class="fineprint" style="clear:both">
Documentation generated by <a href="http://www.jsdoctoolkit.org/" target="_blank">JsDoc Toolkit</a> 2.1.0 on Mon Sep 28 2009 11:12:11 GMT-0700 (PDT) Documentation generated by <a href="http://www.jsdoctoolkit.org/" target="_blank">JsDoc Toolkit</a> 2.1.0 on Mon Sep 28 2009 16:21:33 GMT-0700 (PDT)
</div> </div>
</body> </body>
</html> </html>

View File

@ -418,7 +418,7 @@ ul.inheritsList
<!-- ============================== footer ================================= --> <!-- ============================== footer ================================= -->
<div class="fineprint" style="clear:both"> <div class="fineprint" style="clear:both">
Documentation generated by <a href="http://www.jsdoctoolkit.org/" target="_blank">JsDoc Toolkit</a> 2.1.0 on Mon Sep 28 2009 11:12:11 GMT-0700 (PDT) Documentation generated by <a href="http://www.jsdoctoolkit.org/" target="_blank">JsDoc Toolkit</a> 2.1.0 on Mon Sep 28 2009 16:21:33 GMT-0700 (PDT)
</div> </div>
</body> </body>
</html> </html>

View File

@ -396,7 +396,7 @@ ul.inheritsList
<!-- ============================== footer ================================= --> <!-- ============================== footer ================================= -->
<div class="fineprint" style="clear:both"> <div class="fineprint" style="clear:both">
Documentation generated by <a href="http://www.jsdoctoolkit.org/" target="_blank">JsDoc Toolkit</a> 2.1.0 on Mon Sep 28 2009 11:12:11 GMT-0700 (PDT) Documentation generated by <a href="http://www.jsdoctoolkit.org/" target="_blank">JsDoc Toolkit</a> 2.1.0 on Mon Sep 28 2009 16:21:33 GMT-0700 (PDT)
</div> </div>
</body> </body>
</html> </html>

View File

@ -845,7 +845,7 @@ expect(foo.bar.callCount).toEqual(0);</pre>
<!-- ============================== footer ================================= --> <!-- ============================== footer ================================= -->
<div class="fineprint" style="clear:both"> <div class="fineprint" style="clear:both">
Documentation generated by <a href="http://www.jsdoctoolkit.org/" target="_blank">JsDoc Toolkit</a> 2.1.0 on Mon Sep 28 2009 11:12:11 GMT-0700 (PDT) Documentation generated by <a href="http://www.jsdoctoolkit.org/" target="_blank">JsDoc Toolkit</a> 2.1.0 on Mon Sep 28 2009 16:21:33 GMT-0700 (PDT)
</div> </div>
</body> </body>
</html> </html>

View File

@ -436,7 +436,7 @@ ul.inheritsList
<!-- ============================== footer ================================= --> <!-- ============================== footer ================================= -->
<div class="fineprint" style="clear:both"> <div class="fineprint" style="clear:both">
Documentation generated by <a href="http://www.jsdoctoolkit.org/" target="_blank">JsDoc Toolkit</a> 2.1.0 on Mon Sep 28 2009 11:12:11 GMT-0700 (PDT) Documentation generated by <a href="http://www.jsdoctoolkit.org/" target="_blank">JsDoc Toolkit</a> 2.1.0 on Mon Sep 28 2009 16:21:33 GMT-0700 (PDT)
</div> </div>
</body> </body>
</html> </html>

View File

@ -678,7 +678,7 @@ Jasmine environment.
<!-- ============================== footer ================================= --> <!-- ============================== footer ================================= -->
<div class="fineprint" style="clear:both"> <div class="fineprint" style="clear:both">
Documentation generated by <a href="http://www.jsdoctoolkit.org/" target="_blank">JsDoc Toolkit</a> 2.1.0 on Mon Sep 28 2009 11:12:11 GMT-0700 (PDT) Documentation generated by <a href="http://www.jsdoctoolkit.org/" target="_blank">JsDoc Toolkit</a> 2.1.0 on Mon Sep 28 2009 16:21:32 GMT-0700 (PDT)
</div> </div>
</body> </body>
</html> </html>

View File

@ -316,7 +316,7 @@ ul.inheritsList
<!-- ============================== footer ================================= --> <!-- ============================== footer ================================= -->
<div class="fineprint" style="clear:both"> <div class="fineprint" style="clear:both">
Documentation generated by <a href="http://www.jsdoctoolkit.org/" target="_blank">JsDoc Toolkit</a> 2.1.0 on Mon Sep 28 2009 11:12:11 GMT-0700 (PDT) Documentation generated by <a href="http://www.jsdoctoolkit.org/" target="_blank">JsDoc Toolkit</a> 2.1.0 on Mon Sep 28 2009 16:21:33 GMT-0700 (PDT)
</div> </div>
</body> </body>
</html> </html>

File diff suppressed because it is too large Load Diff

View File

@ -517,7 +517,7 @@ jasmine.version_= {
"major": 0, "major": 0,
"minor": 9, "minor": 9,
"build": 0, "build": 0,
"revision": 1254161532 "revision": 1254180093
}; };
/** /**
* @namespace * @namespace
@ -586,7 +586,7 @@ jasmine.util.argsToArray = function(args) {
jasmine.Env = function() { jasmine.Env = function() {
this.currentSpec = null; this.currentSpec = null;
this.currentSuite = null; this.currentSuite = null;
this.currentRunner = new jasmine.Runner(this); this.currentRunner_ = new jasmine.Runner(this);
this.currentlyRunningTests = false; this.currentlyRunningTests = false;
this.reporter = new jasmine.MultiReporter(); this.reporter = new jasmine.MultiReporter();
@ -644,7 +644,7 @@ jasmine.Env.prototype.addReporter = function(reporter) {
}; };
jasmine.Env.prototype.execute = function() { jasmine.Env.prototype.execute = function() {
this.currentRunner.execute(); this.currentRunner_.execute();
}; };
jasmine.Env.prototype.describe = function(description, specDefinitions) { jasmine.Env.prototype.describe = function(description, specDefinitions) {
@ -654,7 +654,7 @@ jasmine.Env.prototype.describe = function(description, specDefinitions) {
if (parentSuite) { if (parentSuite) {
parentSuite.add(suite); parentSuite.add(suite);
} else { } else {
this.currentRunner.add(suite); this.currentRunner_.add(suite);
} }
this.currentSuite = suite; this.currentSuite = suite;
@ -667,11 +667,24 @@ jasmine.Env.prototype.describe = function(description, specDefinitions) {
}; };
jasmine.Env.prototype.beforeEach = function(beforeEachFunction) { jasmine.Env.prototype.beforeEach = function(beforeEachFunction) {
this.currentSuite.beforeEach(beforeEachFunction); if (this.currentSuite) {
this.currentSuite.beforeEach(beforeEachFunction);
} else {
this.currentRunner_.beforeEach(beforeEachFunction);
}
};
jasmine.Env.prototype.currentRunner = function () {
return this.currentRunner_;
}; };
jasmine.Env.prototype.afterEach = function(afterEachFunction) { jasmine.Env.prototype.afterEach = function(afterEachFunction) {
this.currentSuite.afterEach(afterEachFunction); if (this.currentSuite) {
this.currentSuite.afterEach(afterEachFunction);
} else {
this.currentRunner_.afterEach(afterEachFunction);
}
}; };
jasmine.Env.prototype.xdescribe = function(desc, specDefinitions) { jasmine.Env.prototype.xdescribe = function(desc, specDefinitions) {
@ -1548,6 +1561,8 @@ jasmine.Runner = function(env) {
var self = this; var self = this;
self.env = env; self.env = env;
self.queue = new jasmine.Queue(env); self.queue = new jasmine.Queue(env);
self.before_ = [];
self.after_ = [];
self.suites_ = []; self.suites_ = [];
}; };
@ -1561,6 +1576,17 @@ jasmine.Runner.prototype.execute = function() {
}); });
}; };
jasmine.Runner.prototype.beforeEach = function(beforeEachFunction) {
beforeEachFunction.typeName = 'beforeEach';
this.before_.push(beforeEachFunction);
};
jasmine.Runner.prototype.afterEach = function(afterEachFunction) {
afterEachFunction.typeName = 'afterEach';
this.after_.push(afterEachFunction);
};
jasmine.Runner.prototype.finishCallback = function() { jasmine.Runner.prototype.finishCallback = function() {
this.env.reporter.reportRunnerResults(this); this.env.reporter.reportRunnerResults(this);
}; };
@ -1605,10 +1631,12 @@ jasmine.Runner.prototype.getResults = function() {
jasmine.Spec = function(env, suite, description) { jasmine.Spec = function(env, suite, description) {
if (!env) { if (!env) {
throw new Error('jasmine.Env() required'); throw new Error('jasmine.Env() required');
}; }
;
if (!suite) { if (!suite) {
throw new Error('jasmine.Suite() required'); throw new Error('jasmine.Suite() required');
}; }
;
var spec = this; var spec = this;
spec.id = env.nextSpecId ? env.nextSpecId() : null; spec.id = env.nextSpecId ? env.nextSpecId() : null;
spec.env = env; spec.env = env;
@ -1714,10 +1742,10 @@ jasmine.Spec.prototype.finish = function(onComplete) {
jasmine.Spec.prototype.after = function(doAfter, test) { jasmine.Spec.prototype.after = function(doAfter, test) {
if (this.queue.isRunning()) { if (this.queue.isRunning()) {
this.queue.add(new jasmine.Block(this.env, doAfter, this)); this.queue.add(new jasmine.Block(this.env, doAfter, this));
} else { } else {
this.afterCallbacks.unshift(doAfter); this.afterCallbacks.unshift(doAfter);
} }
}; };
@ -1742,21 +1770,26 @@ jasmine.Spec.prototype.execute = function(onComplete) {
}; };
jasmine.Spec.prototype.addBeforesAndAftersToQueue = function() { jasmine.Spec.prototype.addBeforesAndAftersToQueue = function() {
var runner = this.env.currentRunner();
for (var suite = this.suite; suite; suite = suite.parentSuite) { for (var suite = this.suite; suite; suite = suite.parentSuite) {
if (suite.beforeQueue) { for (var i = 0; i < suite.before_.length; i++) {
for (var i = 0; i < suite.beforeQueue.length; i++) this.queue.addBefore(new jasmine.Block(this.env, suite.before_[i], this));
this.queue.addBefore(new jasmine.Block(this.env, suite.beforeQueue[i], this));
} }
} }
for (var i = 0; i < runner.before_.length; i++) {
this.queue.addBefore(new jasmine.Block(this.env, runner.before_[i], this));
}
for (i = 0; i < this.afterCallbacks.length; i++) { for (i = 0; i < this.afterCallbacks.length; i++) {
this.queue.add(new jasmine.Block(this.env, this.afterCallbacks[i], this)); this.queue.add(new jasmine.Block(this.env, this.afterCallbacks[i], this));
} }
for (suite = this.suite; suite; suite = suite.parentSuite) { for (suite = this.suite; suite; suite = suite.parentSuite) {
if (suite.afterQueue) { for (var i = 0; i < suite.after_.length; i++) {
for (var j = 0; j < suite.afterQueue.length; j++) this.queue.add(new jasmine.Block(this.env, suite.after_[i], this));
this.queue.add(new jasmine.Block(this.env, suite.afterQueue[j], this));
} }
} }
for (var i = 0; i < runner.after_.length; i++) {
this.queue.add(new jasmine.Block(this.env, runner.after_[i], this));
}
}; };
jasmine.Spec.prototype.explodes = function() { jasmine.Spec.prototype.explodes = function() {
@ -1812,8 +1845,8 @@ jasmine.Suite = function(env, description, specDefinitions, parentSuite) {
self.queue = new jasmine.Queue(env); self.queue = new jasmine.Queue(env);
self.parentSuite = parentSuite; self.parentSuite = parentSuite;
self.env = env; self.env = env;
self.beforeQueue = []; self.before_ = [];
self.afterQueue = []; self.after_ = [];
self.specs_ = []; self.specs_ = [];
}; };
@ -1835,12 +1868,12 @@ jasmine.Suite.prototype.finish = function(onComplete) {
jasmine.Suite.prototype.beforeEach = function(beforeEachFunction) { jasmine.Suite.prototype.beforeEach = function(beforeEachFunction) {
beforeEachFunction.typeName = 'beforeEach'; beforeEachFunction.typeName = 'beforeEach';
this.beforeQueue.push(beforeEachFunction); this.before_.push(beforeEachFunction);
}; };
jasmine.Suite.prototype.afterEach = function(afterEachFunction) { jasmine.Suite.prototype.afterEach = function(afterEachFunction) {
afterEachFunction.typeName = 'afterEach'; afterEachFunction.typeName = 'afterEach';
this.afterQueue.push(afterEachFunction); this.after_.push(afterEachFunction);
}; };
/** @deprecated */ /** @deprecated */
@ -1854,7 +1887,7 @@ jasmine.Suite.prototype.results = function() {
jasmine.Suite.prototype.add = function(block) { jasmine.Suite.prototype.add = function(block) {
if (block instanceof jasmine.Suite) { if (block instanceof jasmine.Suite) {
this.env.currentRunner.addSuite(block); this.env.currentRunner().addSuite(block);
} else { } else {
this.specs_.push(block); this.specs_.push(block);
} }

View File

@ -72,7 +72,7 @@ describe('Exceptions:', function() {
}); });
}); });
var runner = env.currentRunner; var runner = env.currentRunner();
suite.execute(); suite.execute();
fakeTimer.tick(2500); fakeTimer.tick(2500);

View File

@ -49,7 +49,7 @@ describe('jasmine.Reporter', function() {
runnerCallback: runnerCallback runnerCallback: runnerCallback
}); });
var runner = env.currentRunner; var runner = env.currentRunner();
runner.execute(); runner.execute();
expect(foo).toEqual(3); // 'foo was expected to be 3, was ' + foo); expect(foo).toEqual(3); // 'foo was expected to be 3, was ' + foo);

View File

@ -12,6 +12,70 @@ describe('RunnerTest', function() {
env.clearInterval = fakeTimer.clearInterval; env.clearInterval = fakeTimer.clearInterval;
}); });
describe('beforeEach', function() {
it('should run before each spec for all suites', function () {
var foo;
env.beforeEach(function () {
foo = 0;
});
env.describe('suite 1', function () {
env.it('test 1-1', function() {
foo++;
this.expect(foo).toEqual(1);
});
env.it('test 1-2', function() {
foo++;
this.expect(foo).toEqual(1);
});
});
env.describe('suite 2', function () {
env.it('test 2-1', function() {
foo++;
this.expect(foo).toEqual(1);
});
});
env.currentRunner().execute();
var runnerResults = env.currentRunner().results();
expect(runnerResults.totalCount).toEqual(3);
expect(runnerResults.passedCount).toEqual(3);
});
});
describe('afterEach', function() {
it('should run after each spec for all suites', function () {
var foo = 3;
env.afterEach(function () {
foo = foo - 1;
});
env.describe('suite 1', function () {
env.it('test 1-1', function() {
this.expect(foo).toEqual(3);
});
env.it('test 1-2', function() {
this.expect(foo).toEqual(2);
});
});
env.describe('suite 2', function () {
env.it('test 2-1', function() {
this.expect(foo).toEqual(1);
});
});
env.currentRunner().execute();
var runnerResults = env.currentRunner().results();
expect(runnerResults.totalCount).toEqual(3);
expect(runnerResults.passedCount).toEqual(3);
});
});
it('should run child suites and specs and generate results when execute is called', function() { it('should run child suites and specs and generate results when execute is called', function() {
env.describe('one suite description', function () { env.describe('one suite description', function () {
env.it('should be a test', function() { env.it('should be a test', function() {
@ -29,9 +93,9 @@ describe('RunnerTest', function() {
}); });
}); });
env.currentRunner.execute(); env.currentRunner().execute();
var runnerResults = env.currentRunner.results(); var runnerResults = env.currentRunner().results();
expect(runnerResults.totalCount).toEqual(2); expect(runnerResults.totalCount).toEqual(2);
expect(runnerResults.passedCount).toEqual(1); expect(runnerResults.passedCount).toEqual(1);
expect(runnerResults.failedCount).toEqual(1); expect(runnerResults.failedCount).toEqual(1);
@ -55,9 +119,9 @@ describe('RunnerTest', function() {
}); });
}); });
env.currentRunner.execute(); env.currentRunner().execute();
var runnerResults = env.currentRunner.results(); var runnerResults = env.currentRunner().results();
expect(runnerResults.totalCount).toEqual(1); expect(runnerResults.totalCount).toEqual(1);
expect(runnerResults.passedCount).toEqual(0); expect(runnerResults.passedCount).toEqual(0);
expect(runnerResults.failedCount).toEqual(1); expect(runnerResults.failedCount).toEqual(1);
@ -80,9 +144,9 @@ describe('RunnerTest', function() {
}); });
}); });
env.currentRunner.execute(); env.currentRunner().execute();
var results = env.currentRunner.results(); var results = env.currentRunner().results();
expect(results.totalCount).toEqual(2); expect(results.totalCount).toEqual(2);
expect(results.passedCount).toEqual(1); expect(results.passedCount).toEqual(1);
expect(results.failedCount).toEqual(1); expect(results.failedCount).toEqual(1);
@ -113,16 +177,16 @@ describe('RunnerTest', function() {
}); });
}); });
env.currentRunner.execute(); env.currentRunner().execute();
expect(fakeReporter.reportRunnerResults).wasNotCalled(); expect(fakeReporter.reportRunnerResults).wasNotCalled();
fakeTimer.tick(200); fakeTimer.tick(200);
//This blows up the JSApiReporter. //This blows up the JSApiReporter.
//expect(fakeReporter.reportRunnerResults).wasCalledWith(env.currentRunner); //expect(fakeReporter.reportRunnerResults).wasCalledWith(env.currentRunner);
expect(fakeReporter.reportRunnerResults).wasCalled(); expect(fakeReporter.reportRunnerResults).wasCalled();
expect(fakeReporter.reportRunnerResults.mostRecentCall.args[0].results()).toEqual(env.currentRunner.results()); expect(fakeReporter.reportRunnerResults.mostRecentCall.args[0].results()).toEqual(env.currentRunner().results());
}); });
}); });
it("should report when the tests start running", function() { it("should report when the tests start running", function() {
@ -139,18 +203,19 @@ describe('RunnerTest', function() {
var reportedRunner = fakeReporter.reportRunnerStarting.mostRecentCall.args[0]; var reportedRunner = fakeReporter.reportRunnerStarting.mostRecentCall.args[0];
expect(reportedRunner.arbitraryVariable).toEqual('foo'); expect(reportedRunner.arbitraryVariable).toEqual('foo');
expect(runner.queue.start).wasCalled(); expect(runner.queue.start).wasCalled();
}); });
it("should return a flat array of all suites, including nested suites", function() { it("should return a flat array of all suites, including nested suites", function() {
var suite1, suite2; var suite1, suite2;
suite1 = env.describe("spec 1", function() { suite1 = env.describe("spec 1", function() {
suite2 = env.describe("nested spec", function() {}); suite2 = env.describe("nested spec", function() {
});
}); });
document.runner = env.currentRunner; document.runner = env.currentRunner();
var suites = env.currentRunner.suites(); var suites = env.currentRunner().suites();
var suiteDescriptions = []; var suiteDescriptions = [];
for (var i = 0; i < suites.length; i++) { for (var i = 0; i < suites.length; i++) {
suiteDescriptions.push(suites[i].getFullName()); suiteDescriptions.push(suites[i].getFullName());

View File

@ -864,6 +864,14 @@ describe("jasmine spec running", function () {
it("testNestedDescribes", function() { it("testNestedDescribes", function() {
var actions = []; var actions = [];
env.beforeEach(function () {
actions.push('runner beforeEach');
});
env.afterEach(function () {
actions.push('runner afterEach');
});
env.describe('Something', function() { env.describe('Something', function() {
env.beforeEach(function() { env.beforeEach(function() {
actions.push('outer beforeEach'); actions.push('outer beforeEach');
@ -914,27 +922,35 @@ describe("jasmine spec running", function () {
var expected = [ var expected = [
"runner beforeEach",
"outer beforeEach", "outer beforeEach",
"outer it 1", "outer it 1",
"outer afterEach", "outer afterEach",
"runner afterEach",
"runner beforeEach",
"outer beforeEach", "outer beforeEach",
"inner 1 beforeEach", "inner 1 beforeEach",
"inner 1 it", "inner 1 it",
"inner 1 afterEach", "inner 1 afterEach",
"outer afterEach", "outer afterEach",
"runner afterEach",
"runner beforeEach",
"outer beforeEach", "outer beforeEach",
"outer it 2", "outer it 2",
"outer afterEach", "outer afterEach",
"runner afterEach",
"runner beforeEach",
"outer beforeEach", "outer beforeEach",
"inner 2 beforeEach", "inner 2 beforeEach",
"inner 2 it", "inner 2 it",
"inner 2 afterEach", "inner 2 afterEach",
"outer afterEach" "outer afterEach",
"runner afterEach"
]; ];
expect(env.equals_(actions, expected)).toEqual(true); expect(actions).toEqual(expected);
}); });
it("builds up nested names", function() { it("builds up nested names", function() {
@ -963,7 +979,8 @@ describe("jasmine spec running", function () {
}); });
}); });
env.describe('EmptySuite', function() {}); env.describe('EmptySuite', function() {
});
env.describe('NonEmptySuite2', function() { env.describe('NonEmptySuite2', function() {
env.it('should pass', function() { env.it('should pass', function() {
@ -973,7 +990,7 @@ describe("jasmine spec running", function () {
env.execute(); env.execute();
var runnerResults = env.currentRunner.results(); var runnerResults = env.currentRunner_.results();
expect(runnerResults.totalCount).toEqual(3); expect(runnerResults.totalCount).toEqual(3);
expect(runnerResults.passedCount).toEqual(3); expect(runnerResults.passedCount).toEqual(3);
expect(runnerResults.failedCount).toEqual(0); expect(runnerResults.failedCount).toEqual(0);

View File

@ -6,7 +6,7 @@
jasmine.Env = function() { jasmine.Env = function() {
this.currentSpec = null; this.currentSpec = null;
this.currentSuite = null; this.currentSuite = null;
this.currentRunner = new jasmine.Runner(this); this.currentRunner_ = new jasmine.Runner(this);
this.currentlyRunningTests = false; this.currentlyRunningTests = false;
this.reporter = new jasmine.MultiReporter(); this.reporter = new jasmine.MultiReporter();
@ -64,7 +64,7 @@ jasmine.Env.prototype.addReporter = function(reporter) {
}; };
jasmine.Env.prototype.execute = function() { jasmine.Env.prototype.execute = function() {
this.currentRunner.execute(); this.currentRunner_.execute();
}; };
jasmine.Env.prototype.describe = function(description, specDefinitions) { jasmine.Env.prototype.describe = function(description, specDefinitions) {
@ -74,7 +74,7 @@ jasmine.Env.prototype.describe = function(description, specDefinitions) {
if (parentSuite) { if (parentSuite) {
parentSuite.add(suite); parentSuite.add(suite);
} else { } else {
this.currentRunner.add(suite); this.currentRunner_.add(suite);
} }
this.currentSuite = suite; this.currentSuite = suite;
@ -87,11 +87,24 @@ jasmine.Env.prototype.describe = function(description, specDefinitions) {
}; };
jasmine.Env.prototype.beforeEach = function(beforeEachFunction) { jasmine.Env.prototype.beforeEach = function(beforeEachFunction) {
this.currentSuite.beforeEach(beforeEachFunction); if (this.currentSuite) {
this.currentSuite.beforeEach(beforeEachFunction);
} else {
this.currentRunner_.beforeEach(beforeEachFunction);
}
};
jasmine.Env.prototype.currentRunner = function () {
return this.currentRunner_;
}; };
jasmine.Env.prototype.afterEach = function(afterEachFunction) { jasmine.Env.prototype.afterEach = function(afterEachFunction) {
this.currentSuite.afterEach(afterEachFunction); if (this.currentSuite) {
this.currentSuite.afterEach(afterEachFunction);
} else {
this.currentRunner_.afterEach(afterEachFunction);
}
}; };
jasmine.Env.prototype.xdescribe = function(desc, specDefinitions) { jasmine.Env.prototype.xdescribe = function(desc, specDefinitions) {

View File

@ -8,6 +8,8 @@ jasmine.Runner = function(env) {
var self = this; var self = this;
self.env = env; self.env = env;
self.queue = new jasmine.Queue(env); self.queue = new jasmine.Queue(env);
self.before_ = [];
self.after_ = [];
self.suites_ = []; self.suites_ = [];
}; };
@ -21,6 +23,17 @@ jasmine.Runner.prototype.execute = function() {
}); });
}; };
jasmine.Runner.prototype.beforeEach = function(beforeEachFunction) {
beforeEachFunction.typeName = 'beforeEach';
this.before_.push(beforeEachFunction);
};
jasmine.Runner.prototype.afterEach = function(afterEachFunction) {
afterEachFunction.typeName = 'afterEach';
this.after_.push(afterEachFunction);
};
jasmine.Runner.prototype.finishCallback = function() { jasmine.Runner.prototype.finishCallback = function() {
this.env.reporter.reportRunnerResults(this); this.env.reporter.reportRunnerResults(this);
}; };

View File

@ -9,10 +9,12 @@
jasmine.Spec = function(env, suite, description) { jasmine.Spec = function(env, suite, description) {
if (!env) { if (!env) {
throw new Error('jasmine.Env() required'); throw new Error('jasmine.Env() required');
}; }
;
if (!suite) { if (!suite) {
throw new Error('jasmine.Suite() required'); throw new Error('jasmine.Suite() required');
}; }
;
var spec = this; var spec = this;
spec.id = env.nextSpecId ? env.nextSpecId() : null; spec.id = env.nextSpecId ? env.nextSpecId() : null;
spec.env = env; spec.env = env;
@ -118,10 +120,10 @@ jasmine.Spec.prototype.finish = function(onComplete) {
jasmine.Spec.prototype.after = function(doAfter, test) { jasmine.Spec.prototype.after = function(doAfter, test) {
if (this.queue.isRunning()) { if (this.queue.isRunning()) {
this.queue.add(new jasmine.Block(this.env, doAfter, this)); this.queue.add(new jasmine.Block(this.env, doAfter, this));
} else { } else {
this.afterCallbacks.unshift(doAfter); this.afterCallbacks.unshift(doAfter);
} }
}; };
@ -146,21 +148,26 @@ jasmine.Spec.prototype.execute = function(onComplete) {
}; };
jasmine.Spec.prototype.addBeforesAndAftersToQueue = function() { jasmine.Spec.prototype.addBeforesAndAftersToQueue = function() {
var runner = this.env.currentRunner();
for (var suite = this.suite; suite; suite = suite.parentSuite) { for (var suite = this.suite; suite; suite = suite.parentSuite) {
if (suite.beforeQueue) { for (var i = 0; i < suite.before_.length; i++) {
for (var i = 0; i < suite.beforeQueue.length; i++) this.queue.addBefore(new jasmine.Block(this.env, suite.before_[i], this));
this.queue.addBefore(new jasmine.Block(this.env, suite.beforeQueue[i], this));
} }
} }
for (var i = 0; i < runner.before_.length; i++) {
this.queue.addBefore(new jasmine.Block(this.env, runner.before_[i], this));
}
for (i = 0; i < this.afterCallbacks.length; i++) { for (i = 0; i < this.afterCallbacks.length; i++) {
this.queue.add(new jasmine.Block(this.env, this.afterCallbacks[i], this)); this.queue.add(new jasmine.Block(this.env, this.afterCallbacks[i], this));
} }
for (suite = this.suite; suite; suite = suite.parentSuite) { for (suite = this.suite; suite; suite = suite.parentSuite) {
if (suite.afterQueue) { for (var i = 0; i < suite.after_.length; i++) {
for (var j = 0; j < suite.afterQueue.length; j++) this.queue.add(new jasmine.Block(this.env, suite.after_[i], this));
this.queue.add(new jasmine.Block(this.env, suite.afterQueue[j], this));
} }
} }
for (var i = 0; i < runner.after_.length; i++) {
this.queue.add(new jasmine.Block(this.env, runner.after_[i], this));
}
}; };
jasmine.Spec.prototype.explodes = function() { jasmine.Spec.prototype.explodes = function() {

View File

@ -14,8 +14,8 @@ jasmine.Suite = function(env, description, specDefinitions, parentSuite) {
self.queue = new jasmine.Queue(env); self.queue = new jasmine.Queue(env);
self.parentSuite = parentSuite; self.parentSuite = parentSuite;
self.env = env; self.env = env;
self.beforeQueue = []; self.before_ = [];
self.afterQueue = []; self.after_ = [];
self.specs_ = []; self.specs_ = [];
}; };
@ -37,12 +37,12 @@ jasmine.Suite.prototype.finish = function(onComplete) {
jasmine.Suite.prototype.beforeEach = function(beforeEachFunction) { jasmine.Suite.prototype.beforeEach = function(beforeEachFunction) {
beforeEachFunction.typeName = 'beforeEach'; beforeEachFunction.typeName = 'beforeEach';
this.beforeQueue.push(beforeEachFunction); this.before_.push(beforeEachFunction);
}; };
jasmine.Suite.prototype.afterEach = function(afterEachFunction) { jasmine.Suite.prototype.afterEach = function(afterEachFunction) {
afterEachFunction.typeName = 'afterEach'; afterEachFunction.typeName = 'afterEach';
this.afterQueue.push(afterEachFunction); this.after_.push(afterEachFunction);
}; };
/** @deprecated */ /** @deprecated */
@ -56,7 +56,7 @@ jasmine.Suite.prototype.results = function() {
jasmine.Suite.prototype.add = function(block) { jasmine.Suite.prototype.add = function(block) {
if (block instanceof jasmine.Suite) { if (block instanceof jasmine.Suite) {
this.env.currentRunner.addSuite(block); this.env.currentRunner().addSuite(block);
} else { } else {
this.specs_.push(block); this.specs_.push(block);
} }