forked from gajus/turbowatch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreateSpawn.test.ts
66 lines (47 loc) · 1.49 KB
/
createSpawn.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
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
import { createSpawn } from './createSpawn';
import { expect, it } from 'vitest';
it('returns outputs', async () => {
const spawn = createSpawn('foo');
const result = await spawn`echo 'Hello, World!'`;
expect(String(result?.stdout)).toEqual('Hello, World!\n');
});
it('injects path to node_modules/.bin', async () => {
const spawn = createSpawn('foo');
const result = await spawn`echo $PATH`;
expect(String(result?.stdout)).toMatch(/node_modules\/\.bin/u);
});
it('rejects if process produces an error', async () => {
const spawn = createSpawn('foo');
await expect(spawn`does-not-exist`).rejects.toThrowError(
'Program exited with code 127.',
);
});
const TIMEOUT = 100;
it(
'terminates spawned process when it receives abort signal',
async () => {
const abortController = new AbortController();
const spawn = createSpawn('foo', { abortSignal: abortController.signal });
setTimeout(() => {
void abortController.abort();
}, 50);
await expect(spawn`sleep 10`).rejects.toThrowError();
},
TIMEOUT,
);
it(
'waits for termination',
async () => {
const abortController = new AbortController();
const spawn = createSpawn('foo', { abortSignal: abortController.signal });
setTimeout(() => {
void abortController.abort();
}, 50);
const start = Date.now();
await expect(
spawn`( trap '' TERM; exec sleep 0.1 )`,
).rejects.toThrowError();
expect(Date.now() - start).toBeGreaterThan(100);
},
TIMEOUT * 2,
);