Vitest error No <functionName> export is defined on the <path>
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
weatherApiController.ts
import { getWeatherData, formatWeatherData} from '../controller/weatherApiController'
import { describe, expect, vi, test } from 'vitest'
export async function formatWeatherData(start: String, end: String){
let data = await getWeatherData(start, end)
let formattedTemps = []
for(let i = 0; i < data.days.length; i++){
formattedTemps.push({datetime: data.days[i]['datetime'], tempmax: data.days[i]['tempmax'], tempmin: data.days[i]['tempmin']})
}
console.log(formattedTemps);
return formattedTemps
}
export async function getWeatherData(start: String, end: String) {
try{
let response = await fetch(`https://weather.visualcrossing.com/VisualCrossingWebServices/rest/services/timeline/Wermelskirchen/${start}/${end}?unitGroup=metric&include=days%2Ccurrent&key=<apiKey>&contentType=json`);
if(!response.ok){
throw new Error(`HTTP Error! Status: ${response.status}`);
}
let data = await response.json()
console.log(data);
return data
}catch(error){
console.error(error)
return null
}
}
weatherApiController.test.ts
test('mock3', async()=> {
let expected = [
{ datetime: '2023-03-12', tempmax: 10.4, tempmin: -0.2 },
{ datetime: '2023-03-13', tempmax: 15.4, tempmin: 9.1 }
]
vi.mock('../controller/weatherApiController.ts', async() => {
const actual = await vi.importActual("../controller/weatherApiController.ts")
return {
actual,
getWeatherData: async() => {
number: 3
},
formatWeatherData: async() => {
number:4
}
}
});
const result = await formatWeatherData('2023-03-12', '2023-03-13')
expect(result).toEqual(expected)
})
This is the error i get
Error: [vitest] No "formatWeatherData" export is defined on the "../controller/weatherApiController.ts" mock. Did you forget to return it from "vi.mock"?
If you need to partially mock a module, you can use "vi.importActual" inside:
i appreciate any help!
I tried to mock the API call to get consistent weather data. I expected to have a constant return value of the function.I had a similar issue where my mock looked like this: vi.mock('@/api/Client', () => { const API = VueMocks.API return API }) and vitest was erroring with Error: [vitest] No "default" export is defined on the "@/api/Client" mock. Did you forget to return it from "vi.mock"? If you need to partially mock a module, you can use "vi.importActual" inside: vi.mock("@/api/Client", async () => { const actual = await vi.importActual("@/api/Client") return { ...actual, // your mocked methods }, }) Reading up the vitest docs, There was a warning in the vitest document under vi.Mock that said: WARNING If you are mocking a module with default export, you will need to provide a default key within the returned factory function object. This is an ES modules-specific caveat, therefore jest documentation may differ as jest uses CommonJS modules. For example, ts vi.mock('./path/to/module.js', () => { return { default: { myDefaultKey: vi.fn() }, namedExport: vi.fn(), // etc... } }) In my actual @/api/Client file, which was a ts file, it looked like this export export class API { ... } <Stuff> export default new API(shallowReactive) So in my case I had to have a default that was returned, in addition to the module I wanted returned. I had to do something like this vi.mock('@/api/Client', () => { const API = VueMocks.API return { default: { ...API }, } }) Where for me VueMocks.API was a var object I had made myself that specified all the mocks required by the API already. In your case, you would probably have to do something like vi.mock('../controller/weatherApiController.ts', async() => { const actual = await vi.importActual("../controller/weatherApiController.ts") return { ...actual, getWeatherData: async() => { number: 3 }, formatWeatherData: async() => { number:4 } } Since the exports in your file above are export async function formatWeatherData([...]) and export async function getWeatherData([...]) It 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
Get this solution programmatically \u2014 free, no authentication.
curl https://depscope.dev/api/error/0b7ab78edee865baea839c9d80173077225161e7b79f53aa205e433962775285