DEV Community

Discussion on: The only 3 steps you need to mock an API call in Jest

Collapse
 
viktorhsn profile image
Victor H Nascimento • Edited

Hi Zak,

Thank you very much for your article, it helped me a lot.
I am having trouble replicating this with typescript, it complains when I try to set the mockResolvedValue into axios get.

test("it should return permission true", async() => {
axios.get.mockResolvedValue({ //type error here.
true
});

expect(await wrapper.obterPermissaoUsuario("token","COD_APLIC","LIMIT")).toBe(true);
Enter fullscreen mode Exit fullscreen mode

});

Collapse
 
zaklaughton profile image
Zak Laughton

Hi Victor! Glad the article helped!

Typescript isn't great at determining the types of mocked values, but there are some great libraries to help. Personally, I've had great success using the mocked method from ts-jest. See details and usage examples here: ts-jest/test-helpers

Collapse
 
antoyne7 profile image
Antoine Braillard

try (axios.get as jest.Mock).mockReturnValue({})