How to add types of Axios mock using Jest and TypeScript?

Sometimes, we want to add types of Axios mock using Jest and TypeScript.

In this article, we’ll look at how to add types of Axios mock using Jest and TypeScript.

How to add types of Axios mock using Jest and TypeScript?

To add types of Axios mock using Jest and TypeScript, we add the jest.Mocked<typeof axios> type.

For instance, we write

import axios from "axios";
jest.mock("axios");
const mockedAxios = axios as jest.Mocked<typeof axios>;

to get the type of axios with typeof axios.

Then we get the type for the mocked version of axios by wrapping jest.Mocked<> around it.

Conclusion

To add types of Axios mock using Jest and TypeScript, we add the jest.Mocked<typeof axios> type.