Cleanup stack traces for test assertions

This commit is contained in:
kpdecker
2015-08-14 15:20:09 -05:00
parent 3ce04ddd23
commit 624a4c71aa
+22 -6
View File
@@ -1,11 +1,27 @@
var AssertError;
if (Error.captureStackTrace) {
AssertError = function AssertError(message, caller) {
Error.prototype.constructor.call(this, message);
this.message = message;
if (Error.captureStackTrace) {
Error.captureStackTrace(this, caller || AssertError);
}
};
AssertError.prototype = new Error();
} else {
AssertError = Error;
}
global.shouldCompileTo = function(string, hashOrArray, expected, message) {
shouldCompileToWithPartials(string, hashOrArray, false, expected, message);
};
global.shouldCompileToWithPartials = function(string, hashOrArray, partials, expected, message) {
global.shouldCompileToWithPartials = function shouldCompileToWithPartials(string, hashOrArray, partials, expected, message) {
var result = compileWithPartials(string, hashOrArray, partials);
if (result !== expected) {
throw new Error("'" + result + "' should === '" + expected + "': " + message);
throw new AssertError("'" + result + "' should === '" + expected + "': " + message, shouldCompileToWithPartials);
}
};
@@ -31,9 +47,9 @@ global.compileWithPartials = function(string, hashOrArray, partials) {
};
global.equals = global.equal = function(a, b, msg) {
global.equals = global.equal = function equals(a, b, msg) {
if (a !== b) {
throw new Error("'" + a + "' should === '" + b + "'" + (msg ? ': ' + msg : ''));
throw new AssertError("'" + a + "' should === '" + b + "'" + (msg ? ': ' + msg : ''), equals);
}
};
@@ -44,13 +60,13 @@ global.shouldThrow = function(callback, type, msg) {
failed = true;
} catch (err) {
if (type && !(err instanceof type)) {
throw new Error('Type failure: ' + err);
throw new AssertError('Type failure: ' + err);
}
if (msg && !(msg.test ? msg.test(err.message) : msg === err.message)) {
equal(msg, err.message);
}
}
if (failed) {
throw new Error('It failed to throw');
throw new AssertError('It failed to throw', shouldThrow);
}
};