The open and composable observability and data visualization platform. Visualize metrics, logs, and traces from multiple sources like Prometheus, Loki, Elasticsearch, InfluxDB, Postgres and many more.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
grafana/public/test/log-reporter.js

83 lines
2.0 KiB

class LogReporter {
constructor(globalConfig, reporterOptions, reporterContext) {
this._globalConfig = globalConfig;
this._options = reporterOptions;
this._context = reporterContext;
}
onRunComplete(testContexts, results) {
if (!this._options.enable) {
return;
}
this.logStats(results);
this.logTestFailures(results);
}
logTestFailures(results) {
results.testResults.forEach(printTestFailures);
}
logStats(results) {
const stats = {
suites: results.numTotalTestSuites,
tests: results.numTotalTests,
passes: results.numPassedTests,
pending: results.numPendingTests,
failures: results.numFailedTests,
duration: Date.now() - results.startTime,
};
// JestStats suites=1 tests=94 passes=93 pending=0 failures=1 duration=3973
console.log(`JestStats ${objToLogAttributes(stats)}`);
}
}
function printTestFailures(result) {
if (result.status === 'pending') {
return;
}
if (result.numFailingTests > 0) {
const testInfo = {
file: result.testFilePath,
failures: result.numFailingTests,
duration: result.perfStats.end - result.perfStats.start,
errorMessage: result.failureMessage,
};
// JestFailure file=<...>/public/app/features/dashboard/state/DashboardMigrator.test.ts
// failures=1 duration=3251 errorMessage="formatted error message"
console.log(`JestFailure ${objToLogAttributes(testInfo)}`);
}
}
/**
* Stringify object to be log friendly
* @param {Object} obj
* @returns {String}
*/
function objToLogAttributes(obj) {
return Object.entries(obj)
.map(([key, value]) => `${key}=${formatValue(value)}`)
.join(' ');
}
/**
* Escape double quotes
* @param {String} str
* @returns
*/
function escapeQuotes(str) {
return String(str).replaceAll('"', '\\"');
}
/**
* Wrap the value within double quote if needed
* @param {*} value
* @returns
*/
function formatValue(value) {
const hasWhiteSpaces = /\s/g.test(value);
return hasWhiteSpaces ? `"${escapeQuotes(value)}"` : value;
}
module.exports = LogReporter;