Skip to content

Commit 4d80fcb

Browse files
committed
refactor(tauri): update script handling in execute function
- Modified the `execute` function to wrap string scripts in an IIFE, ensuring they are callable as statement expressions. - Enhanced comments for clarity on the handling of both string and function scripts, improving understanding of the execution flow.
1 parent 5f2526b commit 4d80fcb

2 files changed

Lines changed: 53 additions & 7 deletions

File tree

packages/tauri-service/src/service.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -319,22 +319,22 @@ export default class TauriWorkerService {
319319
...args: InnerArguments
320320
): Promise<ReturnValue> {
321321
// For functions: use .toString() - produces valid JS function source
322-
// For strings: pass as-is (wrapper template wraps in parentheses to make callable)
323-
const scriptString = typeof script === 'function' ? script.toString() : script;
322+
// For strings:
323+
// - embedded: pass as-is (WebDriver handles execution)
324+
// - non-embedded: wrap in async IIFE to make statement expressions callable
325+
const scriptString =
326+
typeof script === 'function' ? script.toString() : isEmbedded ? script : `(async () => ${script})()`;
324327

325328
if (isEmbedded) {
326329
// For embedded WebDriver: skip console wrapper as console forwarding
327330
// is handled by tauri-plugin-webdriver.
328331
return originalExecute(scriptString, ...args) as Promise<ReturnValue>;
329332
}
330333

331-
// For tauri-driver: use sync execute with console wrapper
332-
// Note: scriptString is passed as-is - wrap in IIFE to make both strings and functions callable
333-
// For string scripts: "return x" -> "(() => return x)()" - wraps statement as expression
334-
// For function scripts: "(a,b) => a+b" -> "((a,b) => a+b)()" - works as IIFE
334+
// For non-embedded (tauri-driver/official): use sync execute with console wrapper
335335
const wrappedScript = `
336336
${CONSOLE_WRAPPER_SCRIPT}
337-
return ((${scriptString})()).apply(null, arguments);
337+
return (${scriptString}).apply(null, arguments);
338338
`;
339339

340340
return originalExecute(wrappedScript, ...args) as Promise<ReturnValue>;

packages/tauri-service/test/service.spec.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,52 @@ describe('TauriWorkerService', () => {
9191

9292
expect(firstExecute).toBe(secondExecute);
9393
});
94+
95+
it('should wrap string scripts in IIFE for non-embedded providers', () => {
96+
const mockExecute = vi.fn().mockResolvedValue(undefined);
97+
const mockBrowser = createMockBrowser({ execute: mockExecute });
98+
const service = new TauriWorkerService({ driverProvider: 'official' }, { 'wdio:tauriServiceOptions': {} });
99+
100+
(service as any).patchBrowserExecute(mockBrowser);
101+
mockBrowser.execute('return document.title');
102+
103+
expect(mockExecute).toHaveBeenCalledWith(expect.stringContaining('(async () => return document.title)()'));
104+
});
105+
106+
it('should pass function scripts as-is for non-embedded providers', () => {
107+
const mockExecute = vi.fn().mockResolvedValue(undefined);
108+
const mockBrowser = createMockBrowser({ execute: mockExecute });
109+
const service = new TauriWorkerService({ driverProvider: 'official' }, { 'wdio:tauriServiceOptions': {} });
110+
111+
(service as any).patchBrowserExecute(mockBrowser);
112+
const testFn = (a: number, b: number) => a + b;
113+
mockBrowser.execute(testFn as any, 1, 2);
114+
115+
expect(mockExecute).toHaveBeenCalledWith(expect.stringContaining('(a, b) => a + b'), 1, 2);
116+
});
117+
118+
it('should pass string scripts as-is for embedded provider', () => {
119+
const mockExecute = vi.fn().mockResolvedValue(undefined);
120+
const mockBrowser = createMockBrowser({ execute: mockExecute });
121+
const service = new TauriWorkerService({ driverProvider: 'embedded' }, { 'wdio:tauriServiceOptions': {} });
122+
123+
(service as any).patchBrowserExecute(mockBrowser);
124+
mockBrowser.execute('return document.title');
125+
126+
expect(mockExecute).toHaveBeenCalledWith('return document.title');
127+
});
128+
129+
it('should pass function scripts as-is for embedded provider', () => {
130+
const mockExecute = vi.fn().mockResolvedValue(undefined);
131+
const mockBrowser = createMockBrowser({ execute: mockExecute });
132+
const service = new TauriWorkerService({ driverProvider: 'embedded' }, { 'wdio:tauriServiceOptions': {} });
133+
134+
(service as any).patchBrowserExecute(mockBrowser);
135+
const testFn = (a: number, b: number) => a + b;
136+
mockBrowser.execute(testFn as any, 1, 2);
137+
138+
expect(mockExecute).toHaveBeenCalledWith(expect.stringContaining('(a, b) => a + b'), 1, 2);
139+
});
94140
});
95141

96142
describe('before()', () => {

0 commit comments

Comments
 (0)