Skip to content

Commit 8674855

Browse files
SakshiKoli-CSclaude
andcommitted
test: pin stdout so the suite reads the same in a terminal as when piped
The setup pinned stdin to a non-terminal but left stdout reporting whatever the environment said, so the CLI picked its human-facing rendering whenever the suite ran in a real terminal: short clock stamps instead of ISO ones, the ora spinner with its own SIGINT handler instead of the silent loader, and tables squeezed to the window width. The expectations were written against the piped rendering, so four tests passed under a pipe and failed in a terminal, on the same commit. Pin stdout's isTTY and columns alongside stdin, and restore all three after each test. Generalise streamReportingTTY to pin any stream property so the new stdoutOfWidth helper can reuse it; stdinReportingTTY and stdoutReportingTTY keep their signatures and behaviour. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
1 parent bedaae0 commit 8674855

2 files changed

Lines changed: 24 additions & 11 deletions

File tree

‎test/no-terminal.setup.ts‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import { cliux } from '@contentstack/cli-utilities';
22

3-
import { stdinReportingTTY } from './support/terminal';
3+
import { stdinReportingTTY, stdoutOfWidth, stdoutReportingTTY } from './support/terminal';
44

5-
let restoreTerminal: () => void = () => undefined;
5+
let restoreTerminal: (() => void)[] = [];
66

77
beforeEach(() => {
8-
restoreTerminal = stdinReportingTTY(false);
8+
// Pin both streams to what a piped run reports, so a suite run inside a real
9+
// terminal renders exactly what CI renders — no TTY colouring, no wrapping
10+
// and no table truncation that depends on how wide the window happens to be.
11+
restoreTerminal = [stdinReportingTTY(false), stdoutReportingTTY(false), stdoutOfWidth(undefined)];
912

1013
jest.spyOn(cliux, 'inquire').mockImplementation(async (payload: unknown) => {
1114
const message = String((payload as Record<string, unknown> | undefined)?.message ?? '');
@@ -17,5 +20,9 @@ beforeEach(() => {
1720
});
1821

1922
afterEach(() => {
20-
restoreTerminal();
23+
for (const restore of restoreTerminal.reverse()) {
24+
restore();
25+
}
26+
27+
restoreTerminal = [];
2128
});

‎test/support/terminal.ts‎

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,32 @@
11
import { cliux } from '@contentstack/cli-utilities';
22

3-
function streamReportingTTY(stream: NodeJS.ReadStream | NodeJS.WriteStream, isTTY: boolean | undefined): () => void {
4-
const descriptor = Object.getOwnPropertyDescriptor(stream, 'isTTY');
3+
type Stream = NodeJS.ReadStream | NodeJS.WriteStream;
54

6-
Object.defineProperty(stream, 'isTTY', { value: isTTY, configurable: true, writable: true });
5+
function pinned(stream: Stream, property: 'isTTY' | 'columns', value: unknown): () => void {
6+
const descriptor = Object.getOwnPropertyDescriptor(stream, property);
7+
8+
Object.defineProperty(stream, property, { value, configurable: true, writable: true });
79

810
return () => {
911
if (descriptor === undefined) {
10-
delete (stream as unknown as { isTTY?: boolean }).isTTY;
12+
delete (stream as unknown as Record<string, unknown>)[property];
1113
return;
1214
}
1315

14-
Object.defineProperty(stream, 'isTTY', descriptor);
16+
Object.defineProperty(stream, property, descriptor);
1517
};
1618
}
1719

1820
export function stdinReportingTTY(isTTY: boolean | undefined): () => void {
19-
return streamReportingTTY(process.stdin, isTTY);
21+
return pinned(process.stdin, 'isTTY', isTTY);
2022
}
2123

2224
export function stdoutReportingTTY(isTTY: boolean | undefined): () => void {
23-
return streamReportingTTY(process.stdout, isTTY);
25+
return pinned(process.stdout, 'isTTY', isTTY);
26+
}
27+
28+
export function stdoutOfWidth(columns: number | undefined): () => void {
29+
return pinned(process.stdout, 'columns', columns);
2430
}
2531

2632
export function pretendTerminal(): () => void {

0 commit comments

Comments
 (0)