Skip to content

Commit 5ee369c

Browse files
committed
chore: experiment with blocking process.stdout and process.stderr
1 parent d80acbd commit 5ee369c

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

tests/bin/typescript-demo-lib.test.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,22 @@ import os from 'os';
33
import path from 'path';
44
import { mockProcessStdout } from 'jest-mock-process';
55
import main from '@/bin/typescript-demo-lib';
6-
import { sleep } from '@/utils';
6+
7+
if (process.stdout.isTTY) {
8+
// @ts-ignore
9+
process.stdout._handle.setBlocking(true);
10+
} else if (os.platform() === 'win32' && !process.stdout.isTTY) {
11+
// @ts-ignore
12+
process.stdout._handle.setBlocking(false);
13+
}
14+
15+
if (process.stderr.isTTY) {
16+
// @ts-ignore
17+
process.stderr._handle.setBlocking(true);
18+
} else if (os.platform() === 'win32' && !process.stderr.isTTY) {
19+
// @ts-ignore
20+
process.stderr._handle.setBlocking(false);
21+
}
722

823
const uuidRegex = new RegExp(
924
'[a-z0-9]{8}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{12}',
@@ -26,7 +41,6 @@ describe('main', () => {
2641
const mockLog = mockProcessStdout();
2742
expect(await main(['', '', '1', '2', dataDir])).toEqual(0);
2843
mockLog.mockRestore();
29-
await sleep(2000);
3044
});
3145
test('no input', async () => {
3246
const mockLog = mockProcessStdout();
@@ -47,7 +61,6 @@ describe('main', () => {
4761
expect(tmpMockLog).toMatch(uuidRegex);
4862
expect(tmpMockLog).toContain('0 + 0 = 0\n');
4963
mockLog.mockRestore();
50-
await sleep(2000);
5164
});
5265
test('adds 0 + 1', async () => {
5366
const mockLog = mockProcessStdout();
@@ -58,7 +71,6 @@ describe('main', () => {
5871
expect(tmpMockLog).toMatch(uuidRegex);
5972
expect(tmpMockLog).toContain('0 + 1 = 1\n');
6073
mockLog.mockRestore();
61-
await sleep(2000);
6274
});
6375
test('adds 1 + 0', async () => {
6476
const mockLog = mockProcessStdout();
@@ -69,7 +81,6 @@ describe('main', () => {
6981
expect(tmpMockLog).toMatch(uuidRegex);
7082
expect(tmpMockLog).toContain('1 + 0 = 1\n');
7183
mockLog.mockRestore();
72-
await sleep(2000);
7384
});
7485
test('adds 7657 + 238947', async () => {
7586
const mockLog = mockProcessStdout();
@@ -80,6 +91,5 @@ describe('main', () => {
8091
expect(tmpMockLog).toMatch(uuidRegex);
8192
expect(tmpMockLog).toContain('7657 + 238947 = 246604\n');
8293
mockLog.mockRestore();
83-
await sleep(2000);
8494
});
8595
});

0 commit comments

Comments
 (0)