Add config option which stops jasmine from capturing exceptions in a test

This commit is contained in:
Alex Kwiatkowski
2012-04-15 17:34:31 -04:00
parent 7bbcf51d45
commit 2385acedd8
4 changed files with 153 additions and 103 deletions
+15 -3
View File
@@ -39,6 +39,13 @@ jasmine.DEFAULT_UPDATE_INTERVAL = 250;
*/ */
jasmine.DEFAULT_TIMEOUT_INTERVAL = 5000; jasmine.DEFAULT_TIMEOUT_INTERVAL = 5000;
/**
* By default exceptions thrown in the context of a test are caught by jasmine so that it can run the remaining tests in the suite.
* Set to false to let the exception bubble up in the browser.
*
*/
jasmine.CATCH_EXCEPTIONS = true;
jasmine.getGlobal = function() { jasmine.getGlobal = function() {
function getGlobal() { function getGlobal() {
return this; return this;
@@ -1020,10 +1027,15 @@ jasmine.Block = function(env, func, spec) {
}; };
jasmine.Block.prototype.execute = function(onComplete) { jasmine.Block.prototype.execute = function(onComplete) {
try { if (!jasmine.CATCH_EXCEPTIONS) {
this.func.apply(this.spec); this.func.apply(this.spec);
} catch (e) { }
this.spec.fail(e); else {
try {
this.func.apply(this.spec);
} catch (e) {
this.spec.fail(e);
}
} }
onComplete(); onComplete();
}; };
+120 -94
View File
@@ -32,118 +32,144 @@ describe('Exceptions:', function() {
expect(jasmine.util.formatException(sampleWebkitException)).toEqual(expected); expect(jasmine.util.formatException(sampleWebkitException)).toEqual(expected);
}); });
it('should handle exceptions thrown, but continue', function() { describe('with break on exception', function() {
var fakeTimer = new jasmine.FakeTimer(); it('should not catch the exception', function() {
env.setTimeout = fakeTimer.setTimeout; var suite = env.describe('suite for break on exceptions', function() {
env.clearTimeout = fakeTimer.clearTimeout; env.it('should break when an exception is thrown', function() {
env.setInterval = fakeTimer.setInterval; throw new Error('I should hit a breakpoint!');
env.clearInterval = fakeTimer.clearInterval;
//we run two exception tests to make sure we continue after throwing an exception
var suite = env.describe('Suite for handles exceptions', function () {
env.it('should be a test that fails because it throws an exception', function() {
throw new Error('fake error 1');
});
env.it('should be another test that fails because it throws an exception', function() {
this.runs(function () {
throw new Error('fake error 2');
}); });
this.runs(function () { });
var runner = env.currentRunner();
var dont_change = 'I will never change!';
var oldCatch = jasmine.CATCH_EXCEPTIONS;
jasmine.CATCH_EXCEPTIONS = false;
try {
suite.execute();
dont_change = 'oops I changed';
}
catch (e) {}
finally {
jasmine.CATCH_EXCEPTIONS = oldCatch;
}
expect(dont_change).toEqual('I will never change!');
});
});
describe("with catch on exception", function() {
it('should handle exceptions thrown, but continue', function() {
var fakeTimer = new jasmine.FakeTimer();
env.setTimeout = fakeTimer.setTimeout;
env.clearTimeout = fakeTimer.clearTimeout;
env.setInterval = fakeTimer.setInterval;
env.clearInterval = fakeTimer.clearInterval;
//we run two exception tests to make sure we continue after throwing an exception
var suite = env.describe('Suite for handles exceptions', function () {
env.it('should be a test that fails because it throws an exception', function() {
throw new Error('fake error 1');
});
env.it('should be another test that fails because it throws an exception', function() {
this.runs(function () {
throw new Error('fake error 2');
});
this.runs(function () {
this.expect(true).toEqual(true);
});
});
env.it('should be a passing test that runs after exceptions are thrown', function() {
this.expect(true).toEqual(true);
});
env.it('should be another test that fails because it throws an exception after a wait', function() {
this.runs(function () {
var foo = 'foo';
});
this.waits(250);
this.runs(function () {
throw new Error('fake error 3');
});
});
env.it('should be a passing test that runs after exceptions are thrown from a async test', function() {
this.expect(true).toEqual(true); this.expect(true).toEqual(true);
}); });
}); });
env.it('should be a passing test that runs after exceptions are thrown', function() { var runner = env.currentRunner();
this.expect(true).toEqual(true); suite.execute();
}); fakeTimer.tick(2500);
env.it('should be another test that fails because it throws an exception after a wait', function() { var suiteResults = suite.results();
this.runs(function () { var specResults = suiteResults.getItems();
var foo = 'foo';
});
this.waits(250);
this.runs(function () {
throw new Error('fake error 3');
});
});
env.it('should be a passing test that runs after exceptions are thrown from a async test', function() { expect(suiteResults.passed()).toEqual(false);
this.expect(true).toEqual(true); //
}); expect(specResults.length).toEqual(5);
expect(specResults[0].passed()).toMatch(false);
var blockResults = specResults[0].getItems();
expect(blockResults[0].passed()).toEqual(false);
expect(blockResults[0].message).toMatch(/fake error 1/);
expect(specResults[1].passed()).toEqual(false);
blockResults = specResults[1].getItems();
expect(blockResults[0].passed()).toEqual(false);
expect(blockResults[0].message).toMatch(/fake error 2/);
expect(blockResults[1].passed()).toEqual(true);
expect(specResults[2].passed()).toEqual(true);
expect(specResults[3].passed()).toEqual(false);
blockResults = specResults[3].getItems();
expect(blockResults[0].message).toMatch(/fake error 3/);
expect(specResults[4].passed()).toEqual(true);
}); });
var runner = env.currentRunner(); it("should handle exceptions thrown directly in top-level describe blocks and continue", function () {
suite.execute(); var suite = env.describe("a top level describe block that throws an exception", function () {
fakeTimer.tick(2500); env.it("is a test that should pass", function () {
var suiteResults = suite.results();
var specResults = suiteResults.getItems();
expect(suiteResults.passed()).toEqual(false);
//
expect(specResults.length).toEqual(5);
expect(specResults[0].passed()).toMatch(false);
var blockResults = specResults[0].getItems();
expect(blockResults[0].passed()).toEqual(false);
expect(blockResults[0].message).toMatch(/fake error 1/);
expect(specResults[1].passed()).toEqual(false);
blockResults = specResults[1].getItems();
expect(blockResults[0].passed()).toEqual(false);
expect(blockResults[0].message).toMatch(/fake error 2/);
expect(blockResults[1].passed()).toEqual(true);
expect(specResults[2].passed()).toEqual(true);
expect(specResults[3].passed()).toEqual(false);
blockResults = specResults[3].getItems();
expect(blockResults[0].message).toMatch(/fake error 3/);
expect(specResults[4].passed()).toEqual(true);
});
it("should handle exceptions thrown directly in top-level describe blocks and continue", function () {
var suite = env.describe("a top level describe block that throws an exception", function () {
env.it("is a test that should pass", function () {
this.expect(true).toEqual(true); this.expect(true).toEqual(true);
}); });
throw new Error("top level error"); throw new Error("top level error");
}); });
suite.execute(); suite.execute();
var suiteResults = suite.results(); var suiteResults = suite.results();
var specResults = suiteResults.getItems(); var specResults = suiteResults.getItems();
expect(suiteResults.passed()).toEqual(false); expect(suiteResults.passed()).toEqual(false);
expect(specResults.length).toEqual(2); expect(specResults.length).toEqual(2);
expect(specResults[1].description).toMatch(/encountered a declaration exception/); expect(specResults[1].description).toMatch(/encountered a declaration exception/);
}); });
it("should handle exceptions thrown directly in nested describe blocks and continue", function () { it("should handle exceptions thrown directly in nested describe blocks and continue", function () {
var suite = env.describe("a top level describe", function () { var suite = env.describe("a top level describe", function () {
env.describe("a mid-level describe that throws an exception", function () { env.describe("a mid-level describe that throws an exception", function () {
env.it("is a test that should pass", function () { env.it("is a test that should pass", function () {
this.expect(true).toEqual(true); this.expect(true).toEqual(true);
}); });
throw new Error("a mid-level error"); throw new Error("a mid-level error");
}); });
}); });
suite.execute(); suite.execute();
var suiteResults = suite.results(); var suiteResults = suite.results();
var specResults = suiteResults.getItems(); var specResults = suiteResults.getItems();
expect(suiteResults.passed()).toEqual(false); expect(suiteResults.passed()).toEqual(false);
expect(specResults.length).toEqual(1); expect(specResults.length).toEqual(1);
var nestedSpecResults = specResults[0].getItems(); var nestedSpecResults = specResults[0].getItems();
expect(nestedSpecResults.length).toEqual(2); expect(nestedSpecResults.length).toEqual(2);
expect(nestedSpecResults[1].description).toMatch(/encountered a declaration exception/); expect(nestedSpecResults[1].description).toMatch(/encountered a declaration exception/);
}); });
});
}); });
+8 -3
View File
@@ -13,10 +13,15 @@ jasmine.Block = function(env, func, spec) {
}; };
jasmine.Block.prototype.execute = function(onComplete) { jasmine.Block.prototype.execute = function(onComplete) {
try { if (!jasmine.CATCH_EXCEPTIONS) {
this.func.apply(this.spec); this.func.apply(this.spec);
} catch (e) { }
this.spec.fail(e); else {
try {
this.func.apply(this.spec);
} catch (e) {
this.spec.fail(e);
}
} }
onComplete(); onComplete();
}; };
+7
View File
@@ -39,6 +39,13 @@ jasmine.DEFAULT_UPDATE_INTERVAL = 250;
*/ */
jasmine.DEFAULT_TIMEOUT_INTERVAL = 5000; jasmine.DEFAULT_TIMEOUT_INTERVAL = 5000;
/**
* By default exceptions thrown in the context of a test are caught by jasmine so that it can run the remaining tests in the suite.
* Set to false to let the exception bubble up in the browser.
*
*/
jasmine.CATCH_EXCEPTIONS = true;
jasmine.getGlobal = function() { jasmine.getGlobal = function() {
function getGlobal() { function getGlobal() {
return this; return this;