-
Notifications
You must be signed in to change notification settings - Fork 2
/
TestChild.ts
79 lines (63 loc) · 2.12 KB
/
TestChild.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import { expect } from 'chai';
import sinon from 'sinon';
import { ChildProcessError } from '../../src/error/ChildProcessError.js';
import { ChildStreams, childResult } from '../../src/Child.js';
import { Maybe, mustExist } from '../../src/Maybe.js';
const { match, stub } = sinon;
type Closer = (status: number) => Promise<void>;
function createChild(): ChildStreams & { closer: Maybe<Closer> } {
return {
closer /* Maybe<Closer> */: undefined,
on(event: string, fn: Closer) {
if (event === 'close') {
this.closer = fn;
}
},
stderr: {
on() {
/* noop */
},
},
stdout: {
on() {
/* noop */
},
},
/* eslint-disable @typescript-eslint/no-explicit-any */
} as any;
}
describe('child process utils', async () => {
describe('wait for child helper', async () => {
it('should read exit status', async () => {
const child = createChild();
const resultPromise = childResult(child);
await mustExist(child.closer)(0);
const result = await resultPromise;
expect(result.status).to.equal(0);
});
it('should reject on failure status', async () => {
const child = createChild();
const resultPromise = childResult(child);
await mustExist(child.closer)(1);
return expect(resultPromise).to.eventually.be.rejectedWith(ChildProcessError);
});
it('should read output', async () => {
const child = createChild();
stub(child.stdout, 'on').withArgs('data', match.func).yields(Buffer.from('hello'));
const resultPromise = childResult(child);
await mustExist(child.closer)(0);
return expect(resultPromise).to.eventually.deep.equal({
stderr: '',
stdout: 'hello',
status: 0,
});
});
it('should reject on error output', async () => {
const child = createChild();
stub(child.stderr, 'on').withArgs('data', match.func).yields(Buffer.from('nope'));
const resultPromise = childResult(child);
await mustExist(child.closer)(0);
return expect(resultPromise).to.eventually.be.rejectedWith(ChildProcessError);
});
});
});