axios-axios/tests/smoke/deno/tests/cancel.smoke.test.ts
Jay 2f52f6b13b
feat: add checks to support deno and bun (#10652)
* feat: added smoke tests for deno

* feat: added bun smoke tests

* chore: added workflows for deno and bun

* chore: swap workflow implementation

* chore: apply ai suggestion

* chore: test alt install of bun deps

* chore: deno install

* chore: map bun file install

* chore: try a different approach for bun

* chore: unpack and then install for bun

* chore: remove un-needed step

* chore: try with tgx again for bun

* chore: alternative zip approach

* ci: full ci added back
2026-04-05 14:37:16 +02:00

82 lines
2.0 KiB
TypeScript

import { assertEquals } from '@std/assert';
import axios from 'axios';
const env = (fetch: any) => ({
fetch,
Request,
Response,
});
Deno.test('cancel: pre-aborted AbortController cancels request', async () => {
let fetchCallCount = 0;
const fetch = async () => {
fetchCallCount += 1;
return new Response(JSON.stringify({ ok: true }), {
status: 200,
headers: { 'Content-Type': 'application/json' },
});
};
const controller = new AbortController();
controller.abort();
const err = await axios
.get('https://example.com/cancel', {
adapter: 'fetch',
signal: controller.signal,
env: env(fetch),
})
.catch((e: any) => e);
assertEquals(axios.isCancel(err), true);
assertEquals(err.code, 'ERR_CANCELED');
assertEquals(fetchCallCount, 0);
});
Deno.test('cancel: in-flight abort cancels request', async () => {
const fetch = (_input: any, init?: any) =>
new Promise<Response>((_resolve, reject) => {
const timeout = setTimeout(() => {
reject(new DOMException('The operation was aborted', 'AbortError'));
}, 20);
if (init?.signal) {
if (init.signal.aborted) {
clearTimeout(timeout);
reject(new DOMException('The operation was aborted', 'AbortError'));
return;
}
init.signal.addEventListener(
'abort',
() => {
clearTimeout(timeout);
reject(new DOMException('The operation was aborted', 'AbortError'));
},
{ once: true }
);
}
});
const controller = new AbortController();
const request = axios.get('https://example.com/in-flight', {
adapter: 'fetch',
signal: controller.signal,
env: env(fetch),
});
controller.abort();
const err = await request.catch((e: any) => e);
assertEquals(axios.isCancel(err), true);
assertEquals(err.code, 'ERR_CANCELED');
});
Deno.test('cancel: isCancel returns false for plain Error', () => {
assertEquals(axios.isCancel(new Error('random')), false);
});