11import net from 'node:net' ;
2- import { describe , it , beforeEach , afterEach } from 'node:test' ;
2+ import { describe , it , beforeEach , afterEach , mock } from 'node:test' ;
33
44import xcode from 'appium-xcode' ;
55import { JWProxy } from 'appium/driver.js' ;
66import { use , expect } from 'chai' ;
77import chaiAsPromised from 'chai-as-promised' ;
8- import esmock from 'esmock' ;
98import { createSandbox , type SinonSandbox , type SinonStubbedMember } from 'sinon' ;
109
10+ import * as helpersIndexModule from '../../lib/commands/helpers/index.js' ;
1111import { RealDevice } from '../../lib/device/real-device-management.js' ;
12+ import * as realDeviceManagementModule from '../../lib/device/real-device-management.js' ;
13+ import * as simulatorManagementModule from '../../lib/device/simulator-management.js' ;
14+ import * as wdaHostOpsModule from '../../lib/device/wda-host-ops.js' ;
1215import type { XCUITestDriverOpts } from '../../lib/driver.js' ;
1316import { mergeDeep } from '../../lib/utils/index.js' ;
1417import { UNIT_LONG_TIMEOUT_MS } from './helpers.js' ;
1518
1619use ( chaiAsPromised ) ;
1720
21+ // driver.js consumes checkAppPresent/getAndCheckXcodeVersion/installAUT through this barrel, so
22+ // it (not the individual modules) is what must be mocked: once a re-exporting module has been
23+ // evaluated, its bindings to a dependency are fixed and don't observe a later mock.module() call
24+ // for that dependency, only a mock.module() call for the barrel's own specifier is picked up.
25+ const HELPERS_INDEX_PATH = '../../lib/commands/helpers/index.js' ;
26+ const APP_PATH = '../../lib/commands/helpers/app.js' ;
27+ const REAL_DEVICE_MANAGEMENT_PATH = '../../lib/device/real-device-management.js' ;
28+ const SIMULATOR_MANAGEMENT_PATH = '../../lib/device/simulator-management.js' ;
29+
30+ let importCounter = 0 ;
31+ function importFresh ( specifier : string ) {
32+ return import ( `${ specifier } ?mock=${ importCounter ++ } ` ) ;
33+ }
34+
1835const defaultCheckAppPresent = async ( ) => { } ;
1936const defaultAssertWdaHostSessionCapsSupported = ( ) => { } ;
2037const defaultAssertWdaHostPlatformSupported = ( ) => { } ;
@@ -39,46 +56,39 @@ let currentInstallToRealDevice: (...args: any[]) => any = defaultInstallToRealDe
3956let currentInstallToSimulator : ( ...args : any [ ] ) => any = defaultInstallToSimulator ;
4057let currentInstallAUT : ( ...args : any [ ] ) => any = defaultInstallAUT ;
4158
42- const { XCUITestDriver} = await esmock (
43- '../../lib/driver.js' ,
44- import . meta. url ,
45- { } ,
46- {
47- '../../lib/commands/helpers/validation.js' : {
48- checkAppPresent : ( ...args : any [ ] ) => currentCheckAppPresent ( ...args ) ,
49- } ,
50- '../../lib/commands/helpers/xcode.js' : {
51- getAndCheckXcodeVersion : ( ...args : any [ ] ) => currentGetAndCheckXcodeVersion ( ...args ) ,
52- } ,
53- '../../lib/device/wda-host-ops.js' : {
54- assertWdaHostSessionCapsSupported : ( ...args : any [ ] ) => currentAssertWdaHostSessionCapsSupported ( ...args ) ,
55- assertWdaHostPlatformSupported : ( ...args : any [ ] ) => currentAssertWdaHostPlatformSupported ( ...args ) ,
56- } ,
57- '../../lib/device/real-device-management.js' : {
58- installToRealDevice : ( ...args : any [ ] ) => currentInstallToRealDevice ( ...args ) ,
59- } ,
60- '../../lib/device/simulator-management.js' : {
61- installToSimulator : ( ...args : any [ ] ) => currentInstallToSimulator ( ...args ) ,
62- } ,
63- '../../lib/commands/helpers/app.js' : {
64- installAUT : ( ...args : any [ ] ) => currentInstallAUT ( ...args ) ,
65- } ,
59+ mock . module ( HELPERS_INDEX_PATH , {
60+ namedExports : {
61+ ...helpersIndexModule ,
62+ checkAppPresent : ( ...args : any [ ] ) => currentCheckAppPresent ( ...args ) ,
63+ getAndCheckXcodeVersion : ( ...args : any [ ] ) => currentGetAndCheckXcodeVersion ( ...args ) ,
64+ installAUT : ( ...args : any [ ] ) => currentInstallAUT ( ...args ) ,
65+ } ,
66+ } ) ;
67+ mock . module ( '../../lib/device/wda-host-ops.js' , {
68+ namedExports : {
69+ ...wdaHostOpsModule ,
70+ assertWdaHostSessionCapsSupported : ( ...args : any [ ] ) => currentAssertWdaHostSessionCapsSupported ( ...args ) ,
71+ assertWdaHostPlatformSupported : ( ...args : any [ ] ) => currentAssertWdaHostPlatformSupported ( ...args ) ,
72+ } ,
73+ } ) ;
74+ mock . module ( REAL_DEVICE_MANAGEMENT_PATH , {
75+ namedExports : {
76+ ...realDeviceManagementModule ,
77+ installToRealDevice : ( ...args : any [ ] ) => currentInstallToRealDevice ( ...args ) ,
6678 } ,
67- ) ;
68-
69- const { installAUT : installAUTWithRealDeviceMocks } = await esmock (
70- '../../lib/commands/helpers/app.js' ,
71- import . meta. url ,
72- { } ,
73- {
74- '../../lib/device/real-device-management.js' : {
75- installToRealDevice : ( ...args : any [ ] ) => currentInstallToRealDevice ( ...args ) ,
76- } ,
77- '../../lib/device/simulator-management.js' : {
78- installToSimulator : ( ...args : any [ ] ) => currentInstallToSimulator ( ...args ) ,
79- } ,
79+ } ) ;
80+ mock . module ( SIMULATOR_MANAGEMENT_PATH , {
81+ namedExports : {
82+ ...simulatorManagementModule ,
83+ installToSimulator : ( ...args : any [ ] ) => currentInstallToSimulator ( ...args ) ,
8084 } ,
81- ) ;
85+ } ) ;
86+
87+ const { XCUITestDriver} = await importFresh ( '../../lib/driver.js' ) ;
88+
89+ // app.js is imported directly (not through the mocked barrel) so its real installAUT runs, with
90+ // only its real-device-management.js/simulator-management.js dependencies mocked above.
91+ const { installAUT : installAUTWithRealDeviceMocks } = await importFresh ( APP_PATH ) ;
8292
8393async function withPlatformAsync ( platform : NodeJS . Platform , fn : ( ) => Promise < void > ) : Promise < void > {
8494 const original = Object . getOwnPropertyDescriptor ( process , 'platform' ) ;
0 commit comments