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

@@ -1,26 +1,52 @@
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() {
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)');
});
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() {
var sampleWebkitException = {
sourceURL: 'foo.js',
lineNumber: '1978',
message: 'you got your foo in my bar',
name: 'A Classic Mistake'
},
message = jasmine.exceptionMessageFor(sampleWebkitException);
describe("#stack", function() {
it("formats stack traces from Webkit, Firefox or node.js", function() {
var error = new Error("an error");
expect(new jasmine.ExceptionFormatter().stack(error)).toMatch(/ExceptionFormatterSpec\.js.*\d+/)
});
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() {
it("defaults to passed", function() {
var result = jasmine.buildExpectationResult({passed: '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() {
var result = jasmine.buildExpectationResult({passed: true, message: 'some-value'});
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'});
expect(result.message).toBe('some-value');
});
it("trace passes trace if exists", function() {
var result = jasmine.buildExpectationResult({trace: 'some-value'});
expect(result.trace).toBe('some-value');
it("delegates message formatting to the provided formatter if there was an Error", function() {
var fakeError = {message: 'foo'},
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() {
var result = jasmine.buildExpectationResult({trace: false});
expect(result.trace).toEqual(jasmine.any(Error));
it("delegates stack formatting to the provided formatter if there was an Error", function() {
var fakeError = {stack: 'foo'},
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() {
@@ -44,5 +58,4 @@ describe("buildExpectationResult", function() {
var result = jasmine.buildExpectationResult({actual: '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() {
match(37).not.toBeGreaterThan(42);
expect(lastResult().message).toEqual("Passed.");
match(42).not.toBeGreaterThan(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();
expect(lastResult().message).toEqual("Expected it was called.");
match(true).not.custom();
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: [
{
message: "a failure message",
trace: {
stack: "a stack trace"
}
stack: "a stack trace"
}
]
};