@@ -21,6 +21,22 @@ import { startLogging } from './helpers/start.js'
2121const pNextTick = promisify ( nextTick )
2222removeProcessListeners ( )
2323
24+ const startProcessLogging = function ( eventName , opts ) {
25+ const processHandler = setProcessEvent ( eventName )
26+ const stopLogging = startExitLogging ( opts )
27+ return stopProcessLogging . bind (
28+ undefined ,
29+ eventName ,
30+ stopLogging ,
31+ processHandler ,
32+ )
33+ }
34+
35+ const stopProcessLogging = function ( eventName , stopLogging , processHandler ) {
36+ stopLogging ( )
37+ unsetProcessEvent ( eventName , processHandler )
38+ }
39+
2440const startExitLogging = function ( opts ) {
2541 stubProcessExit ( )
2642 return stopExitLogging . bind ( undefined , startLogging ( opts ) . stopLogging )
@@ -117,55 +133,53 @@ test.serial('exit process by default', async (t) => {
117133test . serial (
118134 'does not exit process by default if there are other listeners' ,
119135 async ( t ) => {
120- const processHandler = setProcessEvent ( 'uncaughtException' )
121- const stopLogging = startExitLogging ( { exit : undefined } )
136+ const stopLogging = startProcessLogging ( 'uncaughtException' , {
137+ exit : undefined ,
138+ } )
122139
123140 await emit ( 'uncaughtException' )
124141 t . is ( process . exitCode , undefined )
125142
126143 stopLogging ( )
127- unsetProcessEvent ( 'uncaughtException' , processHandler )
128144 } ,
129145)
130146
131147test . serial (
132148 'exits process if there are other listeners but "exit: true"' ,
133149 async ( t ) => {
134- const processHandler = setProcessEvent ( 'uncaughtException' )
135- const stopLogging = startExitLogging ( { exit : true } )
150+ const stopLogging = startProcessLogging ( 'uncaughtException' , { exit : true } )
136151
137152 await emit ( 'uncaughtException' )
138153 t . is ( process . exitCode , EXIT_CODE )
139154
140155 stopLogging ( )
141- unsetProcessEvent ( 'uncaughtException' , processHandler )
142156 } ,
143157)
144158
145159test . serial (
146160 'exits process by default if there are other listeners for other events' ,
147161 async ( t ) => {
148- const processHandler = setProcessEvent ( 'unhandledRejection' )
149- const stopLogging = startExitLogging ( { exit : undefined } )
162+ const stopLogging = startProcessLogging ( 'unhandledRejection' , {
163+ exit : undefined ,
164+ } )
150165
151166 await emit ( 'uncaughtException' )
152167 t . is ( process . exitCode , EXIT_CODE )
153168
154169 stopLogging ( )
155- unsetProcessEvent ( 'unhandledRejection' , processHandler )
156170 } ,
157171)
158172
159173test . serial (
160174 'does not exit process by default if there are other listeners for other events but "exit: false"' ,
161175 async ( t ) => {
162- const processHandler = setProcessEvent ( 'unhandledRejection' )
163- const stopLogging = startExitLogging ( { exit : false } )
176+ const stopLogging = startProcessLogging ( 'unhandledRejection' , {
177+ exit : false ,
178+ } )
164179
165180 await emit ( 'uncaughtException' )
166181 t . is ( process . exitCode , undefined )
167182
168183 stopLogging ( )
169- unsetProcessEvent ( 'unhandledRejection' , processHandler )
170184 } ,
171185)
0 commit comments