fix: improve test path resolution in Detox pilot (#4802)

This commit is contained in:
Yaroslav Serhieiev
2025-07-14 14:09:47 +03:00
committed by GitHub
parent c65fe55339
commit 6b72360029
7 changed files with 119 additions and 12 deletions
+7 -1
View File
@@ -9,7 +9,7 @@
// * Dor Ben Baruch <https://github.com/Dor256>
import { BunyanDebugStreamOptions } from 'bunyan-debug-stream';
import type { Pilot, PromptHandler as _PromptHandler } from '@wix-pilot/core'
import type { Config as PilotConfig, Pilot, PromptHandler as _PromptHandler } from '@wix-pilot/core'
declare global {
namespace Detox {
@@ -1361,6 +1361,12 @@ declare global {
* @param promptHandler The prompt handler to use.
*/
init: (promptHandler: PromptHandler) => void;
/**
* Sets the default values for the Pilot configuration.
* This method is not part of the public API and is subject to change.
* @internal
*/
setDefaults(defaults: Partial<PilotConfig>): void;
}
type PromptHandler = _PromptHandler;
+2 -2
View File
@@ -68,8 +68,8 @@
"wtfnode": "^0.9.1"
},
"dependencies": {
"@wix-pilot/core": "^3.3.0",
"@wix-pilot/detox": "^1.0.11",
"@wix-pilot/core": "^3.4.1",
"@wix-pilot/detox": "^1.0.13",
"ajv": "^8.6.3",
"bunyan": "^1.8.12",
"bunyan-debug-stream": "^3.1.0",
@@ -125,6 +125,12 @@ class DetoxCircusEnvironment extends WithEmitter(NodeEnvironment) {
await detox.installWorker(opts);
}
detox.worker.pilot.setDefaults({
testContext: {
getCurrentTestFilePath: () => path.resolve(this.testPath),
},
});
return detox.worker;
}
+18 -2
View File
@@ -5,10 +5,18 @@ const detox = require('../..');
/** @type {Detox.PilotFacade} */
class DetoxPilot {
/**
* @param {Partial<import('@wix-pilot/core').Config>} [defaults] Arbitrary config defaults for the Pilot.
*/
constructor(defaults = {}) {
this._defaults = defaults;
}
init(promptHandler) {
this.pilot = new Pilot({
this.pilot = new Pilot({
...this._defaults,
frameworkDriver: new DetoxFrameworkDriver(detox),
promptHandler: promptHandler
promptHandler: promptHandler,
});
}
@@ -44,6 +52,14 @@ class DetoxPilot {
isInitialized(){
return !!this.pilot;
}
/**
* @param {Partial<import('@wix-pilot/core').Config>} [defaults] Arbitrary config defaults for the Pilot.
* @internal
*/
setDefaults(defaults) {
Object.assign(this._defaults, defaults);
}
}
module.exports = DetoxPilot;
+50
View File
@@ -86,4 +86,54 @@ describe('DetoxPilot', () => {
expect(() => detoxPilot.end()).toThrow('DetoxPilot is not initialized');
});
});
describe('setDefaults', () => {
it('should merge defaults into init configuration', () => {
const testDefaults = {
testContext: {
someProperty: 'test-value'
}
};
detoxPilot.setDefaults(testDefaults);
detoxPilot.init(mockPromptHandler);
expect(Pilot).toHaveBeenCalledWith({
frameworkDriver: expect.any(DetoxFrameworkDriver),
promptHandler: mockPromptHandler,
testContext: {
someProperty: 'test-value'
}
});
});
it('should work like merge - multiple setDefaults calls accumulate', () => {
const firstDefaults = {
testContext: {
property1: 'value1'
}
};
const secondDefaults = {
cacheOptions: {
property2: 'value2'
}
};
detoxPilot.setDefaults(firstDefaults);
detoxPilot.setDefaults(secondDefaults);
detoxPilot.init(mockPromptHandler);
expect(Pilot).toHaveBeenCalledWith({
frameworkDriver: expect.any(DetoxFrameworkDriver),
promptHandler: mockPromptHandler,
testContext: {
property1: 'value1'
},
cacheOptions: {
property2: 'value2'
}
});
});
});
});
+33 -3
View File
@@ -1,10 +1,11 @@
import type { Circus } from '@jest/types';
import { pilot } from 'detox';
import {
globalSetup,
globalTeardown,
DetoxCircusEnvironment,
DetoxCircusListener,
DetoxCircusListenerConstructorOpts
DetoxCircusListenerConstructorOpts,
globalSetup,
globalTeardown
} from 'detox/runners/jest';
class NoneListener implements DetoxCircusListener {}
@@ -143,6 +144,35 @@ class CustomEnvironment extends DetoxCircusEnvironment {
});
}
async initDetox() {
const instance: DetoxInternals.Worker = await super.initDetox();
// Test 1: setDefaults should work with partial config
pilot.setDefaults({
testContext: {
getCurrentTestFilePath: () => '/default/path.js',
}
});
// Test 2: Simple approach with promptHandler
pilot.init({
async runPrompt(_prompt: string, _image?: string): Promise<string> {
return 'mock response';
},
isSnapshotImageSupported(): boolean {
return true;
},
});
// Test 3: Verify the methods are available after init
await pilot.perform('tap on login button');
await pilot.autopilot('log into the app');
pilot.extendAPICatalog([]);
return instance;
}
handleTestEvent(event: Circus.Event, state: Circus.State): void | Promise<void> {
if (event.name === 'test_done') {
console.log(event.test.name);
+3 -4
View File
@@ -27,18 +27,17 @@ For a more detailed guide on integrating Wix Pilot in your tests, refer to the \
## Methods
- [`pilot.init()`](#pilotinitprompthandler-detox)
- [`pilot.init()`](#pilotinitprompthandler)
- [`pilot.perform()`](#pilotperformsteps)
- [`pilot.autopilot()`](#pilotautopilotgoal)
## `pilot.init(promptHandler, detox)`
## `pilot.init(promptHandler)`
Initializes Pilot with the given prompt handler. Must be called before any other Pilot methods.
**Parameters:**
- `promptHandler` (PromptHandler): An object implementing the [`PromptHandler`](#prompthandler-interface) interface.
- `detox` (DetoxInstance): The Detox instance to integrate with Wix Pilot.
**Example:**
@@ -48,7 +47,7 @@ const OpenAIPromptHandler = require('./OpenAIPromptHandler');
beforeAll(() => {
const promptHandler = new OpenAIPromptHandler('YOUR_OPENAI_API_KEY');
pilot.init(promptHandler, detox);
pilot.init(promptHandler);
});
```