{"id":661,"hash":"75bce83af615d4f9b85d04bb44a4a7d110eb7c052c3eb4beec69b2fb27056bb9","pattern":"Typescript and Jest: Avoiding type errors on mocked functions","full_message":"When wanting to mock external modules with Jest, we can use the jest.mock() method to auto-mock functions on a module. \n\nWe can then manipulate and interrogate the mocked functions on our mocked module as we wish.\n\nFor example, consider the following contrived example for mocking the axios module:\n\nimport myModuleThatCallsAxios from '../myModule';\nimport axios from 'axios';\n\njest.mock('axios');\n\nit('Calls the GET method as expected', async () => {\n  const expectedResult: string = 'result';\n\n  axios.get.mockReturnValueOnce({ data: expectedResult });\n  const result = await myModuleThatCallsAxios.makeGetRequest();\n\n  expect(axios.get).toHaveBeenCalled();\n  expect(result).toBe(expectedResult);\n});\n\nThe above will run fine in Jest but will throw a Typescript error:\n\n  Property 'mockReturnValueOnce' does not exist on type '(url:\n  string, config?: AxiosRequestConfig | undefined) => AxiosPromise'.\n\nThe typedef for axios.get rightly doesn't include a mockReturnValueOnce property. We can force Typescript to treat axios.get as an Object literal by wrapping it as Object(axios.get), but:\n\nWhat is the idiomatic way to mock functions while maintaining type safety?","ecosystem":"npm","package_name":"node.js","package_version":null,"solution":"Please use the mocked function from ts-jest\n\nThe mocked test helper provides typings on your mocked modules and even their deep methods, based on the typing of its source. It makes use of the latest TypeScript feature, so you even have argument types completion in the IDE (as opposed to jest.MockInstance).\n\nimport myModuleThatCallsAxios from '../myModule';\nimport axios from 'axios';\nimport { mocked } from 'ts-jest/utils'\n\njest.mock('axios');\n\n// OPTION - 1\nconst mockedAxios = mocked(axios, true)\n// your original `it` block\nit('Calls the GET method as expected', async () => {\n  const expectedResult: string = 'result';\n\n  mockedAxios.mockReturnValueOnce({ data: expectedResult });\n  const result = await myModuleThatCallsAxios.makeGetRequest();\n\n  expect(mockedAxios.get).toHaveBeenCalled();\n  expect(result).toBe(expectedResult);\n});\n\n// OPTION - 2\n// wrap axios in mocked at the place you use\nit('Calls the GET method as expected', async () => {\n  const expectedResult: string = 'result';\n\n  mocked(axios).get.mockReturnValueOnce({ data: expectedResult });\n  const result = await myModuleThatCallsAxios.makeGetRequest();\n\n  // notice how axios is wrapped in `mocked` call\n  expect(mocked(axios).get).toHaveBeenCalled();\n  expect(result).toBe(expectedResult);\n});\n\nI can't emphasise how great mocked is, no more type-casting ever.","confidence":0.95,"source":"stackoverflow","source_url":"https://stackoverflow.com/questions/51495473/typescript-and-jest-avoiding-type-errors-on-mocked-functions","votes":142,"created_at":"2026-04-19T04:51:27.266211+00:00","updated_at":"2026-04-19T04:51:27.266211+00:00"}