Add support for choosing what stack depth to report in test failures

This gives a much more useful test failure message when an assertion is called
from within a function.
This commit is contained in:
Thomas Goyne 2017-09-13 14:52:01 -07:00
parent 2efa71ebfb
commit 0167e60142
1 changed files with 54 additions and 53 deletions

View File

@ -19,75 +19,74 @@
'use strict';
module.exports = {
assertEqual: function(val1, val2, errorMessage) {
assertEqual: function(val1, val2, errorMessage, depth) {
if (val1 !== val2) {
var message = "'" + val1 + "' does not equal expected value '" + val2 + "'";
let message = `'${val1}' does not equal expected value '${val2}'`;
if (errorMessage) {
message = errorMessage + ' - ' + message;
message = `${errorMessage} - ${message}`;
}
throw new TestFailureError(message);
throw new TestFailureError(message, depth);
}
},
assertNotEqual: function(val1, val2, errorMessage) {
assertNotEqual: function(val1, val2, errorMessage, depth) {
if (val1 === val2) {
var message = "'" + val1 + "' equals '" + val2 + "'";
let message = `'${val1}' equals '${val2}'`;
if (errorMessage) {
message = errorMessage + ' - ' + message;
message = `${errorMessage} - ${message}`;
}
throw new TestFailureError(message);
throw new TestFailureError(message, depth);
}
},
assertEqualWithTolerance: function(val1, val2, tolerance, errorMessage) {
assertEqualWithTolerance: function(val1, val2, tolerance, errorMessage, depth) {
if (val1 < val2 - tolerance || val1 > val2 + tolerance) {
var message = "'" + val1 + "' does not equal '" + val2 + "' with tolerance '" + tolerance + "'";
let message = `'${val1}' does not equal '${val2}' with tolerance '${tolerance}'`;
if (errorMessage) {
message = errorMessage + ' - ' + message;
message = `${errorMessage} - ${message}`;
}
throw new TestFailureError(message);
throw new TestFailureError(message, depth);
}
},
assertArray: function(value, length, errorMessage) {
assertArray: function(value, length, errorMessage, depth) {
if (!Array.isArray(value)) {
throw new TestFailureError(errorMessage || `Value ${value} is not an array`);
throw new TestFailureError(errorMessage || `Value ${value} is not an array`, depth);
}
},
assertArrayLength: function(value, length, errorMessage) {
this.assertArray(value);
assertArrayLength: function(value, length, errorMessage, depth) {
this.assertArray(value, 1 + depth || 0);
if (value.length !== length) {
throw new TestFailureError(errorMessage || `Value ${value} is not an array of length ${length}`);
throw new TestFailureError(errorMessage || `Value ${value} is not an array of length ${length}`, depth);
}
},
assertArraysEqual: function(val1, val2, errorMessage) {
var len1 = val1.length;
var len2 = val2.length;
var message;
assertArraysEqual: function(val1, val2, errorMessage, depth) {
const len1 = val1.length;
const len2 = val2.length;
if (len1 !== len2) {
message = 'Arrays have different lengths (' + len1 + ' != ' + len2 + ')';
let message = `Arrays (${val1}) and (${val2}) have different lengths (${len1} != ${len2})`;
if (errorMessage) {
message = errorMessage + ' - ' + message;
message = `${errorMessage} - ${message}`;
}
throw new TestFailureError(message);
throw new TestFailureError(message, depth);
}
for (var i = 0; i < len1; i++) {
for (let i = 0; i < len1; i++) {
if (val1[i] !== val2[i]) {
message = 'Array contents not equal at index ' + i + ' (' + val1[i] + ' != ' + val2[i] + ')';
let message = `Array contents not equal at index ${i} (${val1[i]} != ${val2[i]})`;
if (errorMessage) {
message = errorMessage + ' - ' + message;
message = `${errorMessage} - ${message}`;
}
throw new TestFailureError(message);
throw new TestFailureError(message, depth);
}
}
},
assertThrows: function(func, errorMessage) {
var caught = false;
assertThrows: function(func, errorMessage, depth) {
let caught = false;
try {
func();
}
@ -96,22 +95,22 @@ module.exports = {
}
if (!caught) {
throw new TestFailureError(errorMessage || 'Expected exception not thrown');
throw new TestFailureError(errorMessage || 'Expected exception not thrown', depth);
}
},
assertThrowsException: function(func, expectedException) {
var caught = false;
let caught = false;
try {
func();
}
catch (e) {
caught = true;
if (e.name !== expectedException.name) {
throw new TestFailureError('Expected a ' + expectedException.name + ' exception but caught a ' + e.name + ' instead. Message was: ' + e.message);
throw new TestFailureError(`Expected a ${expectedException.name} exception but caught a ${e.name} instead. Message was: ${e.message}`);
}
if (e.message != expectedException.message) {
throw new TestFailureError('Expected exception "' + expectedException + '" not thrown - instead caught: "' + e + '"');
throw new TestFailureError(`Expected exception "${expectedException}" not thrown - instead caught: "${e}"`);
}
}
@ -120,7 +119,7 @@ module.exports = {
}
},
assertThrowsContaining: function(func, expectedMessage) {
assertThrowsContaining: function(func, expectedMessage, depth) {
var caught = false;
try {
func();
@ -128,40 +127,40 @@ module.exports = {
catch (e) {
caught = true;
if (!e.message.includes(expectedMessage)) {
throw new TestFailureError(`Expected exception "${expectedMessage}" not thrown - instead caught: "${e}"`);
throw new TestFailureError(`Expected exception "${expectedMessage}" not thrown - instead caught: "${e}"`, depth);
}
}
if (!caught) {
throw new TestFailureError(`Expected exception "${expectedMessage}" not thrown`);
throw new TestFailureError(`Expected exception "${expectedMessage}" not thrown`, depth);
}
},
assertTrue: function(condition, errorMessage) {
assertTrue: function(condition, errorMessage, depth) {
if (!condition) {
throw new TestFailureError(errorMessage || `Condition ${condition} expected to be true`);
throw new TestFailureError(errorMessage || `Condition ${condition} expected to be true`, depth);
}
},
assertInstanceOf: function(object, type, errorMessage) {
assertInstanceOf: function(object, type, errorMessage, depth) {
if (!(object instanceof type)) {
throw new TestFailureError(errorMessage || `Object ${object} expected to be of type ${type}`);
throw new TestFailureError(errorMessage || `Object ${object} expected to be of type ${type}`, depth);
}
},
assertType: function(value, type) {
this.assertEqual(typeof value, type, `Value ${value} expected to be of type ${type}`);
assertType: function(value, type, depth) {
this.assertEqual(typeof value, type, `Value ${value} expected to be of type ${type}`, 1 + depth || 0);
},
assertUndefined: function(value, errorMessage) {
assertUndefined: function(value, errorMessage, depth) {
if (value !== undefined) {
throw new TestFailureError(errorMessage || `Value ${value} expected to be undefined`);
throw new TestFailureError(errorMessage || `Value ${value} expected to be undefined`, depth);
}
},
assertNull: function(value, errorMessage) {
assertNull: function(value, errorMessage, depth) {
if (value !== null) {
throw new TestFailureError(errorMessage || `Value ${value} expected to be null`);
throw new TestFailureError(errorMessage || `Value ${value} expected to be null`, depth);
}
},
@ -176,26 +175,28 @@ module.exports = {
},
};
function TestFailureError(message) {
var error;
function TestFailureError(message, depth) {
let error;
try {
throw new Error(message);
} catch (e) {
error = e;
}
depth = 2 + (depth || 0);
// This regular expression will match stack trace lines provided by JavaScriptCore.
// Example: someMethod@file:///path/to/file.js:10:24
var regex = /^(?:.*?@)?([^\[\(].+?):(\d+)(?::(\d+))?\s*$/;
const regex = /^(?:.*?@)?([^\[\(].+?):(\d+)(?::(\d+))?\s*$/;
// Remove the top two stack frames and use information from the third, if possible.
var stack = error.stack && error.stack.split('\n');
var match = stack[2] && stack[2].match(regex);
const stack = error.stack && error.stack.split('\n');
const match = stack[depth] && stack[depth].match(regex);
if (match) {
this.sourceURL = match[1];
this.line = +match[2];
this.column = +match[3];
this.stack = stack.slice(2).join('\n');
this.stack = stack.slice(depth).join('\n');
}
this.__proto__ = error;