Skip to content

Commit

Permalink
fix: move execute error into callback
Browse files Browse the repository at this point in the history
  • Loading branch information
goosewobbler committed Dec 12, 2023
1 parent f44b917 commit cf62cc0
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 14 deletions.
9 changes: 6 additions & 3 deletions src/commands/execute.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { CONTEXT_BRIDGE_NOT_AVAILABLE } from '../constants.js';
import type ElectronWorkerService from '../service.js';

export async function execute<ReturnValue, InnerArguments extends unknown[]>(
Expand All @@ -23,13 +22,17 @@ export async function execute<ReturnValue, InnerArguments extends unknown[]>(
}

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;
Expand Down
5 changes: 0 additions & 5 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
7 changes: 1 addition & 6 deletions test/commands/execute.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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');
});
});

0 comments on commit cf62cc0

Please sign in to comment.