jest-mocha-reporter
Version:
A reporter for jest which produces a report compatible with Atlassian Bamboo Mocha Test Parser.
175 lines (149 loc) • 5.39 kB
JavaScript
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
var path = _interopDefault(require('path'));
var fs = _interopDefault(require('fs'));
var EOL = require('os').EOL;
var helpers = {
replaceCharsNotSupportedByBamboo: function replaceCharsNotSupportedByBamboo(s) {
return s.replace(/\./g, '_');
},
formatErrorMessages: function formatErrorMessages(errorMessages) {
var lines = [];
if (errorMessages.length === 1) {
lines.push('1 failure:');
} else {
lines.push(errorMessages.length + ' failures:');
}
errorMessages.forEach(function (message) {
lines.push('* ' + message);
});
return lines.join(EOL);
},
replaceVariables: function replaceVariables(template, variables) {
return template.split(/\{|\}/).filter(function (segment) {
return segment;
}).map(function (segment) {
return segment.split('|').reduce(function (result, maybeVariable) {
// Allow variables to fallback
if (maybeVariable in variables) {
return result ? result : variables[maybeVariable];
}
return maybeVariable;
}, '');
}).join('');
}
};
//
/* eslint-disable no-unused-vars */
/**
* A function that is used to process our jest reports.
* @param report
* @param contexts
* @returns {AggregatedResultWithoutCoverage}
*/
function reporter(report, contexts) {
/* eslint-enable no-unused-vars */
var output = {
stats: {},
failures: [],
passes: [],
skipped: []
};
/**
* We will need to update this support defining a configuration.
*/
var filename = process.env.JEST_MOCHA_FILE || 'test-report.json';
var suiteNameTemplate = process.env.JEST_MOCHA_SUITE_NAME || '{firstAncestorTitle|filePath}';
var nameSeparator = process.env.JEST_MOCHA_NAME_SEPARATOR || ' – ';
output.stats.tests = report.numTotalTests;
output.stats.passes = report.numPassedTests;
output.stats.failures = report.numFailedTests;
output.stats.duration = Date.now() - report.startTime;
output.stats.start = new Date(report.startTime);
output.stats.end = new Date();
var existingTestTitles = Object.create(null);
report.testResults.forEach(function (suiteResult) {
var testFileName = path.basename(suiteResult.testFilePath);
if (suiteResult.failureMessage && suiteResult.testResults.length === 0) {
var suiteName = helpers.replaceCharsNotSupportedByBamboo(helpers.replaceVariables(suiteNameTemplate, {
firstAncestorTitle: suiteResult.displayName,
filePath: suiteResult.testFilePath,
fileName: testFileName,
fileNameWithoutExtension: path.parse(testFileName).name
}));
output.failures.push({
title: suiteName,
fullTitle: suiteName,
duration: suiteResult.perfStats.end - suiteResult.perfStats.start,
errorCount: 1,
error: suiteResult.failureMessage
});
}
suiteResult.testResults.forEach(function (testResult) {
var suiteName = helpers.replaceCharsNotSupportedByBamboo(helpers.replaceVariables(suiteNameTemplate, {
firstAncestorTitle: testResult.ancestorTitles[0],
filePath: suiteResult.testFilePath,
fileName: testFileName,
fileNameWithoutExtension: path.parse(testFileName).name
}));
var testTitle = helpers.replaceCharsNotSupportedByBamboo(testResult.ancestorTitles.concat([testResult.title]).join(nameSeparator));
if (testTitle in existingTestTitles) {
var newTestTitle;
var counter = 1;
do {
counter++;
newTestTitle = testTitle + ' (' + counter + ')';
} while (newTestTitle in existingTestTitles);
testTitle = newTestTitle;
}
existingTestTitles[testTitle] = true;
var result = {
title: testTitle,
fullTitle: suiteName,
duration: suiteResult.perfStats.end - suiteResult.perfStats.start,
errorCount: testResult.failureMessages.length,
error: testResult.failureMessages.length ? helpers.formatErrorMessages(testResult.failureMessages) : undefined
};
switch (testResult.status) {
case 'passed':
output.passes.push(result);
break;
case 'failed':
output.failures.push(result);
break;
case 'pending':
output.skipped.push(result);
break;
default:
throw new Error('Unexpected test result status: ' + testResult.status);
}
});
});
fs.writeFileSync(filename, JSON.stringify(output, null, 2), 'utf8');
return report;
}
//
/**
* The function used by jest to act as either a reporter or testResultsProcessor.
* In this function we handle direction to either the supported reporter
* or we direct to testResultsProcessor.
* @param globalConfig
* @returns {AggregatedResultWithoutCoverage}
* @constructor
*/
function JestMochaReporter(globalConfig) {
/**
* Backwards compatibility for testResultsProcessor
*/
if (globalConfig && globalConfig.hasOwnProperty('testResults')) {
/**
* If we don't type cast, flow complains :|
*/
var report = globalConfig;
return reporter(report);
}
this.onRunComplete = function (contexts, report) {
return reporter(report, contexts);
};
}
module.exports = JestMochaReporter;
;