2
0
mirror of https://github.com/tenrok/axios.git synced 2026-06-17 19:21:29 +03:00
Files
axios/tests/smoke/bun/tests/timeout.smoke.test.ts
T
Yossi Eliaz 2344aab609 fix(fetch): preserve abort reasons in fetch adapter (#10806)
Based on the work in axios/axios#7191.

Co-authored-by: Mostafa-Khairy0 <mostafakhairy0305@gmail.com>
Co-authored-by: Jason Saayman <jasonsaayman@gmail.com>
2026-04-27 17:42:33 +02:00

51 lines
1.3 KiB
TypeScript

import { describe, expect, test } from 'bun:test';
import axios from 'axios';
const env = (fetch: typeof globalThis.fetch) => ({
fetch,
Request,
Response,
});
const createAbortedError = () => {
const error = new Error('The operation was aborted') as Error & { code?: string; name: string };
error.name = 'AbortError';
error.code = 'ECONNABORTED';
return error;
};
describe('timeout', () => {
test('timeout: 50 with never-resolving fetch mock rejects with ETIMEDOUT', async () => {
const fetch = (input: unknown, init?: RequestInit) =>
new Promise<Response>((_resolve, reject) => {
const signal = init?.signal || (input instanceof Request ? input.signal : undefined);
if (signal) {
if (signal.aborted) {
reject(createAbortedError());
return;
}
signal.addEventListener(
'abort',
() => {
reject(createAbortedError());
},
{ once: true }
);
}
});
const err = await axios
.get('https://example.com/timeout', {
adapter: 'fetch',
timeout: 50,
env: env(fetch),
})
.catch((e: any) => e);
expect(axios.isAxiosError(err)).toBe(true);
expect(err.code).toBe('ETIMEDOUT');
});
});