Categories
TypeScript Answers

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

Spread the love

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.

Leave a Reply

Your email address will not be published.

If you like the content of this blog, subscribe to my email list to get exclusive articles not available to anyone else.