forked from sindresorhus/exit-hook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
69 lines (57 loc) · 1.49 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
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} = await execa(process.execPath, ['fixture.js']);
t.is(stdout, 'foo\nbar');
});
test('main-async', async t => {
const {stdout} = await execa(process.execPath, ['fixture-async.js']);
t.is(stdout, 'foo\nbar\nquux');
});
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 () => {},
{
minimumWait: 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, {
minimumWait: 100,
});
}, {
instanceOf: TypeError,
});
// Non-numeric passed to `minimumWait` option.
t.throws(() => {
asyncExitHook(async () => true, {});
});
});