mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-14 10:33:30 +01:00
feat: add AbortSignal support (#8672)
* feat: add `AbortSignal` support * fix: move the expect earlier * fix: pass signal Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
/* eslint-disable id-length */
|
||||
/* eslint-disable promise/prefer-await-to-then */
|
||||
import { performance } from 'node:perf_hooks';
|
||||
import { setInterval, clearInterval } from 'node:timers';
|
||||
import { setInterval, clearInterval, setTimeout } from 'node:timers';
|
||||
import { MockAgent, setGlobalDispatcher } from 'undici';
|
||||
import type { Interceptable, MockInterceptor } from 'undici/types/mock-interceptor';
|
||||
import { beforeEach, afterEach, test, expect, vitest } from 'vitest';
|
||||
@@ -548,3 +548,30 @@ test('malformedRequest', async () => {
|
||||
|
||||
await expect(api.get('/malformedRequest')).rejects.toBeInstanceOf(DiscordAPIError);
|
||||
});
|
||||
|
||||
test('abort', async () => {
|
||||
mockPool
|
||||
.intercept({
|
||||
path: genPath('/abort'),
|
||||
method: 'GET',
|
||||
})
|
||||
.reply(200, { message: 'Hello World' }, responseOptions)
|
||||
.delay(100)
|
||||
.times(3);
|
||||
|
||||
const controller = new AbortController();
|
||||
const [aP2, bP2, cP2] = [
|
||||
api.get('/abort', { signal: controller.signal }),
|
||||
api.get('/abort', { signal: controller.signal }),
|
||||
api.get('/abort', { signal: controller.signal }),
|
||||
];
|
||||
|
||||
await expect(aP2).resolves.toStrictEqual({ message: 'Hello World' });
|
||||
controller.abort();
|
||||
|
||||
// Abort mid-execution:
|
||||
await expect(bP2).rejects.toThrowError('Request aborted');
|
||||
|
||||
// Abort scheduled:
|
||||
await expect(cP2).rejects.toThrowError('Request aborted');
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user