@fgv/ts-utils-jest
Version:
Custom matchers for ts-utils result class
54 lines • 1.77 kB
TypeScript
/**
* Utility functions for handling ANSI color codes in test output.
*
* @packageDocumentation
*/
/**
* Strips ANSI color/style escape sequences from a string.
*
* This function removes all ANSI escape sequences commonly used for
* terminal colors and text styling, making the output suitable for
* color-agnostic snapshot testing.
*
* @param text - The text that may contain ANSI escape sequences
* @returns The text with all ANSI escape sequences removed
*
* @example
* ```typescript
* const coloredText = '\u001b[31mError:\u001b[39m Something failed';
* const plainText = stripAnsiColors(coloredText);
* console.log(plainText); // "Error: Something failed"
* ```
*
* @example
* ```typescript
* // Common usage in Jest matchers
* const formattedOutput = matcherUtils.printReceived(value);
* const cleanOutput = stripAnsiColors(formattedOutput);
* expect(cleanOutput).toMatchSnapshot();
* ```
*
* @public
*/
export declare function stripAnsiColors(text: string): string;
/**
* Creates a wrapper function that strips ANSI colors from any string-returning function.
*
* This is useful for wrapping Jest matcher utility functions to make them
* color-agnostic for snapshot testing.
*
* @param fn - A function that returns a string (potentially with ANSI colors)
* @returns A wrapped function that returns the same string with colors stripped
*
* @example
* ```typescript
* import { printReceived } from 'jest-matcher-utils';
*
* const printReceivedClean = createColorStripWrapper(printReceived);
* const output = printReceivedClean(someValue); // No colors
* ```
*
* @public
*/
export declare function createColorStripWrapper<T extends (...args: unknown[]) => string>(fn: T): T;
//# sourceMappingURL=colorHelpers.d.ts.map