{"id":676,"hash":"455ee2c01c7c8599bd5df48acecf1568b27eb61878d3e281d34053b180fcf940","pattern":"TypeError: default is not a function when using vitest","full_message":"I'm using vitest to do some unit tests in my vue application.\nI've written some tests but they fail with the error message: 'TypeError: default is not a function'.\nBut I do not use a function called default() in my code.\n\nimport getInfo from './info';\n\nvi.mock('axios', () => {\n    return {\n        default: {\n            get: vi.fn()\n        }\n    }\n});\n\ntest('fn getInfo() should request api with axios.get url', async () => {\n    const spyAxios = vi.spyOn(axios, 'get');\n    await getInfo('1234');\n    expect(spyAxios).toHaveBeenCalledWith(`${process.env.VUE_APP_API_BASE_URL}`);\n});\n\nIf I then execute npm run test the result is the following:\n\n FAIL  src/api/info/info.test.js > fn getInfo() should request api with axios.get url\nTypeError: default is not a function\n ❯ src/api/info/info.test.js:61:22\n     59| test('fn getInfo() should request api with axios.get url', async () => {\n     60|     const spyAxios = vi.spyOn(axios, 'get');\n     61|     await getInfo('1234');\n       |                  ^\n     62|     expect(spyAxios).toHaveBeenCalledWith(`${process.env.VUE_APP_API_BASE_URL}`);\n     63| });\n\nThe info.ts file looks like the following:\n\nimport { useLoginStore } from \"../../store/LoginStore\";\nimport axios from \"axios\";\n\n// eslint-disable-next-line\nexport async function getInfo(param: string) : Promise<any> {\n    const loginStore = useLoginStore();\n    axios.defaults.headers.common = {'Authorization': `Bearer ${loginStore.accessToken}`};\n    \n    const request = await axios.get(\n        process.env.VUE_APP_API_BASE_URL\n    );\n\n    if (request?.status == 200) {\n        return request.data;\n    }\n    else {\n        return null;\n    }\n}\n\nDoes anyone know what's going on here?","ecosystem":"npm","package_name":"vue.js","package_version":null,"solution":"The default attribute in your return object is not a function (default: {...}).  Instead, you would return something like this:\n\nvi.mock('axios', () => ({\n  default: () => ({\n    get: vi.fn(),\n    post: vi.fn(),\n  }),\n}));","confidence":0.8,"source":"stackoverflow","source_url":"https://stackoverflow.com/questions/73063186/typeerror-default-is-not-a-function-when-using-vitest","votes":10,"created_at":"2026-04-19T04:51:28.855074+00:00","updated_at":"2026-04-19T04:51:28.855074+00:00"}