@@ -46,6 +46,12 @@ const server = setupServer(
4646 status : "sold" ,
4747 } ) ;
4848 } ) ,
49+ // Slow endpoint to allow testing AbortController cancellation
50+ http . get ( "http://localhost/pet/111" , async ( ) => {
51+ // artificial delay so we have time to abort
52+ await new Promise ( ( resolve ) => setTimeout ( resolve , 200 ) ) ;
53+ return HttpResponse . json ( { id : 500 , name : "Slow" , photoUrls : [ ] , status : "pending" } , { status : 200 } ) ;
54+ } ) ,
4955 // GET with path (404 error)
5056 http . get ( "http://localhost/pet/9999" , ( ) => {
5157 return HttpResponse . json ( { code : 404 , message : "Pet not found" } , { status : 404 } ) ;
@@ -209,6 +215,29 @@ describe("Example API Client", () => {
209215 ] ) ;
210216 } ) ;
211217
218+ it ( "should allow aborting requests via AbortController signal" , async ( ) => {
219+ // custom client that forwards the provided signal to fetch
220+ const mini = createApiClient (
221+ {
222+ fetch ( input ) {
223+ return fetch ( input . url . toString ( ) , {
224+ method : input . method ,
225+ signal : input . overrides ?. signal ?? null ,
226+ } ) ;
227+ } ,
228+ } ,
229+ "http://localhost" ,
230+ ) ;
231+
232+ const controller = new AbortController ( ) ;
233+ const promise = mini . get ( "/pet/{petId}" , { path : { petId : 111 } , overrides : { signal : controller . signal } } ) ;
234+
235+ // abort immediately
236+ controller . abort ( ) ;
237+
238+ await expect ( promise ) . rejects . toHaveProperty ( "name" , "AbortError" ) ;
239+ } ) ;
240+
212241 it ( "should fetch /pet/findByStatus and receive mocked pets" , async ( ) => {
213242 const result = await api . get ( "/pet/findByStatus" , { query : { } } ) ;
214243 expect ( result ) . toEqual ( mockPets ) ;
@@ -455,6 +484,21 @@ describe("Example API Client", () => {
455484 expect ( result . data ) . toEqual ( { code : 404 , message : expect . any ( String ) } ) ;
456485 } ) ;
457486
487+ it ( "should allow aborting requests via AbortController signal (tanstack)" , async ( ) => {
488+ const mutation = tanstack . mutation ( "get" , "/pet/{petId}" ) ;
489+ const controller = new AbortController ( ) ;
490+
491+ const promise = mutation . mutationOptions . mutationFn ! ( {
492+ path : { petId : 111 } ,
493+ overrides : { signal : controller . signal } ,
494+ } ) ;
495+
496+ // abort immediately
497+ controller . abort ( ) ;
498+
499+ await expect ( promise ) . rejects . toHaveProperty ( "name" , "AbortError" ) ;
500+ } ) ;
501+
458502 it ( "should throw when throwOnStatusError is true (tanstack)" , async ( ) => {
459503 const mutation = tanstack . mutation ( "get" , "/pet/{petId}" , { withResponse : true } ) ;
460504 let err : unknown ;
0 commit comments