-
-
Notifications
You must be signed in to change notification settings - Fork 39
/
test.js
118 lines (99 loc) · 2.57 KB
/
test.js
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import process from 'node:process';
import test from 'ava';
import {execa} from 'execa';
import exitHook, {asyncExitHook} from './index.js';
test('main', async t => {
const {stdout, stderr, exitCode} = await execa(process.execPath, ['./fixtures/sync.js']);
t.is(stdout, 'foo\nbar');
t.is(stderr, '');
t.is(exitCode, 0);
});
test('main-empty', async t => {
const {stderr, exitCode} = await execa(process.execPath, ['./fixtures/empty.js']);
t.is(stderr, '');
t.is(exitCode, 0);
});
test('main-async', async t => {
const {stdout, stderr, exitCode} = await execa(process.execPath, ['./fixtures/async.js']);
t.is(stdout, 'foo\nbar\nquux');
t.is(stderr, '');
t.is(exitCode, 0);
});
test('main-async-notice', async t => {
const {stdout, stderr, exitCode} = await execa(process.execPath, ['./fixtures/async.js'], {
env: {
EXIT_HOOK_SYNC: '1',
},
});
t.is(stdout, 'foo\nbar');
t.regex(stderr, /SYNCHRONOUS TERMINATION NOTICE/);
t.is(exitCode, 0);
});
test('listener count', t => {
t.is(process.listenerCount('exit'), 0);
const unsubscribe1 = exitHook(() => {});
const unsubscribe2 = exitHook(() => {});
t.is(process.listenerCount('exit'), 1);
// Remove all listeners
unsubscribe1();
unsubscribe2();
t.is(process.listenerCount('exit'), 1);
// Re-add listener
const unsubscribe3 = exitHook(() => {});
t.is(process.listenerCount('exit'), 1);
// Remove again
unsubscribe3();
t.is(process.listenerCount('exit'), 1);
// Add async style listener
const unsubscribe4 = asyncExitHook(
async () => {},
{
wait: 100,
},
);
t.is(process.listenerCount('exit'), 1);
// Remove again
unsubscribe4();
t.is(process.listenerCount('exit'), 1);
});
test('type enforcing', t => {
// Non-function passed to `exitHook`.
t.throws(() => {
exitHook(null);
}, {instanceOf: TypeError});
// Non-function passed to `asyncExitHook`.
t.throws(() => {
asyncExitHook(null, {
wait: 100,
});
}, {
instanceOf: TypeError,
});
// Non-numeric passed to `wait` option.
t.throws(() => {
asyncExitHook(async () => true, {wait: 'abc'});
});
// Empty value passed to `wait` option.
t.throws(() => {
asyncExitHook(async () => true, {});
});
});
const signalTests = [
['SIGINT', 130],
['SIGTERM', 143],
];
for (const [signal, exitCode] of signalTests) {
test(signal, async t => {
const subprocess = execa(process.execPath, ['./fixtures/signal.js']);
setTimeout(() => {
subprocess.kill(signal);
}, 1000);
try {
await subprocess;
} catch (error) {
t.is(error.exitCode, exitCode);
t.is(error.stderr, '');
t.is(error.stdout, `${exitCode}\n${exitCode}`);
}
});
}