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

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,11 +1027,16 @@ jasmine.Block = function(env, func, spec) {
}; };
jasmine.Block.prototype.execute = function(onComplete) { jasmine.Block.prototype.execute = function(onComplete) {
if (!jasmine.CATCH_EXCEPTIONS) {
this.func.apply(this.spec);
}
else {
try { try {
this.func.apply(this.spec); this.func.apply(this.spec);
} catch (e) { } catch (e) {
this.spec.fail(e); this.spec.fail(e);
} }
}
onComplete(); onComplete();
}; };
/** JavaScript API reporter. /** JavaScript API reporter.

View File

@@ -32,6 +32,32 @@ describe('Exceptions:', function() {
expect(jasmine.util.formatException(sampleWebkitException)).toEqual(expected); expect(jasmine.util.formatException(sampleWebkitException)).toEqual(expected);
}); });
describe('with break on exception', function() {
it('should not catch the exception', function() {
var suite = env.describe('suite for break on exceptions', function() {
env.it('should break when an exception is thrown', function() {
throw new Error('I should hit a breakpoint!');
});
});
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() { it('should handle exceptions thrown, but continue', function() {
var fakeTimer = new jasmine.FakeTimer(); var fakeTimer = new jasmine.FakeTimer();
env.setTimeout = fakeTimer.setTimeout; env.setTimeout = fakeTimer.setTimeout;
@@ -103,7 +129,6 @@ describe('Exceptions:', function() {
expect(specResults[4].passed()).toEqual(true); expect(specResults[4].passed()).toEqual(true);
}); });
it("should handle exceptions thrown directly in top-level describe blocks and continue", function () { 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 () { var suite = env.describe("a top level describe block that throws an exception", function () {
env.it("is a test that should pass", function () { env.it("is a test that should pass", function () {
@@ -147,3 +172,4 @@ describe('Exceptions:', function() {
expect(nestedSpecResults[1].description).toMatch(/encountered a declaration exception/); expect(nestedSpecResults[1].description).toMatch(/encountered a declaration exception/);
}); });
}); });
});

View File

@@ -13,10 +13,15 @@ jasmine.Block = function(env, func, spec) {
}; };
jasmine.Block.prototype.execute = function(onComplete) { jasmine.Block.prototype.execute = function(onComplete) {
if (!jasmine.CATCH_EXCEPTIONS) {
this.func.apply(this.spec);
}
else {
try { try {
this.func.apply(this.spec); this.func.apply(this.spec);
} catch (e) { } catch (e) {
this.spec.fail(e); this.spec.fail(e);
} }
}
onComplete(); onComplete();
}; };

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;