fix: tty.WriteStream method stubs for process.stdout/stderr (#29865)

Fixes #29839
This commit is contained in:
Yury Semikhatsky 2024-03-11 10:11:16 -07:00 committed by GitHub
parent 94e61fc95a
commit 59228f19ce
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 49 additions and 0 deletions

View File

@ -131,4 +131,26 @@ function setTtyParams(stream: WriteStream, params: TtyParams) {
count = 16;
return count <= 2 ** params.colorDepth;
})as any;
// Stubs for the rest of the methods to avoid exceptions in user code.
stream.clearLine = (dir: any, callback?: () => void) => {
callback?.();
return true;
};
stream.clearScreenDown = (callback?: () => void) => {
callback?.();
return true;
};
(stream as any).cursorTo = (x: number, y?: number | (() => void), callback?: () => void) => {
if (callback)
callback();
else if (y instanceof Function)
y();
return true;
};
stream.moveCursor = (dx: number, dy: number, callback?: () => void) => {
callback?.();
return true;
};
stream.getWindowSize = () => [stream.columns, stream.rows];
}

View File

@ -128,3 +128,30 @@ test('should not throw type error when using assert', async ({ runInlineTest })
expect(result.output).not.toContain(`TypeError: process.stderr.hasColors is not a function`);
expect(result.output).toContain(`AssertionError`);
});
test('should provide stubs for tty.WriteStream methods on process.stdout/stderr', async ({ runInlineTest }) => {
test.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/29839' });
const result = await runInlineTest({
'a.spec.ts': `
import { test, expect } from '@playwright/test';
import type * as tty from 'tty';
async function checkMethods(stream: tty.WriteStream) {
expect(stream.isTTY).toBe(true);
await new Promise<void>(r => stream.clearLine(-1, r));;
await new Promise<void>(r => stream.clearScreenDown(r));;
await new Promise<void>(r => stream.cursorTo(0, 0, r));;
await new Promise<void>(r => stream.cursorTo(0, r));;
await new Promise<void>(r => stream.moveCursor(1, 1, r));;
expect(stream.getWindowSize()).toEqual([stream.columns, stream.rows]);
// getColorDepth() and hasColors() are covered in other tests.
}
test('process.stdout implementd tty.WriteStream methods', () => {
checkMethods(process.stdout);
});
test('process.stderr implementd tty.WriteStream methods', () => {
checkMethods(process.stderr);
});
`
});
expect(result.exitCode).toBe(0);
});