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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *