Skip to content

Commit 238de0e

Browse files
chore: Drop esmock
1 parent 1cb63dc commit 238de0e

3 files changed

Lines changed: 68 additions & 56 deletions

File tree

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
"install-docs-deps": "appium-docs init --no-mkdocs",
5757
"prepare": "npm run rebuild",
5858
"rebuild": "npm run clean; npm run build",
59-
"test": "node --enable-source-maps --test --test-force-exit --test-timeout=60000 \"./build/test/unit/**/*.spec.js\"",
59+
"test": "node --enable-source-maps --experimental-test-module-mocks --test --test-force-exit --test-timeout=60000 \"./build/test/unit/**/*.spec.js\"",
6060
"e2e-test": "node --enable-source-maps --test --test-force-exit --test-concurrency=1 \"./build/test/functional/**/*.spec.js\"",
6161
"e2e-test:basic": "node --enable-source-maps --test --test-force-exit --test-concurrency=1 \"./build/test/functional/basic/**/*.spec.js\"",
6262
"e2e-test:device": "node --enable-source-maps --test --test-force-exit --test-concurrency=1 \"./build/test/functional/device/**/*.spec.js\"",
@@ -107,7 +107,6 @@
107107
"@types/sinon": "22.0.0",
108108
"chai": "^6.0.0",
109109
"chai-as-promised": "^8.0.0",
110-
"esmock": "^2.7.6",
111110
"pem": "^1.14.8",
112111
"sharp": "^0.x",
113112
"sinon": "^22.0.0",

test/unit/device/remote-xpc-facade.spec.ts

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,28 @@
1-
import {describe, it, afterEach} from 'node:test';
1+
import {describe, it, afterEach, mock} from 'node:test';
22

33
import {expect} from 'chai';
4-
import esmock from 'esmock';
54
import sinon from 'sinon';
65

6+
import * as moduleLoaderModule from '../../../lib/device/remote-xpc/module-loader.js';
7+
import * as usbmuxUtilsModule from '../../../lib/device/remote-xpc/usbmux-utils.js';
8+
79
let currentTryLoadRemoteXPCModule: (...args: any[]) => any = async () => null;
810
let currentIsDeviceListedInUsbmux: (...args: any[]) => any = async () => false;
911

10-
const {RemoteXPCFacade} = await esmock(
11-
'../../../lib/device/remote-xpc/index.js',
12-
import.meta.url,
13-
{},
14-
{
15-
'../../../lib/device/remote-xpc/module-loader.js': {
16-
tryLoadRemoteXPCModule: (...args: any[]) => currentTryLoadRemoteXPCModule(...args),
17-
},
18-
'../../../lib/device/remote-xpc/usbmux-utils.js': {
19-
isDeviceListedInUsbmux: (...args: any[]) => currentIsDeviceListedInUsbmux(...args),
20-
},
12+
mock.module('../../../lib/device/remote-xpc/module-loader.js', {
13+
namedExports: {
14+
...moduleLoaderModule,
15+
tryLoadRemoteXPCModule: (...args: any[]) => currentTryLoadRemoteXPCModule(...args),
16+
},
17+
});
18+
mock.module('../../../lib/device/remote-xpc/usbmux-utils.js', {
19+
namedExports: {
20+
...usbmuxUtilsModule,
21+
isDeviceListedInUsbmux: (...args: any[]) => currentIsDeviceListedInUsbmux(...args),
2122
},
22-
);
23+
});
24+
25+
const {RemoteXPCFacade} = await import('../../../lib/device/remote-xpc/index.js');
2326

2427
describe('RemoteXPCFacade', function () {
2528
afterEach(function () {

test/unit/driver.spec.ts

Lines changed: 50 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,37 @@
11
import net from 'node:net';
2-
import {describe, it, beforeEach, afterEach} from 'node:test';
2+
import {describe, it, beforeEach, afterEach, mock} from 'node:test';
33

44
import xcode from 'appium-xcode';
55
import {JWProxy} from 'appium/driver.js';
66
import {use, expect} from 'chai';
77
import chaiAsPromised from 'chai-as-promised';
8-
import esmock from 'esmock';
98
import {createSandbox, type SinonSandbox, type SinonStubbedMember} from 'sinon';
109

10+
import * as helpersIndexModule from '../../lib/commands/helpers/index.js';
1111
import {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';
1215
import type {XCUITestDriverOpts} from '../../lib/driver.js';
1316
import {mergeDeep} from '../../lib/utils/index.js';
1417
import {UNIT_LONG_TIMEOUT_MS} from './helpers.js';
1518

1619
use(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+
1835
const defaultCheckAppPresent = async () => {};
1936
const defaultAssertWdaHostSessionCapsSupported = () => {};
2037
const defaultAssertWdaHostPlatformSupported = () => {};
@@ -39,46 +56,39 @@ let currentInstallToRealDevice: (...args: any[]) => any = defaultInstallToRealDe
3956
let currentInstallToSimulator: (...args: any[]) => any = defaultInstallToSimulator;
4057
let 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

8393
async function withPlatformAsync(platform: NodeJS.Platform, fn: () => Promise<void>): Promise<void> {
8494
const original = Object.getOwnPropertyDescriptor(process, 'platform');

0 commit comments

Comments
 (0)