File tree Expand file tree Collapse file tree 4 files changed +64
-7
lines changed Expand file tree Collapse file tree 4 files changed +64
-7
lines changed Original file line number Diff line number Diff line change @@ -148,7 +148,7 @@ add.uncaughtExceptionHandler = function (hook) {
148
148
149
149
if ( errHooks . length === 1 ) {
150
150
process . once ( 'uncaughtException' , exit . bind ( null , true , 1 ) ) ;
151
- process . once ( 'uncaughtRejection ' , exit . bind ( null , true , 1 ) ) ;
151
+ process . once ( 'unhandledRejection ' , exit . bind ( null , true , 1 ) ) ;
152
152
}
153
153
} ;
154
154
Original file line number Diff line number Diff line change @@ -28,30 +28,41 @@ exports.addCheck = function (num) {
28
28
29
29
// Only call exit once, and save uncaught errors
30
30
var called = false ;
31
- var ucErr ;
31
+ var ucErrStr ;
32
32
33
33
// Save errors that do not start with 'test'
34
34
process . on ( 'uncaughtException' , function ( err ) {
35
35
if ( err . message . indexOf ( 'test' ) !== 0 ) {
36
- ucErr = err ;
36
+ ucErrStr = err . stack ;
37
+ }
38
+ } ) ;
39
+ // Save rejections that do not start with 'test'
40
+ process . on ( 'unhandledRejection' , function ( reason ) {
41
+ if ( ( reason . message || reason ) . indexOf ( 'test' ) !== 0 ) {
42
+ ucErrStr = reason . message || reason ;
37
43
}
38
44
} ) ;
39
45
40
46
// Check that there were no unexpected errors and all callbacks were called
41
- process . once ( 'exit' , function ( ) {
47
+ function onExitCheck ( timeout ) {
42
48
if ( called ) {
43
49
return ;
44
50
}
45
51
called = true ;
46
52
47
- if ( ucErr ) {
48
- exports . reject ( ucErr . stack ) ;
53
+ if ( timeout ) {
54
+ exports . reject ( 'Test timed out' ) ;
55
+ } else if ( ucErrStr ) {
56
+ exports . reject ( ucErrStr ) ;
49
57
} else if ( c === num ) {
50
58
exports . done ( ) ;
51
59
} else {
52
60
exports . reject ( 'Expected ' + num + ' callback calls, but ' + c + ' received' ) ;
53
61
}
54
- } ) ;
62
+ }
63
+
64
+ process . once ( 'exit' , onExitCheck . bind ( null , null ) ) ;
65
+ setTimeout ( onExitCheck . bind ( null , true ) , 10000 ) ;
55
66
} ;
56
67
57
68
// If the check isn't added, throw on exit
Original file line number Diff line number Diff line change
1
+ 'use strict' ;
2
+ var exitHook = require ( './../../index' ) ;
3
+ var stub = require ( './stub' ) ;
4
+
5
+ exitHook ( function ( cb ) {
6
+ setTimeout ( function ( ) {
7
+ stub . called ( ) ;
8
+ cb ( ) ;
9
+ } , 50 ) ;
10
+ stub . called ( ) ;
11
+ } ) ;
12
+
13
+ exitHook ( function ( ) {
14
+ stub . called ( ) ;
15
+ } ) ;
16
+
17
+ exitHook . uncaughtExceptionHandler ( function ( err , cb ) {
18
+ setTimeout ( function ( ) {
19
+ stub . called ( ) ;
20
+ cb ( ) ;
21
+ } , 50 ) ;
22
+ if ( ! err || err . message !== 'test-promise' ) {
23
+ stub . reject ( `No error passed to uncaughtExceptionHandler, or message not test-promise - ${ err . message } ` ) ;
24
+ }
25
+ stub . called ( ) ;
26
+ } ) ;
27
+
28
+ process . on ( 'unhandledRejection' , function ( ) {
29
+ // All uncaught rejection handlers should be called even though the exit hook handler was registered
30
+ stub . called ( ) ;
31
+ } ) ;
32
+
33
+ stub . addCheck ( 6 ) ;
34
+
35
+ ( ( ) => {
36
+ return Promise . reject ( new Error ( 'test-promise' ) ) ;
37
+ } ) ( ) ;
Original file line number Diff line number Diff line change @@ -103,3 +103,12 @@ test('async exit timeout', t => {
103
103
t . is ( code , 0 ) ;
104
104
} ) ;
105
105
} ) ;
106
+
107
+ test ( 'unhandled promise rejection' , t => {
108
+ t . plan ( 2 ) ;
109
+ return testInSub ( 'unhandled-promise' )
110
+ . then ( ( [ code , output ] ) => {
111
+ t . is ( output , 'SUCCESS' ) ;
112
+ t . is ( code , 0 ) ;
113
+ } ) ;
114
+ } ) ;
You can’t perform that action at this time.
0 commit comments