qunit-harness
Version:
A library for running qunit tests on a local machine and in the SauceLabs environment.
100 lines (72 loc) • 3.55 kB
JavaScript
;
var _interopRequireDefault = require('babel-runtime/helpers/interop-require-default').default;
exports.__esModule = true;
var _chalk = require('chalk');
var _chalk2 = _interopRequireDefault(_chalk);
function checkFailures(results) {
var globalErrors = [];
var testErrors = [];
results.forEach(function (_ref) {
var platform = _ref.platform;
var result = _ref.result;
var msg = [];
var runningError = !result || typeof result === 'string';
var failed = runningError || result.failed;
msg.push(_chalk2.default.bold(failed ? _chalk2.default.red('FAILURES:') : _chalk2.default.green('OK:')));
msg.push(_chalk2.default.bold('Worker:'));
msg.push('"' + platform + '"');
if (runningError) {
var resultErrorMessage = 'There is no test result available.';
msg.push(_chalk2.default.bold(resultErrorMessage));
globalErrors.push(resultErrorMessage);
} else {
msg.push(_chalk2.default.bold('Total:'), result.total);
msg.push(_chalk2.default.bold('Failed:'), result.failed);
var tests = result.reports.reduce(function (prev, curr) {
return prev.concat(curr.result.tests);
}, []);
if (tests.some(function (test) {
return !!test.failed;
})) msg.push(_chalk2.default.bold(_chalk2.default.red('\nERRORS:')));
tests.forEach(function (test) {
if (!test.failed) return;
test.assertions.forEach(function (assertion) {
if (assertion.failed) {
var assertionName = assertion.name ? '\n' + _chalk2.default.bold('Assertion:') + ' "' + assertion.name + '"' : '';
var moduleName = assertion.module ? '\n' + _chalk2.default.bold('Module:') + '} "' + assertion.module + '}"' : '';
var source = assertion.source ? '\n' + assertion.source.replace(/-----> \\n/g, '\n') : '';
msg.push('\n"' + test.name + '" failed.' + moduleName + assertionName + '\n\n' + _chalk2.default.bold('Message:') + ' ' + assertion.message + '\n' + _chalk2.default.bold('Expected:') + ' ' + assertion.expected + '\n' + _chalk2.default.bold('Actual:') + ' ' + assertion.actual + '\n' + source + '\n');
}
});
});
}
console.log(msg.join(' '));
});
return { globalErrors: globalErrors, testErrors: testErrors };
}
function reportTestFailures(errors) {
console.log(_chalk2.default.bold.red('ERRORS:'));
errors.forEach(function (error) {
console.log(_chalk2.default.bold(error.platform + ' - ' + error.testPath));
console.log(_chalk2.default.bold('Test: ' + error.testName));
if (error.customMessage) console.log('message: ' + error.customMessage);
if (error.expected) {
console.log('expected: ' + error.expected);
console.log('actual: ' + error.actual);
}
console.log('-------------------------------------------');
console.log();
});
}
exports.default = function (report) {
var _checkFailures = checkFailures(report);
var testErrors = _checkFailures.testErrors;
var globalErrors = _checkFailures.globalErrors;
if (globalErrors.length) return false;
if (testErrors.length) {
reportTestFailures(testErrors);
return false;
}
return true;
};
module.exports = exports.default;