CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-jest--console

Console utilities for Jest testing framework that provide custom console implementations including BufferedConsole for capturing output, CustomConsole for formatted logging, and NullConsole for suppressing output during tests

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

custom-console.mddocs/

CustomConsole

CustomConsole is a console implementation that outputs to custom streams with optional formatting, providing formatted console output with custom styling and message processing capabilities.

Capabilities

CustomConsole Class

Console implementation that extends Node.js Console to output to custom streams with optional message formatting.

/**
 * Function type for formatting console output messages
 * @param type - The type of log message
 * @param message - The message to format
 * @returns Formatted message string
 */
type Formatter = (type: LogType, message: LogMessage) => string;

/**
 * Console implementation that outputs to custom streams with optional formatting
 * Extends Node.js Console with custom stream and formatting capabilities
 */
class CustomConsole extends Console {
  /**
   * Creates a new CustomConsole instance
   * @param stdout - WriteStream for standard output
   * @param stderr - WriteStream for error output
   * @param formatBuffer - Optional formatter function for message processing
   */
  constructor(
    stdout: WriteStream,
    stderr: WriteStream,
    formatBuffer?: Formatter
  );
  
  /** Reference to the Node.js Console constructor */
  Console: typeof Console;
  
  /** Always returns undefined (no buffering in CustomConsole) */
  getBuffer(): undefined;
}

Usage Examples:

import { CustomConsole } from "@jest/console";
import type { WriteStream } from "tty";

// Basic usage with process streams
const console = new CustomConsole(process.stdout, process.stderr);
console.log("This goes to stdout");
console.error("This goes to stderr");

// With custom formatter
const formattedConsole = new CustomConsole(
  process.stdout,
  process.stderr,
  (type, message) => `[${type.toUpperCase()}] ${new Date().toISOString()}: ${message}`
);

formattedConsole.log("Hello, World!");
// Output: [LOG] 2023-01-01T12:00:00.000Z: Hello, World!

// With custom streams
import { Writable } from "stream";

let output = "";
const customStream = new Writable({
  write(chunk, encoding, callback) {
    output += chunk.toString();
    callback();
  }
}) as WriteStream;

const streamConsole = new CustomConsole(customStream, customStream);
streamConsole.log("Captured output");
console.log(output); // "Captured output\n"

Console Methods

All standard console methods are supported, with output directed to the appropriate streams.

Logging Methods

/**
 * Log a message to stdout
 * @param firstArg - Primary message or data to log
 * @param args - Additional arguments to format and log
 */
log(firstArg: unknown, ...args: Array<unknown>): void;

/**
 * Log an informational message to stdout
 * @param firstArg - Primary message or data to log
 * @param args - Additional arguments to format and log
 */
info(firstArg: unknown, ...args: Array<unknown>): void;

/**
 * Log a debug message to stdout
 * @param firstArg - Primary message or data to log
 * @param args - Additional arguments to format and log
 */
debug(firstArg: unknown, ...args: Array<unknown>): void;

/**
 * Log a warning message to stderr
 * @param firstArg - Primary message or data to log
 * @param args - Additional arguments to format and log
 */
warn(firstArg: unknown, ...args: Array<unknown>): void;

/**
 * Log an error message to stderr
 * @param firstArg - Primary message or data to log
 * @param args - Additional arguments to format and log
 */
error(firstArg: unknown, ...args: Array<unknown>): void;

Assertion Method

/**
 * Assert that a value is truthy, logging assertion failures to stderr
 * @param value - Value to test for truthiness
 * @param message - Optional message or Error object for assertion failure
 */
assert(value: unknown, message?: string | Error): asserts value;

Object Inspection Methods

/**
 * Log an object inspection to stdout
 * @param firstArg - Object to inspect
 * @param options - Optional inspection options from Node.js util.inspect
 */
dir(firstArg: unknown, options?: InspectOptions): void;

/**
 * Log XML-style object representation to stdout
 * @param firstArg - Primary object to log
 * @param args - Additional arguments to format and log
 */
dirxml(firstArg: unknown, ...args: Array<unknown>): void;

Grouping Methods

/**
 * Start a new console group with optional title, increasing indentation depth
 * @param title - Optional group title
 * @param args - Additional arguments to format in the title
 */
group(title?: string, ...args: Array<unknown>): void;

/**
 * Start a new collapsed console group with optional title
 * @param title - Optional group title
 * @param args - Additional arguments to format in the title
 */
groupCollapsed(title?: string, ...args: Array<unknown>): void;

/**
 * End the current console group, decreasing indentation depth
 */
groupEnd(): void;

Counting Methods

/**
 * Increment and log a counter for the given label to stdout
 * @param label - Counter label, defaults to 'default'
 */
count(label?: string): void;

/**
 * Reset a counter for the given label to zero
 * @param label - Counter label to reset, defaults to 'default'
 */
countReset(label?: string): void;

Timing Methods

/**
 * Start a timer with the given label
 * @param label - Timer label, defaults to 'default'
 */
time(label?: string): void;

/**
 * End a timer and log the elapsed time to stdout
 * @param label - Timer label to end, defaults to 'default'
 */
timeEnd(label?: string): void;

/**
 * Log the current elapsed time for a running timer to stdout
 * @param label - Timer label to check, defaults to 'default'
 * @param data - Additional data to log with the time
 */
timeLog(label?: string, ...data: Array<unknown>): void;

docs

buffered-console.md

custom-console.md

index.md

null-console.md

output-formatting.md

tile.json