import { stripVTControlCharacters as stripAnsi, styleText } from 'node:util'; import { toPosixPath } from './toPosixPath.js'; export type ConsoleType = 'error' | 'group' | 'info' | 'log' | 'table' | 'warn'; type LogPattern = string | RegExp | ((log: string) => boolean); type MatchPatternOptions = { /** * Whether to use exact line matching instead of substring matching. * @default false */ strict?: boolean; /** * Whether to convert file paths to POSIX format before matching. * @default false */ posix?: boolean; }; const matchPattern = ( log: string, pattern: LogPattern, options: MatchPatternOptions = {}, ) => { const logToCheck = options.posix ? toPosixPath(log) : log; if (typeof pattern === 'string') { return options.strict ? logToCheck.split('\n').some((line) => line.trim() === pattern.trim()) : logToCheck.includes(pattern); } if (pattern instanceof RegExp) { return pattern.test(logToCheck); } return pattern(logToCheck); }; export const createLogHelper = () => { const logs: string[] = []; const originalLogs: string[] = []; let rawOutput = ''; const logPatterns = new Set<{ pattern: LogPattern; resolve: (value: boolean) => void; options: MatchPatternOptions; }>(); const clearLogs = () => { logs.splice(0); rawOutput = ''; }; const addLog = (input: string, options?: { newline?: boolean }) => { const log = stripAnsi(input); logs.push(log); originalLogs.push(input); rawOutput += options?.newline ? `${input}\n` : input; for (const { pattern, resolve, options } of logPatterns) { if (matchPattern(log, pattern, options)) { resolve(true); } } }; const expectLog = async ( pattern: LogPattern, options: MatchPatternOptions = {}, ) => { if (logs.some((log) => matchPattern(log, pattern, options))) { return true; } return new Promise((resolve, reject) => { const timeoutId = setTimeout(() => { const title = styleText( ['bold', 'red'], 'Timeout: Expected log not found within 5 seconds.', ); const expected = styleText('yellow', pattern.toString()); reject( new Error( `${title}\nExpect: ${expected}\nGet:\n${originalLogs.join('\n')}`, ), ); }, 5000); const patternEntry = { pattern, options, resolve: (value: boolean) => { clearTimeout(timeoutId); logPatterns.delete(patternEntry); resolve(value); }, }; logPatterns.add(patternEntry); }); }; const expectNoLog = ( pattern: LogPattern, options: MatchPatternOptions = {}, ) => { const result = logs.some((log) => matchPattern(log, pattern, options)); if (result) { const title = styleText(['bold', 'red'], 'Unexpected log found.'); const unexpected = styleText('yellow', pattern.toString()); throw new Error( `${title}\nUnexpected: ${unexpected}\nGet:\n${originalLogs.join('\n')}`, ); } }; /** Assert the number of non-overlapping matches in the captured output. */ const expectLogTimes = (pattern: string | RegExp, times: number) => { const output = stripAnsi(rawOutput); let actualTimes = 0; if (typeof pattern === 'string') { let position = 0; while (position <= output.length) { position = output.indexOf(pattern, position); if (position === -1) { break; } actualTimes++; position += pattern.length || 1; } } else { const regexp = new RegExp( pattern.source, pattern.flags.includes('g') ? pattern.flags : `${pattern.flags}g`, ); actualTimes = output.match(regexp)?.length ?? 0; } if (actualTimes !== times) { const title = styleText(['bold', 'red'], 'Unexpected log count.'); const expected = styleText('yellow', pattern.toString()); throw new Error( `${title}\nPattern: ${expected}\nExpected: ${times}\nReceived: ${actualTimes}\nGet:\n${originalLogs.join('\n')}`, ); } }; return { logs, originalLogs, addLog, clearLogs, expectLog, expectNoLog, expectLogTimes, }; }; export type LogHelper = ReturnType; export type ProxyConsoleOptions = { types?: ConsoleType | ConsoleType[]; }; export type ExtendedLogHelper = LogHelper & { /** Restore the original console methods. */ restore: () => void; /** Restore the original console methods and print the captured logs. */ printCapturedLogs: () => void; }; /** Proxy the console methods to capture logs. */ export const proxyConsole = ({ types = ['log', 'warn', 'info', 'error'], }: ProxyConsoleOptions = {}): ExtendedLogHelper => { const restores: Array<() => void> = []; const logHelper = createLogHelper(); for (const type of Array.isArray(types) ? types : [types]) { const method = console[type]; restores.push(() => { console[type] = method; }); console[type] = (...args: unknown[]) => { const logMessage = args .map((arg) => { if (typeof arg === 'string') { return arg; } return typeof arg === 'object' ? JSON.stringify(arg) : String(arg); }) .join(' '); logHelper.addLog(logMessage, { newline: true }); }; } const restore = () => { for (const restoreMethod of restores) { restoreMethod(); } }; const printCapturedLogs = () => { restore(); console.log(logHelper.originalLogs.join('\n')); }; return { restore, printCapturedLogs, ...logHelper, }; };