You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// @ts-expect-error It's work in runtime, but I Haven't idea how to fix this type error
bw.getAllWindows=vi.fn(()=>bw.mock.instances);
bw.prototype.loadURL=vi.fn();
bw.prototype.on=vi.fn();
bw.prototype.destroy=vi.fn();
bw.prototype.isDestroyed=vi.fn();
bw.prototype.isMinimized=vi.fn();
bw.prototype.focus=vi.fn();
bw.prototype.restore=vi.fn();
return{BrowserWindow: bw};
});
Fix
Use MockedObject will fix this issue but the type of bw.prototype is any.
importtype{MockedObject}from'vitest';import{beforeEach,expect,test,vi}from'vitest';import{restoreOrCreateWindow}from'../src/mainWindow';import{BrowserWindow}from'electron';/** * Mock real electron BrowserWindow API */vi.mock('electron',()=>{constbw=vi.fn()asMockedObject<typeofBrowserWindow>;bw.getAllWindows=vi.fn(()=>bw.mock.instances);// bw.prototype is anybw.prototype.loadURL=vi.fn();bw.prototype.on=vi.fn();bw.prototype.destroy=vi.fn();bw.prototype.isDestroyed=vi.fn();bw.prototype.isMinimized=vi.fn();bw.prototype.focus=vi.fn();bw.prototype.restore=vi.fn();return{BrowserWindow: bw};});
For the strictest typing, use MockedClass which ports the prototype part of BrowerWindow class. Unfortunately there's a bug of MockedClass type in vistest (see vitest-dev/vitest#1730 , basically they put args and returns in wrong positions lol). I have to include a manual fix for that.
importtype{Constructable,Mocked,MockInstance}from'vitest';import{beforeEach,expect,test,vi}from'vitest';import{restoreOrCreateWindow}from'../src/mainWindow';import{BrowserWindow}from'electron';// Manual fix of MockedClass type// See https://github.com/vitest-dev/vitest/issues/1730typeMockedClass<TextendsConstructable>=MockInstance<Textendsnew(...args: infer P)=>any ? P : never,InstanceType<T>>&{prototype: Textends{// eslint-disable-next-line @typescript-eslint/no-explicit-anyprototype: any;} ? Mocked<T['prototype']> : never;}&T;/** * Mock real electron BrowserWindow API */vi.mock('electron',()=>{// Use "as unknown as" because vi.fn() does not have static methodsconstbw=vi.fn()asunknownasMockedClass<typeofBrowserWindow>;bw.getAllWindows=vi.fn(()=>bw.mock.instances);bw.prototype.loadURL=vi.fn();// Use "any" because the on function is overloaded// eslint-disable-next-line @typescript-eslint/no-explicit-anybw.prototype.on=vi.fn<any>();bw.prototype.destroy=vi.fn();bw.prototype.isDestroyed=vi.fn();bw.prototype.isMinimized=vi.fn();bw.prototype.focus=vi.fn();bw.prototype.restore=vi.fn();return{BrowserWindow: bw};});
All the fix does not change the runtime behaviors, just types.
The text was updated successfully, but these errors were encountered:
Describe the bug
The types of Mock of BrowserWindow use
vitest
internal typeMaybeMocked
vite-electron-builder/packages/main/tests/unit.spec.ts
Lines 1 to 24 in 1daa1d5
Fix
Use
MockedObject
will fix this issue but the type ofbw.prototype
isany
.For the strictest typing, use
MockedClass
which ports the prototype part ofBrowerWindow
class. Unfortunately there's a bug ofMockedClass
type invistest
(see vitest-dev/vitest#1730 , basically they put args and returns in wrong positions lol). I have to include a manual fix for that.All the fix does not change the runtime behaviors, just types.
The text was updated successfully, but these errors were encountered: