-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_stdin.test.ts
30 lines (27 loc) · 1.02 KB
/
get_stdin.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import stdin from './stdin';
jest.mock('./stdin', () => ({
__esModule: true,
default: jest.requireActual('mock-stdin').stdin(),
}));
const expected = 'TEST';
describe('get_stdin', () => {
it('returns a Promise<string> which resolves to the stdin of the current process', async () => {
stdin.on = jest.fn((_, handler) => handler(expected)) as any;
const {get_stdin} = await import('./get_stdin');
expect(typeof get_stdin).toBe('function');
const actual = await get_stdin();
expect(stdin.on).toHaveBeenCalled();
expect(actual).toBe(expected);
(stdin.on as any).mockReset();
});
it('rejects if stdin has an error', async () => {
stdin.on = jest.fn((type, handler) => type === 'error' && handler(new Error)) as any;
const {get_stdin} = await import('./get_stdin');
try {
await get_stdin();
} catch (error) {
expect(stdin.on).toHaveBeenCalled();
expect(error).toBeInstanceOf(Error);
}
});
});