Attempt at normalizing error stacks across browsers.

Failed expectations now have a `stack` property, remove `trace.stack`
This commit is contained in:
Dan Hansen and Davis W. Frank
2013-02-27 16:37:31 -08:00
parent dc4563d45c
commit d6da13a8dd
12 changed files with 237 additions and 132 deletions

View File

@@ -79,9 +79,8 @@ jasmine.HtmlReporter = function(options) {
for (var i = 0; i < result.failedExpectations.length; i++) { for (var i = 0; i < result.failedExpectations.length; i++) {
var expectation = result.failedExpectations[i]; var expectation = result.failedExpectations[i];
var stack = (expectation.trace && expectation.trace.stack) || "";
messages.appendChild(createDom("div", {className: "result-message"}, expectation.message)); messages.appendChild(createDom("div", {className: "result-message"}, expectation.message));
messages.appendChild(createDom("div", {className: "stack-trace"}, stack)); messages.appendChild(createDom("div", {className: "stack-trace"}, expectation.stack));
} }
failures.push(failure); failures.push(failure);

View File

@@ -439,29 +439,66 @@ jasmine.util.argsToArray = function(args) {
var arrayOfArgs = []; var arrayOfArgs = [];
for (var i = 0; i < args.length; i++) arrayOfArgs.push(args[i]); for (var i = 0; i < args.length; i++) arrayOfArgs.push(args[i]);
return arrayOfArgs; return arrayOfArgs;
};jasmine.exceptionFormatter = function(e) { };jasmine.ExceptionFormatter = function() {
var message = e.name this.message = function(error) {
+ ': ' var message = error.name
+ e.message + ': '
+ ' in ' + error.message;
+ (e.fileName || e.sourceURL || '')
+ ' (line '
+ (e.line || e.lineNumber || '')
+ ')';
return message; if (error.fileName || error.sourceURL) {
}; message += " in " + (error.fileName || error.sourceURL);
//TODO: expectation result may make more sense as a presentation of an expectation. }
jasmine.buildExpectationResult = function(params) {
return { if (error.line || error.lineNumber) {
type: 'expect', message += " (line " + (error.line || error.lineNumber) + ")"
matcherName: params.matcherName, }
expected: params.expected,
actual: params.actual, return message;
message: params.passed ? 'Passed.' : params.message,
trace: params.passed ? '' : (params.trace || new Error(this.message)),
passed: params.passed
}; };
this.stack = function(error) {
return error ? error.stack : null;
}
};//TODO: expectation result may make more sense as a presentation of an expectation.
jasmine.buildExpectationResult = function(options) {
var messageFormatter = options.messageFormatter || function() {},
stackFormatter = options.stackFormatter || function() {};
return {
matcherName: options.matcherName,
expected: options.expected,
actual: options.actual,
message: message(),
stack: stack(),
passed: options.passed
};
function message() {
if (options.passed) {
return "Passed."
} else if (options.message) {
return options.message
} else if (options.error) {
return messageFormatter(options.error);
}
return ""
}
function stack() {
if (options.passed) {
return "";
}
var error = options.error;
if (!error) {
try {
throw new Error(message());
} catch (e) {
error = e;
}
}
return stackFormatter(error);
}
}; };
(function() { (function() {
jasmine.Env = function(options) { jasmine.Env = function(options) {
@@ -536,18 +573,21 @@ jasmine.buildExpectationResult = function(params) {
} }
}; };
var exceptionFormatter = jasmine.exceptionFormatter;
var specConstructor = jasmine.Spec; var specConstructor = jasmine.Spec;
var getSpecName = function(spec, currentSuite) { var getSpecName = function(spec, currentSuite) {
return currentSuite.getFullName() + ' ' + spec.description + '.'; return currentSuite.getFullName() + ' ' + spec.description + '.';
}; };
var buildExpectationResult = jasmine.buildExpectationResult; // TODO: we may just be able to pass in the fn instead of wrapping here
var expectationResultFactory = function(attrs) { var buildExpectationResult = jasmine.buildExpectationResult,
return buildExpectationResult(attrs); exceptionFormatter = new jasmine.ExceptionFormatter(),
}; expectationResultFactory = function(attrs) {
attrs.messageFormatter = exceptionFormatter.message;
attrs.stackFormatter = exceptionFormatter.stack;
return buildExpectationResult(attrs);
};
// TODO: fix this naming, and here's where the value comes in // TODO: fix this naming, and here's where the value comes in
this.catchExceptions = function(value) { this.catchExceptions = function(value) {
@@ -1043,21 +1083,19 @@ jasmine.Matchers.matcherFn_ = function(matcherName, matcherFunction) {
message += "."; message += ".";
} }
} }
var expectationResult = jasmine.buildExpectationResult({
this.spec.addExpectationResult(result, {
matcherName: matcherName, matcherName: matcherName,
passed: result, passed: result,
expected: matcherArgs.length > 1 ? matcherArgs : matcherArgs[0], expected: matcherArgs.length > 1 ? matcherArgs : matcherArgs[0],
actual: this.actual, actual: this.actual,
message: message message: message
}); });
this.spec.addExpectationResult(result, expectationResult);
return jasmine.undefined; return jasmine.undefined;
}; };
}; };
/** /**
* toBe: compares the actual to the expected using === * toBe: compares the actual to the expected using ===
* @param expected * @param expected
@@ -1137,11 +1175,11 @@ jasmine.Matchers.prototype.toBeNull = function() {
* Matcher that compares the actual to NaN. * Matcher that compares the actual to NaN.
*/ */
jasmine.Matchers.prototype.toBeNaN = function() { jasmine.Matchers.prototype.toBeNaN = function() {
this.message = function() { this.message = function() {
return [ "Expected " + jasmine.pp(this.actual) + " to be NaN." ]; return [ "Expected " + jasmine.pp(this.actual) + " to be NaN." ];
}; };
return (this.actual !== this.actual); return (this.actual !== this.actual);
}; };
/** /**
@@ -1356,7 +1394,7 @@ jasmine.Matchers.Any.prototype.jasmineToString = function() {
return '<jasmine.any(' + this.expectedClass + ')>'; return '<jasmine.any(' + this.expectedClass + ')>';
}; };
jasmine.Matchers.ObjectContaining = function (sample) { jasmine.Matchers.ObjectContaining = function(sample) {
this.sample = sample; this.sample = sample;
}; };
@@ -1382,7 +1420,7 @@ jasmine.Matchers.ObjectContaining.prototype.jasmineMatches = function(other, mis
return (mismatchKeys.length === 0 && mismatchValues.length === 0); return (mismatchKeys.length === 0 && mismatchValues.length === 0);
}; };
jasmine.Matchers.ObjectContaining.prototype.jasmineToString = function () { jasmine.Matchers.ObjectContaining.prototype.jasmineToString = function() {
return "<jasmine.objectContaining(" + jasmine.pp(this.sample) + ")>"; return "<jasmine.objectContaining(" + jasmine.pp(this.sample) + ")>";
}; };
/** /**
@@ -1583,9 +1621,10 @@ jasmine.Spec = function(attrs) {
jasmine.Spec.prototype.addExpectationResult = function(passed, data) { jasmine.Spec.prototype.addExpectationResult = function(passed, data) {
this.encounteredExpectations = true; this.encounteredExpectations = true;
if (!passed) { if (passed) {
this.result.failedExpectations.push(data); return;
} }
this.result.failedExpectations.push(this.expectationResultFactory(data));
}; };
jasmine.Spec.prototype.expect = function(actual) { jasmine.Spec.prototype.expect = function(actual) {
@@ -1608,14 +1647,13 @@ jasmine.Spec.prototype.execute = function(onComplete) {
this.queueRunner({ this.queueRunner({
fns: allFns, fns: allFns,
onException: function(e) { onException: function(e) {
self.addExpectationResult(false, self.expectationResultFactory({ self.addExpectationResult(false, {
matcherName: "", matcherName: "",
passed: false, passed: false,
expected: "", expected: "",
actual: "", actual: "",
message: self.exceptionFormatter(e), error: e
trace: e });
}));
}, },
onComplete: complete onComplete: complete
}); });
@@ -1652,7 +1690,7 @@ jasmine.Spec.prototype.status = function() {
jasmine.Spec.prototype.getFullName = function() { jasmine.Spec.prototype.getFullName = function() {
return this.getSpecName(this); return this.getSpecName(this);
} };
jasmine.Suite = function(attrs) { jasmine.Suite = function(attrs) {
this.env = attrs.env; this.env = attrs.env;
this.id = attrs.id; this.id = attrs.id;

View File

@@ -1,26 +1,52 @@
describe("ExceptionFormatter", function() { describe("ExceptionFormatter", function() {
describe("#message", function() {
it('formats Firefox exception messages', function() {
var sampleFirefoxException = {
fileName: 'foo.js',
lineNumber: '1978',
message: 'you got your foo in my bar',
name: 'A Classic Mistake'
},
exceptionFormatter = new jasmine.ExceptionFormatter(),
message = exceptionFormatter.message(sampleFirefoxException);
it('formats Firefox exception messages', function() { expect(message).toEqual('A Classic Mistake: you got your foo in my bar in foo.js (line 1978)');
var sampleFirefoxException = { });
fileName: 'foo.js',
line: '1978',
message: 'you got your foo in my bar',
name: 'A Classic Mistake'
},
message = jasmine.exceptionMessageFor(sampleFirefoxException);
expect(message).toEqual('A Classic Mistake: you got your foo in my bar in foo.js (line 1978)'); it('formats Webkit exception messages', function() {
var sampleWebkitException = {
sourceURL: 'foo.js',
line: '1978',
message: 'you got your foo in my bar',
name: 'A Classic Mistake'
},
exceptionFormatter = new jasmine.ExceptionFormatter(),
message = exceptionFormatter.message(sampleWebkitException);
expect(message).toEqual('A Classic Mistake: you got your foo in my bar in foo.js (line 1978)');
});
it('formats V8 exception messages', function() {
var sampleV8 = {
message: 'you got your foo in my bar',
name: 'A Classic Mistake'
},
exceptionFormatter = new jasmine.ExceptionFormatter(),
message = exceptionFormatter.message(sampleV8);
expect(message).toEqual('A Classic Mistake: you got your foo in my bar');
});
}); });
it('formats Webkit exception messages', function() { describe("#stack", function() {
var sampleWebkitException = { it("formats stack traces from Webkit, Firefox or node.js", function() {
sourceURL: 'foo.js', var error = new Error("an error");
lineNumber: '1978', expect(new jasmine.ExceptionFormatter().stack(error)).toMatch(/ExceptionFormatterSpec\.js.*\d+/)
message: 'you got your foo in my bar', });
name: 'A Classic Mistake'
},
message = jasmine.exceptionMessageFor(sampleWebkitException);
expect(message).toEqual('A Classic Mistake: you got your foo in my bar in foo.js (line 1978)'); it("returns null if no Error provided", function() {
expect(new jasmine.ExceptionFormatter().stack()).toBeNull();
});
}); });
}); });

View File

@@ -1,33 +1,47 @@
describe("buildExpectationResult", function() { describe("buildExpectationResult", function() {
it("defaults to passed", function() { it("defaults to passed", function() {
var result = jasmine.buildExpectationResult({passed: 'some-value'}); var result = jasmine.buildExpectationResult({passed: 'some-value'});
expect(result.passed).toBe('some-value'); expect(result.passed).toBe('some-value');
}); });
it("has a type of expect", function() {
var result = jasmine.buildExpectationResult({});
expect(result.type).toBe('expect');
});
it("message defaults to Passed for passing specs", function() { it("message defaults to Passed for passing specs", function() {
var result = jasmine.buildExpectationResult({passed: true, message: 'some-value'}); var result = jasmine.buildExpectationResult({passed: true, message: 'some-value'});
expect(result.message).toBe('Passed.'); expect(result.message).toBe('Passed.');
}); });
it("message returns the message for failing specs", function() { it("message returns the message for failing expecations", function() {
var result = jasmine.buildExpectationResult({passed: false, message: 'some-value'}); var result = jasmine.buildExpectationResult({passed: false, message: 'some-value'});
expect(result.message).toBe('some-value'); expect(result.message).toBe('some-value');
}); });
it("trace passes trace if exists", function() { it("delegates message formatting to the provided formatter if there was an Error", function() {
var result = jasmine.buildExpectationResult({trace: 'some-value'}); var fakeError = {message: 'foo'},
expect(result.trace).toBe('some-value'); messageFormatter = jasmine.createSpy("exception message formatter").andReturn(fakeError.message);
var result = jasmine.buildExpectationResult(
{
passed: false,
error: fakeError,
messageFormatter: messageFormatter
});
expect(messageFormatter).toHaveBeenCalledWith(fakeError);
expect(result.message).toEqual('foo');
}); });
it("trace returns a new error if trace is falsy", function() { it("delegates stack formatting to the provided formatter if there was an Error", function() {
var result = jasmine.buildExpectationResult({trace: false}); var fakeError = {stack: 'foo'},
expect(result.trace).toEqual(jasmine.any(Error)); stackFormatter = jasmine.createSpy("stack formatter").andReturn(fakeError.stack);
var result = jasmine.buildExpectationResult(
{
passed: false,
error: fakeError,
stackFormatter: stackFormatter
});
expect(stackFormatter).toHaveBeenCalledWith(fakeError);
expect(result.stack).toEqual('foo');
}); });
it("matcherName returns passed matcherName", function() { it("matcherName returns passed matcherName", function() {
@@ -44,5 +58,4 @@ describe("buildExpectationResult", function() {
var result = jasmine.buildExpectationResult({actual: 'some-value'}); var result = jasmine.buildExpectationResult({actual: 'some-value'});
expect(result.actual).toBe('some-value'); expect(result.actual).toBe('some-value');
}); });
}); });

View File

@@ -660,9 +660,6 @@ describe("jasmine.Matchers", function() {
}); });
it("should provide an inverted default message", function() { it("should provide an inverted default message", function() {
match(37).not.toBeGreaterThan(42);
expect(lastResult().message).toEqual("Passed.");
match(42).not.toBeGreaterThan(37); match(42).not.toBeGreaterThan(37);
expect(lastResult().message).toEqual("Expected 42 not to be greater than 37."); expect(lastResult().message).toEqual("Expected 42 not to be greater than 37.");
}); });
@@ -677,14 +674,10 @@ describe("jasmine.Matchers", function() {
} }
}); });
match(true).custom();
expect(lastResult().message).toEqual("Passed.");
match(false).custom(); match(false).custom();
expect(lastResult().message).toEqual("Expected it was called."); expect(lastResult().message).toEqual("Expected it was called.");
match(true).not.custom(); match(true).not.custom();
expect(lastResult().message).toEqual("Expected it wasn't called."); expect(lastResult().message).toEqual("Expected it wasn't called.");
match(false).not.custom();
expect(lastResult().message).toEqual("Passed.");
}); });
}); });

View File

@@ -369,9 +369,7 @@ describe("New HtmlReporter", function() {
failedExpectations: [ failedExpectations: [
{ {
message: "a failure message", message: "a failure message",
trace: { stack: "a stack trace"
stack: "a stack trace"
}
} }
] ]
}; };

View File

@@ -71,18 +71,21 @@
} }
}; };
var exceptionFormatter = jasmine.exceptionFormatter;
var specConstructor = jasmine.Spec; var specConstructor = jasmine.Spec;
var getSpecName = function(spec, currentSuite) { var getSpecName = function(spec, currentSuite) {
return currentSuite.getFullName() + ' ' + spec.description + '.'; return currentSuite.getFullName() + ' ' + spec.description + '.';
}; };
var buildExpectationResult = jasmine.buildExpectationResult; // TODO: we may just be able to pass in the fn instead of wrapping here
var expectationResultFactory = function(attrs) { var buildExpectationResult = jasmine.buildExpectationResult,
return buildExpectationResult(attrs); exceptionFormatter = new jasmine.ExceptionFormatter(),
}; expectationResultFactory = function(attrs) {
attrs.messageFormatter = exceptionFormatter.message;
attrs.stackFormatter = exceptionFormatter.stack;
return buildExpectationResult(attrs);
};
// TODO: fix this naming, and here's where the value comes in // TODO: fix this naming, and here's where the value comes in
this.catchExceptions = function(value) { this.catchExceptions = function(value) {

View File

@@ -1,12 +1,21 @@
jasmine.exceptionMessageFor = function(e) { jasmine.ExceptionFormatter = function() {
var message = e.name this.message = function(error) {
+ ': ' var message = error.name
+ e.message + ': '
+ ' in ' + error.message;
+ (e.fileName || e.sourceURL || '')
+ ' (line '
+ (e.line || e.lineNumber || '')
+ ')';
return message; if (error.fileName || error.sourceURL) {
}; message += " in " + (error.fileName || error.sourceURL);
}
if (error.line || error.lineNumber) {
message += " (line " + (error.line || error.lineNumber) + ")"
}
return message;
};
this.stack = function(error) {
return error ? error.stack : null;
}
};

View File

@@ -1,12 +1,41 @@
//TODO: expectation result may make more sense as a presentation of an expectation. //TODO: expectation result may make more sense as a presentation of an expectation.
jasmine.buildExpectationResult = function(params) { jasmine.buildExpectationResult = function(options) {
var messageFormatter = options.messageFormatter || function() {},
stackFormatter = options.stackFormatter || function() {};
return { return {
type: 'expect', matcherName: options.matcherName,
matcherName: params.matcherName, expected: options.expected,
expected: params.expected, actual: options.actual,
actual: params.actual, message: message(),
message: params.passed ? 'Passed.' : params.message, stack: stack(),
trace: params.passed ? '' : (params.trace || new Error(this.message)), passed: options.passed
passed: params.passed
}; };
function message() {
if (options.passed) {
return "Passed."
} else if (options.message) {
return options.message
} else if (options.error) {
return messageFormatter(options.error);
}
return ""
}
function stack() {
if (options.passed) {
return "";
}
var error = options.error;
if (!error) {
try {
throw new Error(message());
} catch (e) {
error = e;
}
}
return stackFormatter(error);
}
}; };

View File

@@ -53,21 +53,19 @@ jasmine.Matchers.matcherFn_ = function(matcherName, matcherFunction) {
message += "."; message += ".";
} }
} }
var expectationResult = jasmine.buildExpectationResult({
this.spec.addExpectationResult(result, {
matcherName: matcherName, matcherName: matcherName,
passed: result, passed: result,
expected: matcherArgs.length > 1 ? matcherArgs : matcherArgs[0], expected: matcherArgs.length > 1 ? matcherArgs : matcherArgs[0],
actual: this.actual, actual: this.actual,
message: message message: message
}); });
this.spec.addExpectationResult(result, expectationResult);
return jasmine.undefined; return jasmine.undefined;
}; };
}; };
/** /**
* toBe: compares the actual to the expected using === * toBe: compares the actual to the expected using ===
* @param expected * @param expected
@@ -147,11 +145,11 @@ jasmine.Matchers.prototype.toBeNull = function() {
* Matcher that compares the actual to NaN. * Matcher that compares the actual to NaN.
*/ */
jasmine.Matchers.prototype.toBeNaN = function() { jasmine.Matchers.prototype.toBeNaN = function() {
this.message = function() { this.message = function() {
return [ "Expected " + jasmine.pp(this.actual) + " to be NaN." ]; return [ "Expected " + jasmine.pp(this.actual) + " to be NaN." ];
}; };
return (this.actual !== this.actual); return (this.actual !== this.actual);
}; };
/** /**
@@ -366,7 +364,7 @@ jasmine.Matchers.Any.prototype.jasmineToString = function() {
return '<jasmine.any(' + this.expectedClass + ')>'; return '<jasmine.any(' + this.expectedClass + ')>';
}; };
jasmine.Matchers.ObjectContaining = function (sample) { jasmine.Matchers.ObjectContaining = function(sample) {
this.sample = sample; this.sample = sample;
}; };
@@ -392,6 +390,6 @@ jasmine.Matchers.ObjectContaining.prototype.jasmineMatches = function(other, mis
return (mismatchKeys.length === 0 && mismatchValues.length === 0); return (mismatchKeys.length === 0 && mismatchValues.length === 0);
}; };
jasmine.Matchers.ObjectContaining.prototype.jasmineToString = function () { jasmine.Matchers.ObjectContaining.prototype.jasmineToString = function() {
return "<jasmine.objectContaining(" + jasmine.pp(this.sample) + ")>"; return "<jasmine.objectContaining(" + jasmine.pp(this.sample) + ")>";
}; };

View File

@@ -26,9 +26,10 @@ jasmine.Spec = function(attrs) {
jasmine.Spec.prototype.addExpectationResult = function(passed, data) { jasmine.Spec.prototype.addExpectationResult = function(passed, data) {
this.encounteredExpectations = true; this.encounteredExpectations = true;
if (!passed) { if (passed) {
this.result.failedExpectations.push(data); return;
} }
this.result.failedExpectations.push(this.expectationResultFactory(data));
}; };
jasmine.Spec.prototype.expect = function(actual) { jasmine.Spec.prototype.expect = function(actual) {
@@ -51,14 +52,13 @@ jasmine.Spec.prototype.execute = function(onComplete) {
this.queueRunner({ this.queueRunner({
fns: allFns, fns: allFns,
onException: function(e) { onException: function(e) {
self.addExpectationResult(false, self.expectationResultFactory({ self.addExpectationResult(false, {
matcherName: "", matcherName: "",
passed: false, passed: false,
expected: "", expected: "",
actual: "", actual: "",
message: self.exceptionFormatter(e), error: e
trace: e });
}));
}, },
onComplete: complete onComplete: complete
}); });
@@ -95,4 +95,4 @@ jasmine.Spec.prototype.status = function() {
jasmine.Spec.prototype.getFullName = function() { jasmine.Spec.prototype.getFullName = function() {
return this.getSpecName(this); return this.getSpecName(this);
} };

View File

@@ -79,9 +79,8 @@ jasmine.HtmlReporter = function(options) {
for (var i = 0; i < result.failedExpectations.length; i++) { for (var i = 0; i < result.failedExpectations.length; i++) {
var expectation = result.failedExpectations[i]; var expectation = result.failedExpectations[i];
var stack = (expectation.trace && expectation.trace.stack) || "";
messages.appendChild(createDom("div", {className: "result-message"}, expectation.message)); messages.appendChild(createDom("div", {className: "result-message"}, expectation.message));
messages.appendChild(createDom("div", {className: "stack-trace"}, stack)); messages.appendChild(createDom("div", {className: "stack-trace"}, expectation.stack));
} }
failures.push(failure); failures.push(failure);