Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

TypeScript says mockResolvedValue does not exist on an axios or fetch function

ErrorProperty 'mockResolvedValue' does not exist on type '<T = any, R = AxiosResponse<T, any>, D = any>(url: string, config?: AxiosRequestConfig<D>) => Promise<R>'

Short answer

TypeScript sees axios.get as a normal function, not a Jest mock, so it has no mockResolvedValue. The module is mocked at run time but not in the types. Mock the module with jest.mock, then wrap the function with jest.mocked(), or type the module with jest.Mocked, so the mock methods appear.

  • Applies to Jest 29, TypeScript 5, axios 1.x
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Check that the module is mocked at all, then look at how the function is referenced in the failing line.

    grep -n "jest.mock" path/to/file.test.ts
    grep -n "mockResolvedValue" path/to/file.test.ts
  2. 2Match what you see, then jump to its fix

    1. 1The error names the function's own signature, such as axios.get, and the test calls axios.get.mockResolvedValue(...).The function is not typed as a Jest mockFix
    2. 2You set up several functions of the same module and every line gives the same error.The whole module needs the mocked typeFix
    3. 3The mock is created with jest.fn() and mockResolvedValue rejects the value you pass.A hand-made jest.fn() has no return type to resolveFix

What does the error look like?

  • Property 'mockResolvedValue' does not exist on type '<T = any, R = AxiosResponse<T, any>, D = any>(url: string, config?: AxiosRequestConfig<D>) => Promise<R>'

Cause 1 of 3

The function is not typed as a Jest mock

1Check

Jest's docs say jest.mocked() wraps the types of the source object and its nested members with type definitions of Jest mock functions.

grep -n "axios" path/to/file.test.ts

2Fix

Mock the module, then wrap the member you set up in jest.mocked().

import axios from 'axios';
jest.mock('axios');

const mockedGet = jest.mocked(axios.get);
mockedGet.mockResolvedValue({ data: { id: 1 } });

3Verify

Run the test file; the type error is gone and the mock returns the value.

npx jest path/to/file.test.ts

Confirmed by jestjs.io

Did this fix it?

Cause 2 of 3

The whole module needs the mocked type

1Check

The jest.Mocked<Source> utility type returns the source type wrapped with Jest mock function types, so one declaration covers the module.

grep -n "jest.Mocked" path/to/file.test.ts

2Fix

Declare the module once as jest.Mocked and call the mock methods on it.

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

mockedAxios.get.mockResolvedValue({ data: [] });

3Verify

Run the test; the line compiles and your code receives the data.

npx jest path/to/file.test.ts

Confirmed by jestjs.io

Did this fix it?

Cause 3 of 3

A hand-made jest.fn() has no return type to resolve

1Check

With Jest's TypeScript setup, jest.fn takes the function type as a type argument; the docs import jest from @jest/globals for this.

grep -n "jest.fn" path/to/file.test.ts

2Fix

Give jest.fn the function type it should have, so mockResolvedValue knows the resolved type.

import { jest, test } from '@jest/globals';

const getUser = jest.fn<() => Promise<number>>().mockResolvedValue(43);

3Verify

Run the test file.

npx jest path/to/file.test.ts

Confirmed by jestjs.io

Did this fix it?

How do I stop it coming back?

  • Call jest.mock for a module before you use jest.mocked on its members.
  • Create typed mocks in one test helper file and import them, so every test uses the same pattern.
  • Import Jest APIs from @jest/globals in TypeScript projects, as the Jest docs do for their TypeScript examples.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Web apps

tsconfig rejects allowImportingTsExtensions

An error that allowImportingTsExtensions needs noEmit or emitDeclarationOnly

allowImportingTsExtensions lets files import each other with .ts or .tsx extensions, and TypeScript only allows it together with noEmit or emitDeclarationOnly.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.