1- import { describe , expect , it , vi } from "vitest" ;
1+ import { afterEach , beforeEach , describe , expect , it , vi } from "vitest" ;
22import {
33 createAbortableFetchMock ,
44 createJsonResponse ,
@@ -21,6 +21,14 @@ async function expectAbortError(promise: Promise<unknown>) {
2121}
2222
2323describe ( "createDiscordRequestClient" , ( ) => {
24+ beforeEach ( ( ) => {
25+ vi . useRealTimers ( ) ;
26+ } ) ;
27+
28+ afterEach ( ( ) => {
29+ vi . useRealTimers ( ) ;
30+ } ) ;
31+
2432 it ( "preserves the REST client's abort signal for proxied fetch calls" , async ( ) => {
2533 const fetchSpy = vi . fn ( async ( _input : string | URL | Request , init ?: RequestInit ) => {
2634 if ( ! ( init ?. signal instanceof AbortSignal ) ) {
@@ -41,14 +49,20 @@ describe("createDiscordRequestClient", () => {
4149
4250 it ( "lets the REST client abort hanging proxied requests after its timeout" , async ( ) => {
4351 const { fetch : fetchSpy } = createAbortableFetchMock ( ) ;
52+ vi . useFakeTimers ( ) ;
4453
4554 const client = createDiscordRequestClient ( "Bot test-token" , {
4655 fetch : fetchSpy as never ,
4756 queueRequests : false ,
4857 timeout : 20 ,
4958 } ) ;
5059
51- await expectAbortError ( client . get ( "/channels/123/messages" ) ) ;
60+ const request = client . get ( "/channels/123/messages" ) ;
61+ const abortExpectation = expectAbortError ( request ) ;
62+ await Promise . resolve ( ) ;
63+ expect ( fetchSpy ) . toHaveBeenCalledTimes ( 1 ) ;
64+ await vi . advanceTimersByTimeAsync ( 20 ) ;
65+ await abortExpectation ;
5266 } , 1_000 ) ;
5367
5468 it ( "lets abortAllRequests cancel active proxied fetches" , async ( ) => {
0 commit comments