Simplifying toThrow:
- It still supports no expected, which means that something was thrown - Expected value is now tested via equality in order to pass Adding toThrowError: - toThrowError() passes if an Error type was thrown - toThrowError(String) & toThrowError(RegExp) compare Expected to the Error message - toThrowError(Error constructor) compares Expected to the constructor of what was thrown - toThrowError(Error constructor, String) & toThrowError(Error constructor, RegExp) compares both the Error and the message Also, equality now handles Errors, enforcing the message as part of the equality.
This commit is contained in:
@@ -58,6 +58,14 @@ describe("matchersUtil", function() {
|
||||
expect(j$.matchersUtil.equals([1, 2], [1, 2, 3])).toBe(false);
|
||||
});
|
||||
|
||||
it("passes for Errors that are the same type and have the same message", function() {
|
||||
expect(j$.matchersUtil.equals(new Error("foo"), new Error("foo"))).toBe(true);
|
||||
});
|
||||
|
||||
it("fails for Errors that are the same type and have different messages", function() {
|
||||
expect(j$.matchersUtil.equals(new Error("foo"), new Error("bar"))).toBe(false);
|
||||
});
|
||||
|
||||
it("passes for Objects that are equivalent (simple case)", function() {
|
||||
expect(j$.matchersUtil.equals({a: "foo"}, {a: "foo"})).toBe(true);
|
||||
});
|
||||
|
||||
240
spec/core/matchers/toThrowErrorSpec.js
Normal file
240
spec/core/matchers/toThrowErrorSpec.js
Normal file
@@ -0,0 +1,240 @@
|
||||
describe("toThrowError", function() {
|
||||
it("throws an error when the actual is not a function", function() {
|
||||
var matcher = j$.matchers.toThrowError();
|
||||
|
||||
expect(function() {
|
||||
matcher.compare({});
|
||||
}).toThrow(new Error("Actual is not a Function")); // TODO: this needs to change for self-test
|
||||
});
|
||||
|
||||
it("throws an error when the expected is not an Error, string, or RegExp", function() {
|
||||
var matcher = j$.matchers.toThrowError(),
|
||||
fn = function() {
|
||||
throw new Error("foo");
|
||||
};
|
||||
|
||||
expect(function() {
|
||||
matcher.compare(fn, 1);
|
||||
}).toThrow(new Error("Expected is not an Error, string, or RegExp.")); // TODO: this needs to change for self-test
|
||||
});
|
||||
|
||||
it("throws an error when the expected error type is not an Error", function() {
|
||||
var matcher = j$.matchers.toThrowError(),
|
||||
fn = function() {
|
||||
throw new Error("foo");
|
||||
};
|
||||
|
||||
expect(function() {
|
||||
matcher.compare(fn, "string", "foo");
|
||||
}).toThrow(new Error("Expected error type is not an Error.")); // TODO: this needs to change for self-test
|
||||
});
|
||||
|
||||
it("throws an error when the expected error message is not a string or RegExp", function() {
|
||||
var matcher = j$.matchers.toThrowError(),
|
||||
fn = function() {
|
||||
throw new Error("foo");
|
||||
};
|
||||
|
||||
expect(function() {
|
||||
matcher.compare(fn, Error, 1);
|
||||
}).toThrow(new Error("Expected error message is not a string or RegExp.")); // TODO: this needs to change for self-test
|
||||
});
|
||||
|
||||
it("fails if actual does not throw at all", function() {
|
||||
var matcher = j$.matchers.toThrowError(),
|
||||
fn = function() {
|
||||
return true;
|
||||
},
|
||||
result;
|
||||
|
||||
result = matcher.compare(fn);
|
||||
|
||||
expect(result.pass).toBe(false);
|
||||
expect(result.message).toEqual("Expected function to throw an Error.");
|
||||
});
|
||||
|
||||
it("fails if thrown is not an instanceof Error", function() {
|
||||
var matcher = j$.matchers.toThrowError(),
|
||||
fn = function() {
|
||||
throw 4;
|
||||
},
|
||||
result;
|
||||
|
||||
result = matcher.compare(fn);
|
||||
expect(result.pass).toBe(false);
|
||||
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() {
|
||||
var matcher = j$.matchers.toThrowError(),
|
||||
fn = function() {
|
||||
throw undefined;
|
||||
},
|
||||
result;
|
||||
|
||||
result = matcher.compare(fn);
|
||||
expect(result.pass).toBe(false);
|
||||
expect(result.message).toEqual("Expected function to throw an Error, but it threw undefined.");
|
||||
});
|
||||
|
||||
it("passes if thrown is an Error, but there is no expected error", function() {
|
||||
var matcher = j$.matchers.toThrowError(),
|
||||
fn = function() {
|
||||
throw new TypeError();
|
||||
},
|
||||
result;
|
||||
|
||||
result = matcher.compare(fn);
|
||||
|
||||
expect(result.pass).toBe(true);
|
||||
expect(result.message).toEqual("Expected function not to throw an Error, but it threw TypeError.");
|
||||
});
|
||||
|
||||
it("passes if thrown is an Error and the expected is the same message", function() {
|
||||
var matcher = j$.matchers.toThrowError(),
|
||||
fn = function() {
|
||||
throw new Error("foo");
|
||||
},
|
||||
result;
|
||||
|
||||
result = matcher.compare(fn, "foo");
|
||||
|
||||
expect(result.pass).toBe(true);
|
||||
expect(result.message).toEqual("Expected function not to throw an execption with message 'foo'.");
|
||||
});
|
||||
|
||||
it("fails if thrown is an Error and the expected is not the same message", function() {
|
||||
var matcher = j$.matchers.toThrowError(),
|
||||
fn = function() {
|
||||
throw new Error("foo");
|
||||
},
|
||||
result;
|
||||
|
||||
result = matcher.compare(fn, "bar");
|
||||
|
||||
expect(result.pass).toBe(false);
|
||||
expect(result.message).toEqual("Expected function to throw an execption with message 'bar'.");
|
||||
});
|
||||
|
||||
it("passes if thrown is an Error and the expected is a RegExp that matches the message", function() {
|
||||
var matcher = j$.matchers.toThrowError(),
|
||||
fn = function() {
|
||||
throw new Error("a long message");
|
||||
},
|
||||
result;
|
||||
|
||||
result = matcher.compare(fn, /long/);
|
||||
|
||||
expect(result.pass).toBe(true);
|
||||
expect(result.message).toEqual("Expected function not to throw an execption 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() {
|
||||
var matcher = j$.matchers.toThrowError(),
|
||||
fn = function() {
|
||||
throw new Error("a long message");
|
||||
},
|
||||
result;
|
||||
|
||||
result = matcher.compare(fn, /foo/);
|
||||
|
||||
expect(result.pass).toBe(false);
|
||||
expect(result.message).toEqual("Expected function to throw an execption with a message matching /foo/.");
|
||||
});
|
||||
|
||||
it("passes if thrown is an Error and the expected the same Error", function() {
|
||||
var util = {
|
||||
equals: j$.createSpy('delegated-equal').andReturn(true)
|
||||
},
|
||||
matcher = j$.matchers.toThrowError(util),
|
||||
fn = function() {
|
||||
throw new Error();
|
||||
},
|
||||
result;
|
||||
|
||||
result = matcher.compare(fn, Error);
|
||||
|
||||
expect(result.pass).toBe(true);
|
||||
expect(result.message).toEqual("Expected function not to throw Error.");
|
||||
});
|
||||
|
||||
it("fails if thrown is an Error and the expected is a different Error", function() {
|
||||
var util = {
|
||||
equals: j$.createSpy('delegated-equal').andReturn(false)
|
||||
},
|
||||
matcher = j$.matchers.toThrowError(util),
|
||||
fn = function() {
|
||||
throw new Error();
|
||||
},
|
||||
result;
|
||||
|
||||
result = matcher.compare(fn, TypeError);
|
||||
|
||||
expect(result.pass).toBe(false);
|
||||
expect(result.message).toEqual("Expected function to throw TypeError.");
|
||||
});
|
||||
|
||||
it("passes if thrown is an Error and it is equal to the expected Error and message", function() {
|
||||
var util = {
|
||||
equals: j$.createSpy('delegated-equal').andReturn(true)
|
||||
},
|
||||
matcher = j$.matchers.toThrowError(util),
|
||||
fn = function() {
|
||||
throw new Error("foo");
|
||||
},
|
||||
result;
|
||||
|
||||
result = matcher.compare(fn, Error, "foo");
|
||||
|
||||
expect(result.pass).toBe(true);
|
||||
expect(result.message).toEqual("Expected function not to throw Error with message \"foo\".");
|
||||
});
|
||||
|
||||
it("fails if thrown is an Error and the expected is a different Error", function() {
|
||||
var util = {
|
||||
equals: j$.createSpy('delegated-equal').andReturn(false)
|
||||
},
|
||||
matcher = j$.matchers.toThrowError(util),
|
||||
fn = function() {
|
||||
throw new Error("foo");
|
||||
},
|
||||
result;
|
||||
|
||||
result = matcher.compare(fn, Error, "bar");
|
||||
|
||||
expect(result.pass).toBe(false);
|
||||
expect(result.message).toEqual("Expected function to throw Error with message \"bar\".");
|
||||
});
|
||||
|
||||
it("passes if thrown is an Error and has the same type as the expected Error and the message matches the exepcted message", function() {
|
||||
var util = {
|
||||
equals: j$.createSpy('delegated-equal').andReturn(true)
|
||||
},
|
||||
matcher = j$.matchers.toThrowError(util),
|
||||
fn = function() {
|
||||
throw new Error("foo");
|
||||
},
|
||||
result;
|
||||
|
||||
result = matcher.compare(fn, Error, /foo/);
|
||||
|
||||
expect(result.pass).toBe(true);
|
||||
expect(result.message).toEqual("Expected function not to throw Error with message matching /foo/.");
|
||||
});
|
||||
|
||||
it("fails if thrown is an Error and the expected is a different Error", function() {
|
||||
var util = {
|
||||
equals: j$.createSpy('delegated-equal').andReturn(false)
|
||||
},
|
||||
matcher = j$.matchers.toThrowError(util),
|
||||
fn = function() {
|
||||
throw new Error("foo");
|
||||
},
|
||||
result;
|
||||
|
||||
result = matcher.compare(fn, Error, /bar/);
|
||||
|
||||
expect(result.pass).toBe(false);
|
||||
expect(result.message).toEqual("Expected function to throw Error with message matching /bar/.");
|
||||
});
|
||||
});
|
||||
@@ -1,41 +1,16 @@
|
||||
describe("toThrow", function() {
|
||||
it("throw an error when the acutal is not a function ", function() {
|
||||
it("throws an error when the actual is not a function", function() {
|
||||
var matcher = j$.matchers.toThrow();
|
||||
|
||||
expect(function() {
|
||||
matcher.compare({});
|
||||
}).toThrow(new Error("Actual is not a Function"));
|
||||
}).toThrow(new Error("Actual is not a Function")); // TODO: this needs to change for self-test
|
||||
});
|
||||
|
||||
it("throws an error when the expected can't be turned into an exception", function() {
|
||||
it("fails if actual does not throw", function() {
|
||||
var matcher = j$.matchers.toThrow(),
|
||||
fn = function() {
|
||||
throw "foo";
|
||||
},
|
||||
result;
|
||||
|
||||
expect(function() {
|
||||
matcher.compare(fn, 1);
|
||||
}).toThrow(new Error("Expected cannot be treated as an exception."));
|
||||
});
|
||||
|
||||
it("passes if the actual throws any exception", function() {
|
||||
var matcher = j$.matchers.toThrow(),
|
||||
fn = function() {
|
||||
throw "foo";
|
||||
},
|
||||
result;
|
||||
|
||||
result = matcher.compare(fn);
|
||||
|
||||
expect(result.pass).toBe(true);
|
||||
expect(result.message).toEqual("Expected function not to throw an exception.");
|
||||
});
|
||||
|
||||
it("fails if the actual does not throw an exception", function() {
|
||||
var matcher = j$.matchers.toThrow(),
|
||||
fn = function() {
|
||||
return 0;
|
||||
return true;
|
||||
},
|
||||
result;
|
||||
|
||||
@@ -45,107 +20,79 @@ describe("toThrow", function() {
|
||||
expect(result.message).toEqual("Expected function to throw an exception.");
|
||||
});
|
||||
|
||||
it("passes if the actual throws an exception with the expected message", function() {
|
||||
it("passes if it throws but there is no expected", function() {
|
||||
var util = {
|
||||
equals: j$.createSpy('delegated-equal').andReturn(true)
|
||||
},
|
||||
matcher = j$.matchers.toThrow(util),
|
||||
fn = function() {
|
||||
throw 5;
|
||||
},
|
||||
result;
|
||||
|
||||
result = matcher.compare(fn);
|
||||
|
||||
expect(result.pass).toBe(true);
|
||||
expect(result.message).toEqual("Expected function not to throw.");
|
||||
});
|
||||
|
||||
it("passes even if what is thrown is falsy", function() {
|
||||
var matcher = j$.matchers.toThrow(),
|
||||
fn = function() {
|
||||
throw "foo";
|
||||
throw undefined;
|
||||
},
|
||||
result;
|
||||
|
||||
result = matcher.compare(fn);
|
||||
expect(result.pass).toBe(true);
|
||||
expect(result.message).toEqual("Expected function not to throw.");
|
||||
});
|
||||
|
||||
it("passes if what is thrown is equivalent to what is expected", function() {
|
||||
var util = {
|
||||
equals: j$.createSpy('delegated-equal').andReturn(true)
|
||||
},
|
||||
matcher = j$.matchers.toThrow(util),
|
||||
fn = function() {
|
||||
throw 5;
|
||||
},
|
||||
result;
|
||||
|
||||
result = matcher.compare(fn, 5);
|
||||
|
||||
expect(result.pass).toBe(true);
|
||||
expect(result.message).toEqual("Expected function not to throw 5.");
|
||||
});
|
||||
|
||||
it("fails if what is thrown is not equivalent to what is expected", function() {
|
||||
var util = {
|
||||
equals: j$.createSpy('delegated-equal').andReturn(false)
|
||||
},
|
||||
matcher = j$.matchers.toThrow(util),
|
||||
fn = function() {
|
||||
throw 5;
|
||||
},
|
||||
result;
|
||||
|
||||
result = matcher.compare(fn, "foo");
|
||||
|
||||
expect(result.pass).toBe(true);
|
||||
expect(result.message).toEqual("Expected function not to throw an exception \"foo\".");
|
||||
expect(result.pass).toBe(false);
|
||||
expect(result.message).toEqual("Expected function to throw 'foo'.");
|
||||
});
|
||||
|
||||
it("fails if the actual throws an exception with a different message", function() {
|
||||
var matcher = j$.matchers.toThrow(),
|
||||
it("fails if what is thrown is not equivalent to undefined", function() {
|
||||
var util = {
|
||||
equals: j$.createSpy('delegated-equal').andReturn(false)
|
||||
},
|
||||
matcher = j$.matchers.toThrow(util),
|
||||
fn = function() {
|
||||
throw "foo";
|
||||
throw 5;
|
||||
},
|
||||
result;
|
||||
|
||||
result = matcher.compare(fn, "bar");
|
||||
result = matcher.compare(fn, void 0);
|
||||
|
||||
expect(result.pass).toBe(false);
|
||||
expect(result.message).toEqual("Expected function to throw an exception \"bar\".");
|
||||
});
|
||||
|
||||
it("passes if the actual throws an exception and matches the message of the expected exception", function() {
|
||||
var matcher = j$.matchers.toThrow(),
|
||||
fn = function() {
|
||||
throw "foo";
|
||||
},
|
||||
result;
|
||||
|
||||
result = matcher.compare(fn, new Error("foo"));
|
||||
|
||||
expect(result.pass).toBe(true);
|
||||
expect(result.message).toEqual("Expected function not to throw an exception \"foo\".");
|
||||
});
|
||||
|
||||
it("fails if the actual throws an exception and it does not match the message of the expected exception with a custom message", function() {
|
||||
var matcher = j$.matchers.toThrow(),
|
||||
fn = function() {
|
||||
throw "foo";
|
||||
},
|
||||
result;
|
||||
|
||||
result = matcher.compare(fn, new Error("bar"));
|
||||
|
||||
expect(result.pass).toBe(false);
|
||||
expect(result.message).toEqual("Expected function to throw an exception \"bar\".");
|
||||
});
|
||||
|
||||
it("passes if the actual throws an exception and the message matches the expected regular expression", function() {
|
||||
var matcher = j$.matchers.toThrow(),
|
||||
fn = function() {
|
||||
throw "a long message";
|
||||
},
|
||||
result;
|
||||
|
||||
result = matcher.compare(fn, /long/);
|
||||
|
||||
expect(result.pass).toBe(true);
|
||||
expect(result.message).toEqual("Expected function not to throw an exception matching /long/.");
|
||||
});
|
||||
|
||||
it("fails if the actual throws an exception and the message does not match the expected regular expression", function() {
|
||||
var matcher = j$.matchers.toThrow(),
|
||||
fn = function() {
|
||||
throw "a long message";
|
||||
},
|
||||
result;
|
||||
|
||||
result = matcher.compare(fn, /short/);
|
||||
|
||||
expect(result.pass).toBe(false);
|
||||
expect(result.message).toEqual("Expected function to throw an exception matching /short/.");
|
||||
});
|
||||
|
||||
it("passes if the actual throws an exception with an undefined message", function() {
|
||||
var matcher = j$.matchers.toThrow(),
|
||||
fn = function() {
|
||||
throw void 0;
|
||||
},
|
||||
result;
|
||||
|
||||
result = matcher.compare(fn);
|
||||
|
||||
expect(result.pass).toBe(true);
|
||||
expect(result.message).toEqual("Expected function not to throw an exception.");
|
||||
});
|
||||
|
||||
it("passes if the actual throws an exception with an empty message", function() {
|
||||
var matcher = j$.matchers.toThrow(),
|
||||
fn = function() {
|
||||
throw "";
|
||||
},
|
||||
result;
|
||||
|
||||
result = matcher.compare(fn);
|
||||
|
||||
expect(result.pass).toBe(true);
|
||||
expect(result.message).toEqual("Expected function not to throw an exception.");
|
||||
expect(result.message).toEqual("Expected function to throw undefined.");
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user