mirror of
https://github.com/axios/axios.git
synced 2026-04-12 02:31:57 +08:00
* chore: port karma tests * chore: port karma tests * chore: port karma tests * chore: tests * chore: tests * chore: tests * chore: fix issues with port collisions * refactor: utils tests * refactor: utils tests * refactor: utils tests * refactor: tests to vitests * refactor: tests to vitests * refactor: tests to vitests * refactor: tests to vitests * refactor: tests to vitests * refactor: tests to vitests * refactor: tests to vitests * refactor: ci * chore: install pw deps * chore: fixx ai feedback * chore: wip compatability tests * chore: wip compatability tests * chore: wip compatability tests * refactor: wip smoke * chore: smoke test run * chore: update unzip * chore: update testing * chore: update testing * chore: update testing * chore: update testing * chore: update testing * chore: skip tests that cannot run on node 16 and lower * chore: fix 16x under tests * chore: rest of tests * fix: functions and runs * feat: added tests for esm smoke * feat: added smoke * chore: ignore ai gen plans * chore: ci fixes * chore: fix small p2s
100 lines
2.5 KiB
JavaScript
100 lines
2.5 KiB
JavaScript
import { describe, expect, it, vi } from 'vitest';
|
|
|
|
import settle from '../../lib/core/settle.js';
|
|
import AxiosError from '../../lib/core/AxiosError.js';
|
|
|
|
describe('core::settle (vitest browser)', () => {
|
|
it('resolves when response status is missing', () => {
|
|
const resolve = vi.fn();
|
|
const reject = vi.fn();
|
|
const response = {
|
|
config: {
|
|
validateStatus: () => true,
|
|
},
|
|
};
|
|
|
|
settle(resolve, reject, response);
|
|
|
|
expect(resolve).toHaveBeenCalledOnce();
|
|
expect(resolve).toHaveBeenCalledWith(response);
|
|
expect(reject).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('resolves when validateStatus is not configured', () => {
|
|
const resolve = vi.fn();
|
|
const reject = vi.fn();
|
|
const response = {
|
|
status: 500,
|
|
config: {},
|
|
};
|
|
|
|
settle(resolve, reject, response);
|
|
|
|
expect(resolve).toHaveBeenCalledOnce();
|
|
expect(resolve).toHaveBeenCalledWith(response);
|
|
expect(reject).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('resolves when validateStatus returns true', () => {
|
|
const resolve = vi.fn();
|
|
const reject = vi.fn();
|
|
const response = {
|
|
status: 500,
|
|
config: {
|
|
validateStatus: () => true,
|
|
},
|
|
};
|
|
|
|
settle(resolve, reject, response);
|
|
|
|
expect(resolve).toHaveBeenCalledOnce();
|
|
expect(resolve).toHaveBeenCalledWith(response);
|
|
expect(reject).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('rejects with an AxiosError when validateStatus returns false', () => {
|
|
const resolve = vi.fn();
|
|
const reject = vi.fn();
|
|
const request = {
|
|
path: '/foo',
|
|
};
|
|
const response = {
|
|
status: 500,
|
|
config: {
|
|
validateStatus: () => false,
|
|
},
|
|
request,
|
|
};
|
|
|
|
settle(resolve, reject, response);
|
|
|
|
expect(resolve).not.toHaveBeenCalled();
|
|
expect(reject).toHaveBeenCalledOnce();
|
|
|
|
const reason = reject.mock.calls[0][0];
|
|
expect(reason).toBeInstanceOf(AxiosError);
|
|
expect(reason.message).toBe('Request failed with status code 500');
|
|
expect(reason.code).toBe(AxiosError.ERR_BAD_RESPONSE);
|
|
expect(reason.config).toBe(response.config);
|
|
expect(reason.request).toBe(request);
|
|
expect(reason.response).toBe(response);
|
|
});
|
|
|
|
it('passes response status to validateStatus', () => {
|
|
const resolve = vi.fn();
|
|
const reject = vi.fn();
|
|
const validateStatus = vi.fn();
|
|
const response = {
|
|
status: 500,
|
|
config: {
|
|
validateStatus,
|
|
},
|
|
};
|
|
|
|
settle(resolve, reject, response);
|
|
|
|
expect(validateStatus).toHaveBeenCalledOnce();
|
|
expect(validateStatus).toHaveBeenCalledWith(500);
|
|
});
|
|
});
|