Allow matcher custom failure messages to be a function

By deferring the evaluation of these messages, we can avoid the
expensive creation of them when in the majority use case (tests are
    passing) they are not needed.

These failure messages were causing performance problems with larger
objects needed to be pretty printed as discussed in #520 and brought up
by @rdy.

[fixes #65925900][fixes #520]
This commit is contained in:
Sheel Choksi
2014-02-18 19:58:07 -08:00
parent 46d2c43da1
commit 76ca5ef6d4
11 changed files with 142 additions and 98 deletions

View File

@@ -193,6 +193,40 @@ describe("Expectation", function() {
j$.Expectation.addMatchers(matchers);
expectation = new j$.Expectation({
actual: "an actual",
addExpectationResult: addExpectationResult
});
expectation.toFoo("hello");
expect(addExpectationResult).toHaveBeenCalledWith(false, {
matcherName: "toFoo",
passed: false,
expected: "hello",
actual: "an actual",
message: "I am a custom message"
});
});
it("reports a failing result with a custom fail message function to the spec when the comparison fails", function() {
var matchers = {
toFoo: function() {
return {
compare: function() {
return {
pass: false,
message: function() { return "I am a custom message"; }
};
}
};
}
},
addExpectationResult = jasmine.createSpy("addExpectationResult"),
expectation;
j$.Expectation.addMatchers(matchers);
expectation = new j$.Expectation({
matchers: matchers,
actual: "an actual",

View File

@@ -32,6 +32,6 @@ describe("toBeNaN", function() {
var matcher = j$.matchers.toBeNaN(),
result = matcher.compare(0);
expect(result.message).toEqual("Expected 0 to be NaN.");
expect(result.message()).toEqual("Expected 0 to be NaN.");
});
});

View File

@@ -11,7 +11,7 @@ describe("toHaveBeenCalledWith", function() {
result = matcher.compare(calledSpy, 'a', 'b');
expect(result.pass).toBe(true);
expect(result.message).toEqual("Expected spy called-spy not to have been called with [ 'a', 'b' ] but it was.");
expect(result.message()).toEqual("Expected spy called-spy not to have been called with [ 'a', 'b' ] but it was.");
});
it("fails when the actual was not called", function() {
@@ -24,7 +24,7 @@ describe("toHaveBeenCalledWith", function() {
result = matcher.compare(uncalledSpy);
expect(result.pass).toBe(false);
expect(result.message).toEqual("Expected spy uncalled spy to have been called with [ ] but it was never called.");
expect(result.message()).toEqual("Expected spy uncalled spy to have been called with [ ] but it was never called.");
});
it("fails when the actual was called with different parameters", function() {
@@ -40,7 +40,7 @@ describe("toHaveBeenCalledWith", function() {
result = matcher.compare(calledSpy, 'a', 'b');
expect(result.pass).toBe(false);
expect(result.message).toEqual("Expected spy called spy to have been called with [ 'a', 'b' ] but actual calls were [ 'a' ], [ 'c', 'd' ].");
expect(result.message()).toEqual("Expected spy called spy to have been called with [ 'a', 'b' ] but actual calls were [ 'a' ], [ 'c', 'd' ].");
});
it("throws an exception when the actual is not a spy", function() {

View File

@@ -62,7 +62,7 @@ describe("toThrowError", function() {
result = matcher.compare(fn);
expect(result.pass).toBe(false);
expect(result.message).toEqual("Expected function to throw an Error, but it threw 4.");
expect(result.message()).toEqual("Expected function to throw an Error, but it threw 4.");
});
it("fails with the correct message if thrown is a falsy value", function() {
@@ -74,7 +74,7 @@ describe("toThrowError", function() {
result = matcher.compare(fn);
expect(result.pass).toBe(false);
expect(result.message).toEqual("Expected function to throw an Error, but it threw undefined.");
expect(result.message()).toEqual("Expected function to throw an Error, but it threw undefined.");
});
it("passes if thrown is a type of Error, but there is no expected error", function() {
@@ -100,7 +100,7 @@ describe("toThrowError", function() {
result = matcher.compare(fn, "foo");
expect(result.pass).toBe(true);
expect(result.message).toEqual("Expected function not to throw an exception with message 'foo'.");
expect(result.message()).toEqual("Expected function not to throw an exception with message 'foo'.");
});
it("fails if thrown is an Error and the expected is not the same message", function() {
@@ -113,7 +113,7 @@ describe("toThrowError", function() {
result = matcher.compare(fn, "bar");
expect(result.pass).toBe(false);
expect(result.message).toEqual("Expected function to throw an exception with message 'bar', but it threw an exception with message 'foo'.");
expect(result.message()).toEqual("Expected function to throw an exception with message 'bar', but it threw an exception with message 'foo'.");
});
it("passes if thrown is an Error and the expected is a RegExp that matches the message", function() {
@@ -126,7 +126,7 @@ describe("toThrowError", function() {
result = matcher.compare(fn, /long/);
expect(result.pass).toBe(true);
expect(result.message).toEqual("Expected function not to throw an exception with a message matching /long/.");
expect(result.message()).toEqual("Expected function not to throw an exception with a message matching /long/.");
});
it("fails if thrown is an Error and the expected is a RegExp that does not match the message", function() {
@@ -139,7 +139,7 @@ describe("toThrowError", function() {
result = matcher.compare(fn, /foo/);
expect(result.pass).toBe(false);
expect(result.message).toEqual("Expected function to throw an exception with a message matching /foo/, but it threw an exception with message 'a long message'.");
expect(result.message()).toEqual("Expected function to throw an exception with a message matching /foo/, but it threw an exception with message 'a long message'.");
});
it("passes if thrown is an Error and the expected the same Error", function() {
@@ -207,7 +207,7 @@ describe("toThrowError", function() {
result = matcher.compare(fn, TypeError, "foo");
expect(result.pass).toBe(true);
expect(result.message).toEqual("Expected function not to throw TypeError with message \"foo\".");
expect(result.message()).toEqual("Expected function not to throw TypeError with message 'foo'.");
});
it("passes if thrown is a custom error that takes arguments and it is equal to the expected custom error and message", function() {
@@ -227,7 +227,7 @@ describe("toThrowError", function() {
result = matcher.compare(fn, CustomError, "foo");
expect(result.pass).toBe(true);
expect(result.message).toEqual("Expected function not to throw CustomError with message \"foo\".");
expect(result.message()).toEqual("Expected function not to throw CustomError with message 'foo'.");
});
it("fails if thrown is a type of Error and the expected is a different Error", function() {
@@ -243,7 +243,7 @@ describe("toThrowError", function() {
result = matcher.compare(fn, TypeError, "bar");
expect(result.pass).toBe(false);
expect(result.message).toEqual("Expected function to throw TypeError with message \"bar\", but it threw TypeError with message \"foo\".");
expect(result.message()).toEqual("Expected function to throw TypeError with message 'bar', but it threw TypeError with message 'foo'.");
});
it("passes if thrown is a type of Error and has the same type as the expected Error and the message matches the expected message", function() {
@@ -259,7 +259,7 @@ describe("toThrowError", function() {
result = matcher.compare(fn, TypeError, /foo/);
expect(result.pass).toBe(true);
expect(result.message).toEqual("Expected function not to throw TypeError with message matching /foo/.");
expect(result.message()).toEqual("Expected function not to throw TypeError with message matching /foo/.");
});
it("fails if thrown is a type of Error and the expected is a different Error", function() {
@@ -275,6 +275,6 @@ describe("toThrowError", function() {
result = matcher.compare(fn, TypeError, /bar/);
expect(result.pass).toBe(false);
expect(result.message).toEqual("Expected function to throw TypeError with message matching /bar/, but it threw TypeError with message \"foo\".");
expect(result.message()).toEqual("Expected function to throw TypeError with message matching /bar/, but it threw TypeError with message 'foo'.");
});
});

View File

@@ -34,7 +34,7 @@ describe("toThrow", function() {
result = matcher.compare(fn);
expect(result.pass).toBe(true);
expect(result.message).toEqual("Expected function not to throw, but it threw 5.");
expect(result.message()).toEqual("Expected function not to throw, but it threw 5.");
});
it("passes even if what is thrown is falsy", function() {
@@ -46,7 +46,7 @@ describe("toThrow", function() {
result = matcher.compare(fn);
expect(result.pass).toBe(true);
expect(result.message).toEqual("Expected function not to throw, but it threw undefined.");
expect(result.message()).toEqual("Expected function not to throw, but it threw undefined.");
});
it("passes if what is thrown is equivalent to what is expected", function() {
@@ -62,7 +62,7 @@ describe("toThrow", function() {
result = matcher.compare(fn, 5);
expect(result.pass).toBe(true);
expect(result.message).toEqual("Expected function not to throw 5.");
expect(result.message()).toEqual("Expected function not to throw 5.");
});
it("fails if what is thrown is not equivalent to what is expected", function() {
@@ -78,7 +78,7 @@ describe("toThrow", function() {
result = matcher.compare(fn, "foo");
expect(result.pass).toBe(false);
expect(result.message).toEqual("Expected function to throw 'foo', but it threw 5.");
expect(result.message()).toEqual("Expected function to throw 'foo', but it threw 5.");
});
it("fails if what is thrown is not equivalent to undefined", function() {
@@ -94,6 +94,6 @@ describe("toThrow", function() {
result = matcher.compare(fn, void 0);
expect(result.pass).toBe(false);
expect(result.message).toEqual("Expected function to throw undefined, but it threw 5.");
expect(result.message()).toEqual("Expected function to throw undefined, but it threw 5.");
});
});