@@ -139,6 +139,53 @@ describe("createSSEStream", () => {
139139 expect ( consoleError ) . toHaveBeenCalledWith ( "open error" , "Internal Error" ) ;
140140 } ) ;
141141
142+ test ( "open error with json error" , async ( ) => {
143+ mockFetchEventSource . mockImplementation ( async ( _url , options ) => {
144+ const { onopen, onerror } = options ;
145+ await Promise . resolve ( ) ;
146+ try {
147+ await onopen ?.( {
148+ ok : false ,
149+ statusText : "Internal Error" ,
150+ text : ( ) =>
151+ Promise . resolve (
152+ JSON . stringify ( {
153+ error : "Something went wrong" ,
154+ } )
155+ ) ,
156+ } as Response ) ;
157+ } catch ( e ) {
158+ await onerror ?.( e ) ;
159+ }
160+ } ) ;
161+ await expect ( createSSEStream ( url ) ) . rejects . toMatchInlineSnapshot (
162+ `[Error: Something went wrong]`
163+ ) ;
164+ expect ( consoleError ) . toHaveBeenCalledTimes ( 1 ) ;
165+ expect ( consoleError ) . toHaveBeenCalledWith ( "open error" , "Internal Error" ) ;
166+ } ) ;
167+
168+ test ( "open error with non-json error" , async ( ) => {
169+ mockFetchEventSource . mockImplementation ( async ( _url , options ) => {
170+ const { onopen, onerror } = options ;
171+ await Promise . resolve ( ) ;
172+ try {
173+ await onopen ?.( {
174+ ok : false ,
175+ statusText : "Internal Error" ,
176+ text : ( ) => Promise . resolve ( "Oops" ) ,
177+ } as Response ) ;
178+ } catch ( e ) {
179+ await onerror ?.( e ) ;
180+ }
181+ } ) ;
182+ await expect ( createSSEStream ( url ) ) . rejects . toMatchInlineSnapshot (
183+ `[Error: Oops]`
184+ ) ;
185+ expect ( consoleError ) . toHaveBeenCalledTimes ( 1 ) ;
186+ expect ( consoleError ) . toHaveBeenCalledWith ( "open error" , "Internal Error" ) ;
187+ } ) ;
188+
142189 test ( "message error" , async ( ) => {
143190 mockFetchEventSource . mockImplementation ( async ( _url , options ) => {
144191 const { onopen, onmessage } = options ;
0 commit comments