forked from sindresorhus/exit-hook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
90 lines (76 loc) · 2.04 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
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 () => {},
{
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, {});
});
});