From cf62cc000e79dc0c711d77fc27447e699a1db7c8 Mon Sep 17 00:00:00 2001 From: Sam Maister Date: Tue, 12 Dec 2023 17:48:06 +0000 Subject: [PATCH] fix: move execute error into callback --- src/commands/execute.ts | 9 ++++++--- src/constants.ts | 5 ----- test/commands/execute.spec.ts | 7 +------ 3 files changed, 7 insertions(+), 14 deletions(-) diff --git a/src/commands/execute.ts b/src/commands/execute.ts index 8d5648f5e..b0d5d0f06 100644 --- a/src/commands/execute.ts +++ b/src/commands/execute.ts @@ -1,4 +1,3 @@ -import { CONTEXT_BRIDGE_NOT_AVAILABLE } from '../constants.js'; import type ElectronWorkerService from '../service.js'; export async function execute( @@ -23,13 +22,17 @@ export async function execute( } return browser.execute( - function executeWithinElectron(errMessage: string, script: string, ...args) { + function executeWithinElectron(script: string, ...args) { if (window.wdioElectron === undefined) { + const errMessage = + 'Electron context bridge not available! ' + + 'Did you import the service hook scripts into your application via e.g. ' + + "`import('wdio-electron-service/main')` and `import('wdio-electron-service/preload')`?\n\n" + + 'Find more information at https://webdriver.io/docs/desktop-testing/electron#api-configuration'; throw new Error(errMessage); } return window.wdioElectron.execute(script, args); }, - CONTEXT_BRIDGE_NOT_AVAILABLE, `${script}`, ...args, ) as ReturnValue; diff --git a/src/constants.ts b/src/constants.ts index 9dd862de2..e46e0b3c5 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -9,11 +9,6 @@ export const BUILD_TOOL_DETECTION_ERROR = export const APP_NAME_DETECTION_ERROR = 'No application name was detected, please set name / productName in your package.json or build tool configuration.'; export const CUSTOM_CAPABILITY_NAME = 'wdio:electronServiceOptions'; -export const CONTEXT_BRIDGE_NOT_AVAILABLE = - 'Electron context bridge not available! ' + - 'Did you import the service hook scripts into your application via e.g. ' + - "`import('wdio-electron-service/main')` and `import('wdio-electron-service/preload')`?\n\n" + - 'Find more information at https://webdriver.io/docs/desktop-testing/electron#api-configuration'; export enum Channel { Custom = 'wdio-electron', diff --git a/test/commands/execute.spec.ts b/test/commands/execute.spec.ts index 2edd051c7..a5a23c208 100644 --- a/test/commands/execute.spec.ts +++ b/test/commands/execute.spec.ts @@ -2,7 +2,6 @@ import { vi, describe, beforeEach, it, expect } from 'vitest'; import { execute } from '../../src/commands/execute'; import ElectronWorkerService from '../../src'; -import { CONTEXT_BRIDGE_NOT_AVAILABLE } from '../../src/constants'; describe('execute', () => { let workerService; @@ -40,10 +39,6 @@ describe('execute', () => { it('should execute a function', async () => { await execute.call(workerService, () => 1 + 2 + 3); - expect(globalThis.browser.execute).toHaveBeenCalledWith( - expect.any(Function), - CONTEXT_BRIDGE_NOT_AVAILABLE, - '() => 1 + 2 + 3', - ); + expect(globalThis.browser.execute).toHaveBeenCalledWith(expect.any(Function), '() => 1 + 2 + 3'); }); });