inspector/clients/tui/__tests__/helpers/renderTui.tsx
cliffhall a3bb679f1e fix: strip ANSI from TUI test frames so FORCE_COLOR cannot break assertions (#2207)
Ink writes styling as escape sequences inside the styled run, so an
accelerator underline splits the word it decorates: `<Text underline>I</Text>nfo`
reaches the frame buffer with escapes between `I` and `nfo`, and a plain
`toContain("Info")` fails against a component that is rendering correctly.
Chalk only emits color when it detects a TTY, so CI stays green and the six
failures land only on a developer whose shell exports FORCE_COLOR.

Add `__tests__/helpers/renderTui.tsx` — ink-testing-library's `render` with
`lastFrame()` and `frames` ANSI-stripped — and point all 16 renderer test
files at it. `frames` is a getter reading through to the live array, since
Ink appends to it as the component re-renders; the raw bytes stay reachable
as `stdout.lastFrame()`.

Pinning FORCE_COLOR=0 would turn the six green while hiding the class: any
future assertion on styled text has the same problem, and the suite would
stop covering the styled path at all.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01MFp2LVH7UJST5i8bDBJghw
Signed-off-by: cliffhall <cliff@futurescale.com>
2026-09-06 20:00:15 -04:00

38 lines
1.8 KiB
TypeScript

// A drop-in replacement for ink-testing-library's `render` that strips ANSI
// styling from every frame the tests read back.
//
// Ink writes styling as escape sequences *inside* the styled run, so an
// accelerator underline splits the word it decorates: `<Text underline>I</Text>nfo`
// reaches the frame buffer as `ESC[4mI ESC[24m nfo` (without the spaces). A plain
// `expect(frame).toContain("Info")` then fails against a component that is
// rendering perfectly — and only for a developer whose shell exports
// FORCE_COLOR, since CI has no TTY and chalk emits nothing there (#2207).
//
// Making every assertion read a stripped frame fixes the whole class rather
// than the six assertions that happened to trip it, and keeps the suite
// covering the styled path instead of pinning FORCE_COLOR=0 to hide it. No test
// asserts on escape sequences; one that needs the raw bytes can reach
// `stdout.lastFrame()` on the returned instance, which is left untouched.
import { render as inkRender } from "ink-testing-library";
import stripAnsi from "strip-ansi";
import type { ReactElement } from "react";
// ink-testing-library does not export its `Instance` type.
type Instance = ReturnType<typeof inkRender>;
/** Strip ANSI styling from a frame, passing `undefined` through unchanged. */
export const stripFrameStyling = (frame: string | undefined) =>
frame === undefined ? undefined : stripAnsi(frame);
export const render = (tree: ReactElement): Instance => {
const instance = inkRender(tree);
return {
...instance,
lastFrame: () => stripFrameStyling(instance.lastFrame()),
// Read through to the live array on each access — ink appends to it as the
// component re-renders, so a copy taken here would go stale.
get frames() {
return instance.frames.map(stripAnsi);
},
};
};