79229849

Date: 2024-11-27 10:31:50
Score: 1
Natty:
Report link

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.

Reasons:
  • Blacklisted phrase (1): stackoverflow
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Low reputation (1):
Posted by: Robert Wloch