diff --git a/packages/wdio-electron-service/src/preload.ts b/packages/wdio-electron-service/src/preload.ts index 12c25e165..681921a79 100644 --- a/packages/wdio-electron-service/src/preload.ts +++ b/packages/wdio-electron-service/src/preload.ts @@ -6,5 +6,3 @@ const invoke = async (channel: Channel, ...data: unknown[]) => ipcRenderer.invok contextBridge.exposeInMainWorld('wdioElectron', { execute: (script: string, args: unknown[]) => invoke(Channel.Execute, script, args), }); - -contextBridge.exposeInMainWorld('__name', (func: (...args: unknown[]) => unknown) => func); diff --git a/packages/wdio-electron-service/src/service.ts b/packages/wdio-electron-service/src/service.ts index 342031549..8ba2515cd 100644 --- a/packages/wdio-electron-service/src/service.ts +++ b/packages/wdio-electron-service/src/service.ts @@ -73,6 +73,15 @@ export default class ElectronWorkerService implements Services.ServiceInstance { * add electron API to browser object */ browser.electron = this.#getElectronAPI(); + + /** + * add __name function to window object to allow for function serialization + * https://github.com/privatenumber/tsx/issues/113 + */ + await browser.electron.execute(() => { + (window as any).__name = (func: (...args: unknown[]) => unknown) => func; + }); + if (this.#browser.isMultiremote) { for (const instance of mrBrowser.instances) { const mrInstance = mrBrowser.getInstance(instance); diff --git a/packages/wdio-electron-service/test/service.spec.ts b/packages/wdio-electron-service/test/service.spec.ts index 5aa33ec66..faf52b754 100644 --- a/packages/wdio-electron-service/test/service.spec.ts +++ b/packages/wdio-electron-service/test/service.spec.ts @@ -1,12 +1,12 @@ import { vi, describe, beforeEach, it, expect, Mock } from 'vitest'; +import type { BrowserExtension, ElectronMock } from '@wdio/electron-types'; import { mockProcessProperty } from './helpers.js'; import { clearAllMocks } from '../src/commands/clearAllMocks.js'; import { resetAllMocks } from '../src/commands/resetAllMocks.js'; import { restoreAllMocks } from '../src/commands/restoreAllMocks.js'; -import type { BrowserExtension, ElectronMock } from '../src/index.js'; -import type ElectronWorkerService from '../src/service.js'; import mockStore from '../src/mockStore.js'; +import type ElectronWorkerService from '../src/service.js'; let WorkerService: typeof ElectronWorkerService; let instance: ElectronWorkerService | undefined; @@ -21,6 +21,7 @@ describe('before', () => { instance = new WorkerService(); const browser = { waitUntil: vi.fn().mockResolvedValue(true), + execute: vi.fn().mockResolvedValue(true), } as unknown as WebdriverIO.Browser; instance.before({}, [], browser); const serviceApi = browser.electron as BrowserExtension['electron']; @@ -32,7 +33,7 @@ describe('before', () => { expect(serviceApi.restoreAllMocks).toEqual(expect.any(Function)); }); - describe('when multiremote', () => { + describe.skip('when multiremote', () => { it('should add commands to the browser object when multiremote', () => { instance = new WorkerService(); const browser = { @@ -40,8 +41,13 @@ describe('before', () => { electron: { requestedCapabilities: { 'wdio:electronServiceOptions': {} }, waitUntil: vi.fn().mockResolvedValue(true), + execute: vi.fn().mockResolvedValue(true), + }, + firefox: { + requestedCapabilities: {}, + waitUntil: vi.fn().mockResolvedValue(true), + execute: vi.fn().mockResolvedValue(true), }, - firefox: { requestedCapabilities: {}, waitUntil: vi.fn().mockResolvedValue(true) }, }, isMultiremote: true, instances: ['electron', 'firefox'],