@typed/test
Version:
Simple test framework
37 lines • 1.45 kB
JavaScript
import { bold, green, red } from 'typed-colors';
import { diffJson } from 'diff';
export function createDiff(assertionError) {
var message = assertionError.message, actual = assertionError.actual, expected = assertionError.expected;
var str = bold("AssertionError: " + message + "\n") + (" " + red('- expected') + " " + green('+ actual') + "\n\n");
if (typeof actual === 'object' && typeof expected === 'object')
str += formatDiff(diffJson(actual, expected));
else
str += JSON.stringify(expected) + " !== " + JSON.stringify(actual) + "\n";
return str;
}
function formatDiff(diffResults) {
var diffString = ' ';
for (var _i = 0, diffResults_1 = diffResults; _i < diffResults_1.length; _i++) {
var result = diffResults_1[_i];
if (!result.hasOwnProperty('added') && !result.hasOwnProperty('removed')) {
diffString += "" + result.value;
}
else {
var removed = result.removed, value = result.value;
if (removed) {
diffString += green('+') + " " + dropFirstTwoSpaces(value);
}
else {
diffString += red('-') + " " + dropFirstTwoSpaces(value);
}
}
}
return padNewLine(diffString) + '\n';
}
function padNewLine(str) {
return str.replace(/(\n)/g, "\n ");
}
function dropFirstTwoSpaces(str) {
return str.slice(2);
}
//# sourceMappingURL=createDiff.js.map