@@ -5,6 +5,7 @@ const path = require('path');
55const coffee = require ( 'coffee' ) ;
66const sleep = require ( 'mz-modules/sleep' ) ;
77const urllib = require ( 'urllib' ) ;
8+ const mm = require ( 'mm' ) ;
89
910const fixtures = path . join ( __dirname , 'fixtures' ) ;
1011const waitStart = process . env . COV ? 5000 : 2000 ;
@@ -108,4 +109,87 @@ describe('test/index.test.js', () => {
108109 child . expect ( 'stderr' , / 2 2 2 e x i t w i t h c o d e : 1 1 0 / ) ;
109110 } ) ;
110111 } ) ;
112+
113+ describe ( 'beforeExit' , ( ) => {
114+ afterEach ( mm . restore ) ;
115+
116+ it ( 'should support normal function' , function * ( ) {
117+ mm ( process . env , 'MODE' , '' ) ;
118+ const startFile = path . join ( fixtures , 'before-exit.js' ) ;
119+ const child = coffee . fork ( startFile )
120+ . debug ( ) ;
121+ yield sleep ( waitStart ) ;
122+ const result = yield urllib . request ( 'http://127.0.0.1:8000/' ) ;
123+ assert ( result . status === 200 ) ;
124+
125+ child . proc . kill ( ) ;
126+ yield sleep ( 5000 ) ;
127+ child . expect ( 'stdout' , / p r o c e s s e x i t / ) ;
128+ child . expect ( 'stdout' , / e x i t w i t h c o d e : 0 / ) ;
129+ } ) ;
130+
131+ it ( 'should support function return promise' , function * ( ) {
132+ mm ( process . env , 'MODE' , 'promise' ) ;
133+ const startFile = path . join ( fixtures , 'before-exit.js' ) ;
134+ const child = coffee . fork ( startFile )
135+ . debug ( ) ;
136+ yield sleep ( waitStart ) ;
137+ const result = yield urllib . request ( 'http://127.0.0.1:8000/' ) ;
138+ assert ( result . status === 200 ) ;
139+
140+ child . proc . kill ( ) ;
141+ yield sleep ( 5000 ) ;
142+ child . expect ( 'stdout' , / b e f o r e E x i t s u c c e s s / ) ;
143+ child . expect ( 'stdout' , / p r o c e s s e x i t i n g \n p r o c e s s e x i t e d / ) ;
144+ child . expect ( 'stdout' , / e x i t w i t h c o d e : 0 / ) ;
145+ } ) ;
146+
147+ if ( parseInt ( process . versions . node ) < 8 ) return ;
148+
149+ it ( 'should support async function' , function * ( ) {
150+ mm ( process . env , 'MODE' , 'async' ) ;
151+ const startFile = path . join ( fixtures , 'before-exit.js' ) ;
152+ const child = coffee . fork ( startFile )
153+ . debug ( ) ;
154+ yield sleep ( waitStart ) ;
155+ const result = yield urllib . request ( 'http://127.0.0.1:8000/' ) ;
156+ assert ( result . status === 200 ) ;
157+
158+ child . proc . kill ( ) ;
159+ yield sleep ( 5000 ) ;
160+ child . expect ( 'stdout' , / b e f o r e E x i t s u c c e s s / ) ;
161+ child . expect ( 'stdout' , / p r o c e s s e x i t i n g \n p r o c e s s e x i t e d / ) ;
162+ child . expect ( 'stdout' , / e x i t w i t h c o d e : 0 / ) ;
163+ } ) ;
164+
165+ it ( 'should exit when async error' , function * ( ) {
166+ mm ( process . env , 'MODE' , 'async-error' ) ;
167+ const startFile = path . join ( fixtures , 'before-exit.js' ) ;
168+ const child = coffee . fork ( startFile )
169+ . debug ( ) ;
170+ yield sleep ( waitStart ) ;
171+ const result = yield urllib . request ( 'http://127.0.0.1:8000/' ) ;
172+ assert ( result . status === 200 ) ;
173+
174+ child . proc . kill ( ) ;
175+ yield sleep ( 5000 ) ;
176+ child . expect ( 'stderr' , / b e f o r e E x i t f a i l , e r r o r : r e j e c t / ) ;
177+ child . expect ( 'stdout' , / e x i t w i t h c o d e : 0 / ) ;
178+ } ) ;
179+
180+ it ( 'should exit when function error' , function * ( ) {
181+ mm ( process . env , 'MODE' , 'function-error' ) ;
182+ const startFile = path . join ( fixtures , 'before-exit.js' ) ;
183+ const child = coffee . fork ( startFile )
184+ . debug ( ) ;
185+ yield sleep ( waitStart ) ;
186+ const result = yield urllib . request ( 'http://127.0.0.1:8000/' ) ;
187+ assert ( result . status === 200 ) ;
188+
189+ child . proc . kill ( ) ;
190+ yield sleep ( 5000 ) ;
191+ child . expect ( 'stderr' , / b e f o r e E x i t f a i l , e r r o r : p r o c e s s e x i t / ) ;
192+ child . expect ( 'stdout' , / e x i t w i t h c o d e : 0 / ) ;
193+ } ) ;
194+ } ) ;
111195} ) ;
0 commit comments