|
1 | | -import axios, { AxiosError, AxiosHeaders } from "axios"; |
| 1 | +import axios, { |
| 2 | + AxiosError, |
| 3 | + AxiosHeaders, |
| 4 | + type AxiosAdapter, |
| 5 | + type AxiosResponse, |
| 6 | + type InternalAxiosRequestConfig, |
| 7 | +} from "axios"; |
2 | 8 | import { vi } from "vitest"; |
3 | 9 | import * as vscode from "vscode"; |
4 | 10 |
|
@@ -559,7 +565,7 @@ export class MockOAuthSessionManager { |
559 | 565 | .mockResolvedValue({ access_token: "test-token" }); |
560 | 566 | readonly refreshIfAlmostExpired = vi.fn().mockResolvedValue(undefined); |
561 | 567 | readonly revokeRefreshToken = vi.fn().mockResolvedValue(undefined); |
562 | | - readonly isLoggedInWithOAuth = vi.fn().mockReturnValue(false); |
| 568 | + readonly isLoggedInWithOAuth = vi.fn().mockResolvedValue(false); |
563 | 569 | readonly clearOAuthState = vi.fn().mockResolvedValue(undefined); |
564 | 570 | readonly showReAuthenticationModal = vi.fn().mockResolvedValue(undefined); |
565 | 571 | readonly dispose = vi.fn(); |
@@ -618,7 +624,7 @@ export function createAxiosError( |
618 | 624 | return error; |
619 | 625 | } |
620 | 626 |
|
621 | | -type MockAdapterFn = ReturnType<typeof vi.fn>; |
| 627 | +type MockAdapterFn = ReturnType<typeof vi.fn<AxiosAdapter>>; |
622 | 628 |
|
623 | 629 | const AXIOS_MOCK_SETUP_EXAMPLE = ` |
624 | 630 | vi.mock("axios", async () => { |
@@ -680,40 +686,44 @@ export function setupAxiosMockRoutes( |
680 | 686 | mockAdapter: MockAdapterFn, |
681 | 687 | routes: Record<string, unknown>, |
682 | 688 | ): void { |
683 | | - mockAdapter.mockImplementation(async (config: { url?: string }) => { |
684 | | - for (const [pattern, value] of Object.entries(routes)) { |
685 | | - if (config.url?.includes(pattern)) { |
686 | | - if (value instanceof Error) { |
687 | | - throw value; |
| 689 | + mockAdapter.mockImplementation( |
| 690 | + async ( |
| 691 | + config: InternalAxiosRequestConfig, |
| 692 | + ): Promise<AxiosResponse<unknown>> => { |
| 693 | + for (const [pattern, value] of Object.entries(routes)) { |
| 694 | + if (config.url?.includes(pattern)) { |
| 695 | + if (value instanceof Error) { |
| 696 | + throw value; |
| 697 | + } |
| 698 | + const data = typeof value === "function" ? await value() : value; |
| 699 | + return { |
| 700 | + data, |
| 701 | + status: 200, |
| 702 | + statusText: "OK", |
| 703 | + headers: new AxiosHeaders(), |
| 704 | + config, |
| 705 | + }; |
688 | 706 | } |
689 | | - const data = typeof value === "function" ? await value() : value; |
690 | | - return { |
691 | | - data, |
692 | | - status: 200, |
693 | | - statusText: "OK", |
694 | | - headers: {}, |
695 | | - config, |
696 | | - }; |
697 | 707 | } |
698 | | - } |
699 | | - const error = new AxiosError( |
700 | | - `Request failed with status code 404`, |
701 | | - "ERR_BAD_REQUEST", |
702 | | - undefined, |
703 | | - undefined, |
704 | | - { |
705 | | - status: 404, |
706 | | - statusText: "Not Found", |
707 | | - headers: {}, |
708 | | - config: { headers: new AxiosHeaders() }, |
709 | | - data: { |
710 | | - message: "Not found", |
711 | | - detail: `No route matched: ${config.url}`, |
| 708 | + const error = new AxiosError( |
| 709 | + `Request failed with status code 404`, |
| 710 | + "ERR_BAD_REQUEST", |
| 711 | + undefined, |
| 712 | + undefined, |
| 713 | + { |
| 714 | + status: 404, |
| 715 | + statusText: "Not Found", |
| 716 | + headers: new AxiosHeaders(), |
| 717 | + config, |
| 718 | + data: { |
| 719 | + message: "Not found", |
| 720 | + detail: `No route matched: ${config.url}`, |
| 721 | + }, |
712 | 722 | }, |
713 | | - }, |
714 | | - ); |
715 | | - throw error; |
716 | | - }); |
| 723 | + ); |
| 724 | + throw error; |
| 725 | + }, |
| 726 | + ); |
717 | 727 | } |
718 | 728 |
|
719 | 729 | /** |
|
0 commit comments