{"id":684,"hash":"0b7ab78edee865baea839c9d80173077225161e7b79f53aa205e433962775285","pattern":"Vitest error No &lt;functionName&gt; export is defined on the &lt;path&gt;","full_message":"Here i have a function which formats Weatherdata fetched from an api, i want to mock this api call to have consistent weather data to only test the pure formatting function\nweatherApiController.ts\n\nimport { getWeatherData, formatWeatherData} from '../controller/weatherApiController'\nimport { describe, expect, vi, test } from 'vitest'\n\nexport async function formatWeatherData(start: String, end: String){\n    let data = await getWeatherData(start, end)\n    let formattedTemps = []\n    for(let i = 0; i < data.days.length; i++){\n        formattedTemps.push({datetime: data.days[i]['datetime'], tempmax: data.days[i]['tempmax'], tempmin: data.days[i]['tempmin']})\n    }\n    console.log(formattedTemps);\n    \n    return formattedTemps\n}\n\nexport async function getWeatherData(start: String, end: String) {\n    try{\n        let response = await fetch(`https://weather.visualcrossing.com/VisualCrossingWebServices/rest/services/timeline/Wermelskirchen/${start}/${end}?unitGroup=metric&include=days%2Ccurrent&key=<apiKey>&contentType=json`);\n        if(!response.ok){\n            throw new Error(`HTTP Error! Status: ${response.status}`);\n        }\n        let data = await response.json()\n        console.log(data);\n        \n        return data\n    }catch(error){\n        console.error(error)\n        return null\n    }\n}\n\nweatherApiController.test.ts\n\ntest('mock3', async()=> {\n    let expected = [\n  { datetime: '2023-03-12', tempmax: 10.4, tempmin: -0.2 },\n  { datetime: '2023-03-13', tempmax: 15.4, tempmin: 9.1 }\n]\n    vi.mock('../controller/weatherApiController.ts', async() => {\n        const actual = await vi.importActual(\"../controller/weatherApiController.ts\")\n        return {\n          actual,\n          getWeatherData: async() => {\n            number: 3\n          },\n          formatWeatherData: async() => {\n            number:4\n          }\n        }\n      });\n      const result = await formatWeatherData('2023-03-12', '2023-03-13')\n      expect(result).toEqual(expected)\n})\n\nThis is the error i get\nError: [vitest] No \"formatWeatherData\" export is defined on the \"../controller/weatherApiController.ts\" mock. Did you forget to return it from \"vi.mock\"?\nIf you need to partially mock a module, you can use \"vi.importActual\" inside:\n\ni appreciate any help!\n\nI tried to mock the API call to get consistent weather data. I expected to have a constant return value of the function.","ecosystem":"npm","package_name":"typescript","package_version":null,"solution":"I had a similar issue where my mock looked like this:\n\nvi.mock('@/api/Client', () => {\n    const API = VueMocks.API\n    return API\n})\n\nand vitest was erroring with\n\nError: [vitest] No \"default\" export is defined on the \"@/api/Client\" mock. \nDid you forget to return it from \"vi.mock\"?\nIf you need to partially mock a module, you can use \"vi.importActual\" inside:\n\nvi.mock(\"@/api/Client\", async () => {\n  const actual = await vi.importActual(\"@/api/Client\")\n  return {\n    ...actual,\n    // your mocked methods\n  },\n})\n\nReading up the vitest docs, There was a warning in the vitest document under vi.Mock that said:\n\nWARNING\n\nIf you are mocking a module with default export, you will need to\nprovide a default key within the returned factory function object.\nThis is an ES modules-specific caveat, therefore jest documentation\nmay differ as jest uses CommonJS modules. For example, ts\n\nvi.mock('./path/to/module.js', () => {   return {\n    default: { myDefaultKey: vi.fn() },\n    namedExport: vi.fn(),\n    // etc...   } })\n\nIn my actual @/api/Client file, which was a ts file, it looked like this\n\nexport export class API { ... }\n\n<Stuff>\n\nexport default new API(shallowReactive)\n\nSo in my case I had to have a default that was returned, in addition to the module I wanted returned.\n\nI had to do something like this\n\nvi.mock('@/api/Client', () => {\n    const API = VueMocks.API\n    return {\n        default: { ...API },\n    }\n})\n\nWhere for me VueMocks.API was a var object I had made myself that specified all the mocks required by the API already.\n\nIn your case, you would probably have to do something like\n\nvi.mock('../controller/weatherApiController.ts', async() => {\n    const actual = await vi.importActual(\"../controller/weatherApiController.ts\")\n    return {\n      ...actual,\n      getWeatherData: async() => {\n        number: 3\n      },\n      formatWeatherData: async() => {\n        number:4\n      }\n    }\n\nSince the exports in your file above are export async function formatWeatherData([...]) and export async function getWeatherData([...])\n\nIt also looked like you forgot to add the ... for copying the value of actual, because in yours it is return { actual when it should be return { ...actual","confidence":0.9,"source":"stackoverflow","source_url":"https://stackoverflow.com/questions/75890929/vitest-error-no-functionname-export-is-defined-on-the-path","votes":6,"created_at":"2026-04-19T04:51:28.860273+00:00","updated_at":"2026-04-19T04:51:28.860273+00:00"}