Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add timestamps for logs into the functional tests reports #24509

16 changes: 14 additions & 2 deletions src/functional_test_runner/lib/mocha/reporter/reporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { format } from 'util';

import Mocha from 'mocha';
import { ToolingLogTextWriter } from '@kbn/dev-utils';
import moment from 'moment';

import { setupJUnitReportGeneration } from '../../../../dev';
import * as colors from './colors';
Expand All @@ -33,6 +34,7 @@ export function MochaReporterProvider({ getService }) {
const log = getService('log');
const config = getService('config');
let originalLogWriters;
let reporterCaptureStartTime;

return class MochaReporter extends Mocha.reporters.Base {
constructor(runner, options) {
Expand Down Expand Up @@ -60,7 +62,10 @@ export function MochaReporterProvider({ getService }) {
onStart = () => {
if (config.get('mochaReporter.captureLogOutput')) {
log.warning('debug logs are being captured, only error logs will be written to the console');

reporterCaptureStartTime = moment();
originalLogWriters = log.getWriters();

log.setWriters([
new ToolingLogTextWriter({
level: 'error',
Expand All @@ -69,7 +74,7 @@ export function MochaReporterProvider({ getService }) {
new ToolingLogTextWriter({
level: 'debug',
writeTo: {
write: (chunk) => {
write: (line) => {
// if the current runnable is a beforeEach hook then
// `runner.suite` is set to the suite that defined the
// hook, rather than the suite executing, so instead we
Expand All @@ -80,7 +85,14 @@ export function MochaReporterProvider({ getService }) {
? this.runner.test.parent
: this.runner.suite;

recordLog(currentSuite, chunk);
// We are computing the difference between the time when this
// reporter has started and the time when each line are being
// logged in order to be able to label the test results log lines
// with this relative time information
const diffTimeSinceStart = moment().diff(reporterCaptureStartTime);
const readableDiffTimeSinceStart = `[${moment(diffTimeSinceStart).format('HH:mm:ss')}] `;

recordLog(currentSuite, `${readableDiffTimeSinceStart} ${line}`);
}
}
})
Expand Down