2009-06-15 15:28:14 +00:00
|
|
|
describe('Exceptions:', function() {
|
|
|
|
var env;
|
|
|
|
|
|
|
|
beforeEach(function() {
|
|
|
|
env = new jasmine.Env();
|
2011-04-18 04:44:41 +00:00
|
|
|
env.updateInterval = 0;
|
2009-06-15 15:28:14 +00:00
|
|
|
});
|
|
|
|
|
2009-10-30 00:03:24 +00:00
|
|
|
it('jasmine.formatException formats Firefox exception messages as expected', function() {
|
2009-06-15 15:28:14 +00:00
|
|
|
var sampleFirefoxException = {
|
|
|
|
fileName: 'foo.js',
|
|
|
|
line: '1978',
|
|
|
|
message: 'you got your foo in my bar',
|
|
|
|
name: 'A Classic Mistake'
|
|
|
|
};
|
|
|
|
|
|
|
|
var expected = 'A Classic Mistake: you got your foo in my bar in foo.js (line 1978)';
|
|
|
|
|
|
|
|
expect(jasmine.util.formatException(sampleFirefoxException)).toEqual(expected);
|
|
|
|
});
|
|
|
|
|
2009-10-30 00:03:24 +00:00
|
|
|
it('jasmine.formatException formats Webkit exception messages as expected', function() {
|
2009-06-15 15:28:14 +00:00
|
|
|
var sampleWebkitException = {
|
|
|
|
sourceURL: 'foo.js',
|
|
|
|
lineNumber: '1978',
|
|
|
|
message: 'you got your foo in my bar',
|
|
|
|
name: 'A Classic Mistake'
|
|
|
|
};
|
|
|
|
|
|
|
|
var expected = 'A Classic Mistake: you got your foo in my bar in foo.js (line 1978)';
|
|
|
|
|
|
|
|
expect(jasmine.util.formatException(sampleWebkitException)).toEqual(expected);
|
|
|
|
});
|
|
|
|
|
2009-06-15 20:47:05 +00:00
|
|
|
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);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2009-09-28 23:23:21 +00:00
|
|
|
var runner = env.currentRunner();
|
2009-06-15 20:47:05 +00:00
|
|
|
suite.execute();
|
2009-08-01 22:28:39 +00:00
|
|
|
fakeTimer.tick(2500);
|
2009-06-15 20:47:05 +00:00
|
|
|
|
2009-09-28 18:13:44 +00:00
|
|
|
var suiteResults = suite.results();
|
2009-08-19 14:42:47 +00:00
|
|
|
var specResults = suiteResults.getItems();
|
2009-08-01 22:28:39 +00:00
|
|
|
|
2009-08-19 14:42:47 +00:00
|
|
|
expect(suiteResults.passed()).toEqual(false);
|
2009-10-30 00:03:24 +00:00
|
|
|
//
|
2009-08-19 14:42:47 +00:00
|
|
|
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/);
|
2009-06-15 20:47:05 +00:00
|
|
|
|
2009-08-19 14:42:47 +00:00
|
|
|
expect(specResults[1].passed()).toEqual(false);
|
2011-02-26 23:07:59 +00:00
|
|
|
blockResults = specResults[1].getItems();
|
2009-08-19 14:42:47 +00:00
|
|
|
expect(blockResults[0].passed()).toEqual(false);
|
2011-02-26 23:07:59 +00:00
|
|
|
expect(blockResults[0].message).toMatch(/fake error 2/);
|
2009-08-19 14:42:47 +00:00
|
|
|
expect(blockResults[1].passed()).toEqual(true);
|
2009-06-15 20:47:05 +00:00
|
|
|
|
2009-08-19 14:42:47 +00:00
|
|
|
expect(specResults[2].passed()).toEqual(true);
|
2009-06-15 20:47:05 +00:00
|
|
|
|
2009-08-19 14:42:47 +00:00
|
|
|
expect(specResults[3].passed()).toEqual(false);
|
|
|
|
blockResults = specResults[3].getItems();
|
|
|
|
expect(blockResults[0].message).toMatch(/fake error 3/);
|
2009-06-15 20:47:05 +00:00
|
|
|
|
2009-08-19 14:42:47 +00:00
|
|
|
expect(specResults[4].passed()).toEqual(true);
|
2009-06-15 20:47:05 +00:00
|
|
|
});
|
|
|
|
|
2011-04-18 04:44:41 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
|
|
|
|
throw new Error("top level error");
|
|
|
|
});
|
|
|
|
|
|
|
|
suite.execute();
|
|
|
|
var suiteResults = suite.results();
|
|
|
|
var specResults = suiteResults.getItems();
|
|
|
|
|
|
|
|
expect(suiteResults.passed()).toEqual(false);
|
|
|
|
expect(specResults.length).toEqual(2);
|
|
|
|
|
|
|
|
expect(specResults[1].description).toMatch(/encountered a declaration exception/);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should handle exceptions thrown directly in nested describe blocks and continue", function () {
|
|
|
|
var suite = env.describe("a top level describe", function () {
|
|
|
|
env.describe("a mid-level describe that throws an exception", function () {
|
|
|
|
env.it("is a test that should pass", function () {
|
|
|
|
this.expect(true).toEqual(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
throw new Error("a mid-level error");
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
suite.execute();
|
|
|
|
var suiteResults = suite.results();
|
|
|
|
var specResults = suiteResults.getItems();
|
|
|
|
|
|
|
|
expect(suiteResults.passed()).toEqual(false);
|
|
|
|
expect(specResults.length).toEqual(1);
|
|
|
|
|
|
|
|
var nestedSpecResults = specResults[0].getItems();
|
|
|
|
|
|
|
|
expect(nestedSpecResults.length).toEqual(2);
|
|
|
|
expect(nestedSpecResults[1].description).toMatch(/encountered a declaration exception/);
|
|
|
|
});
|
2009-06-15 20:47:05 +00:00
|
|
|
});
|