How to use vi#mocked
correctly?
#853
-
Hello 🙋🏼♂️ I have a function that creates a Axios instance with some default settings, ie: import Axios, { type AxiosInstance } from "axios";
export function useHelixClient(token: string): AxiosInstance {
const { VITE_TWITCH_API, VITE_TWITCH_CLIENT_ID } = import.meta.env;
const client = Axios.create({ baseURL: VITE_TWITCH_API });
client.defaults.headers.common = {
"Client-Id": VITE_TWITCH_CLIENT_ID,
Accept: "application/vnd.twitchtv.v5+json",
Authorization: `Bearer ${token}`,
};
return client;
} But I'm getting a error when I try to mock this using
And here's the code of my test: import { describe, it, vi } from "vitest";
import { useHelixClient } from "@/api/client";
describe("API", async () => {
const client = await vi.mocked(useHelixClient("token"), true);
describe("A basic describe", () => {
it("A basic test", async () => {
client.get.mockResolvedValue({ status: 500 });
});
});
}); I'm not understand why it's undefined, since it appears to be a mocked function: Can anyone help me understand what I'm doing wrong? Thank you |
Beta Was this translation helpful? Give feedback.
Answered by
sheremet-va
Feb 25, 2022
Replies: 1 comment
-
|
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
rn4n
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
vi.mocked
is just a type helper. You need to youvi.mock
orvi.doMock
to actually mock something.