Имеется такой код
Как правильно замокать данные и протестировать запросы с Vitest?
axiosInstanse
import axios, {
AxiosError,
AxiosInstance,
AxiosResponse,
InternalAxiosRequestConfig,
} from "axios";
import Cookies from "js-cookie";
import TokenLogic from "../appLogic/tokenLogic/TokenLogic.ts";
import AuthService from "./service/AuthService.ts";
import { userStore } from "../context/context.ts";
import PathApp from "../routes/pathApp/PathApp.ts";
const BASE_URL: string = import.meta.env.VITE_API_URL;
type ErrorConfig =
| (InternalAxiosRequestConfig & { _isRetry?: boolean })
| undefined;
const $api: AxiosInstance = axios.create({
baseURL: BASE_URL,
withCredentials: true,
});
$api.interceptors.request.use((config) => {
const token = Cookies.get(TokenLogic.TOKEN);
if (typeof token === "string") {
config.headers.Authorization = `Bearer ${token}`;
}
return config;
});
$api.interceptors.response.use(
(config: AxiosResponse) => {
return config;
},
async (error: AxiosError) => {
const originalRequest: ErrorConfig = error.config;
const refreshTokenStore: string | undefined = Cookies.get(
TokenLogic.REFRESH_TOKEN,
);
if (
error.response?.status === 401 &&
refreshTokenStore &&
originalRequest &&
!originalRequest._isRetry
) {
originalRequest._isRetry = true;
const res = await AuthService.refresh(refreshTokenStore);
const { token, refreshToken } = res.data;
Cookies.set(TokenLogic.TOKEN, token);
Cookies.set(TokenLogic.REFRESH_TOKEN, refreshToken);
userStore.setAuthByToken(token);
return $api.request(originalRequest);
}
if (error.response?.status === 401) {
window.location.pathname = PathApp.authorization;
}
throw error;
},
);
const paramsPagination = (
page: number,
itemsPerPage: number,
): Record<PropertyKey, unknown> => ({
Page: page,
ItemsPerPage: itemsPerPage,
});
export default $api;
export { paramsPagination };
AuthServise
import $api from "../api.ts";
import { AxiosResponse } from "axios";
import IPostLoginUser from "../interfaces/request/IPostLoginUser.ts";
import IPostRegistrationUser from "../interfaces/request/IPostRegistrationUser.ts";
import IResponseRoles from "@/API/interfaces/response/IResponseRoles.ts";
interface AuthResponse {
token: string;
refreshToken: string;
}
class AuthService {
private readonly pathAccount: string = "/accounts";
public async login(
dataLogin: IPostLoginUser,
): Promise<AxiosResponse<AuthResponse>> {
return $api.post<AuthResponse>(`${this.pathAccount}/login`, dataLogin);
}
public async registration(
dataLogin: IPostRegistrationUser,
): Promise<AxiosResponse> {
return $api.post<AxiosResponse>(
`${this.pathAccount}/register`,
dataLogin,
);
}
public async refresh(
refreshToken: string,
): Promise<AxiosResponse<AuthResponse>> {
return $api.post<AuthResponse>(`${this.pathAccount}/refresh`, {
refreshToken: refreshToken,
});
}
public async roles(): Promise<AxiosResponse<IResponseRoles>> {
return $api.get(`${this.pathAccount}/roles`);
}
}
export default new AuthService();