feat(logging): Display failed test console logs in default mode

This commit is contained in:
2025-05-15 19:40:46 +00:00
parent 553d5f0df7
commit 42cd08eb1c
7 changed files with 328 additions and 38 deletions

View File

@ -3,6 +3,6 @@
*/
export const commitinfo = {
name: '@git.zone/tstest',
version: '1.3.1',
version: '1.4.0',
description: 'a test utility to run tests that match test/**/*.ts'
}

View File

@ -18,6 +18,8 @@ export class TapParser {
testStatusRegex = /(ok|not\sok)\s([0-9]+)\s-\s(.*)\s#\stime=(.*)ms$/;
activeTapTestResult: TapTestResult;
collectingErrorDetails: boolean = false;
currentTestError: string[] = [];
pretaskRegex = /^::__PRETASK:(.*)$/;
@ -91,6 +93,9 @@ export class TapParser {
this.logger.testResult(testSubject, true, testDuration);
}
} else {
// Start collecting error details for failed test
this.collectingErrorDetails = true;
this.currentTestError = [];
if (this.logger) {
this.logger.testResult(testSubject, false, testDuration);
}
@ -101,13 +106,43 @@ export class TapParser {
if (this.activeTapTestResult) {
this.activeTapTestResult.addLogLine(logLine);
}
if (this.logger) {
// This is console output from the test file, not TAP protocol
this.logger.testConsoleOutput(logLine);
// Check if we're collecting error details
if (this.collectingErrorDetails) {
// Check if this line is an error detail (starts with Error: or has stack trace characteristics)
if (logLine.trim().startsWith('Error:') || logLine.trim().match(/^\s*at\s/)) {
this.currentTestError.push(logLine);
} else if (this.currentTestError.length > 0) {
// End of error details, show the error
const errorMessage = this.currentTestError.join('\n');
if (this.logger) {
this.logger.testErrorDetails(errorMessage);
}
this.collectingErrorDetails = false;
this.currentTestError = [];
}
}
// Don't output TAP error details as console output when we're collecting them
if (!this.collectingErrorDetails || (!logLine.trim().startsWith('Error:') && !logLine.trim().match(/^\s*at\s/))) {
if (this.logger) {
// This is console output from the test file, not TAP protocol
this.logger.testConsoleOutput(logLine);
}
}
}
if (this.activeTapTestResult && this.activeTapTestResult.testSettled) {
// Ensure any pending error is shown before settling the test
if (this.collectingErrorDetails && this.currentTestError.length > 0) {
const errorMessage = this.currentTestError.join('\n');
if (this.logger) {
this.logger.testErrorDetails(errorMessage);
}
this.collectingErrorDetails = false;
this.currentTestError = [];
}
this.testStore.push(this.activeTapTestResult);
this._getNewTapTestResult();
}

View File

@ -40,6 +40,8 @@ export class TsTestLogger {
private fileResults: TestFileResult[] = [];
private currentFileResult: TestFileResult | null = null;
private currentTestLogFile: string | null = null;
private currentTestLogs: string[] = []; // Buffer for current test logs
private currentTestFailed: boolean = false;
constructor(options: LogOptions = {}) {
this.options = options;
@ -145,6 +147,10 @@ export class TsTestLogger {
tests: []
};
// Reset test-specific state
this.currentTestLogs = [];
this.currentTestFailed = false;
// Only set up test log file if --logfile option is specified
if (this.options.logFile) {
const baseFilename = path.basename(filename, '.ts');
@ -179,6 +185,7 @@ export class TsTestLogger {
this.currentFileResult.passed++;
} else {
this.currentFileResult.failed++;
this.currentTestFailed = true;
}
this.currentFileResult.duration += duration;
}
@ -188,6 +195,14 @@ export class TsTestLogger {
return;
}
// If test failed and we have buffered logs, show them now
if (!passed && this.currentTestLogs.length > 0 && !this.options.verbose) {
this.log(this.format(' 📋 Console output from failed test:', 'yellow'));
this.currentTestLogs.forEach(logMessage => {
this.log(this.format(` ${logMessage}`, 'dim'));
});
}
const icon = passed ? '✅' : '❌';
const color = passed ? 'green' : 'red';
@ -199,6 +214,9 @@ export class TsTestLogger {
this.log(this.format(` ${error}`, 'red'));
}
}
// Clear logs after each test
this.currentTestLogs = [];
}
testFileEnd(passed: number, failed: number, duration: number) {
@ -242,9 +260,12 @@ export class TsTestLogger {
testConsoleOutput(message: string) {
if (this.options.json) return;
// Show console output from test files only in verbose mode
// In verbose mode, show console output immediately
if (this.options.verbose) {
this.log(this.format(` ${message}`, 'dim'));
} else {
// In non-verbose mode, buffer the logs
this.currentTestLogs.push(message);
}
// Always log to test file if --logfile is specified
@ -267,6 +288,26 @@ export class TsTestLogger {
}
}
// Test error details display
testErrorDetails(errorMessage: string) {
if (this.options.json) {
this.logJson({ event: 'testError', error: errorMessage });
return;
}
if (!this.options.quiet) {
this.log(this.format(' Error details:', 'red'));
errorMessage.split('\n').forEach(line => {
this.log(this.format(` ${line}`, 'red'));
});
}
// Always log to test file if --logfile is specified
if (this.currentTestLogFile) {
this.logToTestFile(` Error: ${errorMessage}`);
}
}
// Final summary
summary() {
const totalDuration = Date.now() - this.startTime;