@@ -5,7 +5,7 @@ import process from 'node:process'
55
66import { afterEach , describe , expect , it , vi } from 'vitest'
77
8- import { createRestartHook } from '../../src/dev'
8+ import { createRejectionHandler , createRestartHook } from '../../src/dev'
99
1010const ERROR_EVENTS = [ 'uncaughtException' , 'unhandledRejection' ] as const
1111
@@ -48,7 +48,7 @@ describe('restart hook', () => {
4848 expect ( callback ) . toHaveBeenCalledExactlyOnceWith ( { type : 'shortcut' } )
4949 } )
5050
51- it ( 'should restart on an error that is not a broken pipe ' , ( ) => {
51+ it ( 'should restart on an error that is not a remote peer error ' , ( ) => {
5252 const source = new EventEmitter ( )
5353 const callback = vi . fn ( )
5454 arm ( source , callback )
@@ -57,6 +57,9 @@ describe('restart hook', () => {
5757 onError ! ( Object . assign ( new Error ( 'write EPIPE' ) , { code : 'EPIPE' } ) )
5858 expect ( callback ) . not . toHaveBeenCalled ( )
5959
60+ onError ! ( Object . assign ( new Error ( 'read ECONNRESET' ) , { code : 'ECONNRESET' } ) )
61+ expect ( callback ) . not . toHaveBeenCalled ( )
62+
6063 onError ! ( new Error ( 'boom' ) )
6164 expect ( callback ) . toHaveBeenCalledWith ( { type : 'error' , message : expect . stringContaining ( 'boom' ) } )
6265 } )
@@ -106,3 +109,36 @@ describe('restart hook', () => {
106109 expect ( second ) . toHaveBeenCalledTimes ( 1 )
107110 } )
108111} )
112+
113+ describe ( 'rejection handler' , ( ) => {
114+ it ( 'should report the rejection and stop' , ( ) => {
115+ const report = vi . fn ( )
116+ const stop = vi . fn ( )
117+
118+ createRejectionHandler ( report , stop ) ( new Error ( 'boom' ) )
119+
120+ expect ( report ) . toHaveBeenCalledExactlyOnceWith ( expect . stringContaining ( 'boom' ) )
121+ expect ( stop ) . toHaveBeenCalledTimes ( 1 )
122+ } )
123+
124+ it ( 'should describe a rejection that is not an error' , ( ) => {
125+ const report = vi . fn ( )
126+
127+ createRejectionHandler ( report , vi . fn ( ) ) ( 'nope' )
128+
129+ expect ( report ) . toHaveBeenCalledExactlyOnceWith ( 'Unhandled Rejection' )
130+ } )
131+
132+ it ( 'should keep the process alive on remote peer errors' , ( ) => {
133+ const report = vi . fn ( )
134+ const stop = vi . fn ( )
135+ const handle = createRejectionHandler ( report , stop )
136+
137+ handle ( Object . assign ( new Error ( 'write EPIPE' ) , { code : 'EPIPE' } ) )
138+ handle ( Object . assign ( new Error ( 'read ECONNRESET' ) , { code : 'ECONNRESET' } ) )
139+ handle ( Object . assign ( new Error ( 'premature close' ) , { code : 'ERR_STREAM_PREMATURE_CLOSE' } ) )
140+
141+ expect ( report ) . not . toHaveBeenCalled ( )
142+ expect ( stop ) . not . toHaveBeenCalled ( )
143+ } )
144+ } )
0 commit comments