I'm using Vitest and Vue Test Utils and got it mocked successfully. In a test-helpers.ts export this function:
export function mockCreateObjectUrl() {
const createObjectURLMock = vi.fn().mockImplementation((file: File) => {
return file.name
})
Reflect.deleteProperty(global.window.URL, 'createObjectURL')
window.URL.createObjectURL = createObjectURLMock
}
Then import that function in your spec.ts file and call it before the first describe
:
mockCreateObjectUrl()
The key lies in using Reflect
to delete the function first before replacing it. IDK, where I got it from years ago. Probably from somewhere here on stackoverflow.