Skip to content

Commit 6ac993e

Browse files
chore: Migrate tests to typescript (#1083)
1 parent 74bc1b3 commit 6ac993e

File tree

12 files changed

+245
-210
lines changed

12 files changed

+245
-210
lines changed

.github/workflows/functional-test.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ jobs:
1313
matrix:
1414
test_targets:
1515
- HOST_OS: 'macos-15'
16-
XCODE_VERSION: '26.0'
17-
IOS_VERSION: '26.0'
16+
XCODE_VERSION: '26.1'
17+
IOS_VERSION: '26.1'
1818
IOS_MODEL: iPhone 17
1919
- HOST_OS: 'macos-15'
2020
XCODE_VERSION: '16.4'

lib/types.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,16 @@ export interface AppleDevice {
9999
[key: string]: any;
100100
}
101101

102+
/**
103+
* Information of the device under test
104+
*/
105+
export interface DeviceInfo {
106+
isRealDevice: boolean;
107+
udid: string;
108+
platformVersion: string;
109+
platformName: string;
110+
}
111+
102112
export interface XcodeBuildArgs {
103113
realDevice: boolean; // Required
104114
agentPath: string; // Required

lib/utils.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -160,12 +160,8 @@ async function setRealDeviceSecurity (keychainPath, keychainPassword) {
160160

161161
/**
162162
* Information of the device under test
163-
* @typedef {Object} DeviceInfo
164-
* @property {boolean} isRealDevice - Equals to true if the current device is a real device
165-
* @property {string} udid - The device UDID.
166-
* @property {string} platformVersion - The platform version of OS.
167-
* @property {string} platformName - The platform name of iOS, tvOS
168-
*/
163+
* @typedef {import('./types').DeviceInfo} DeviceInfo
164+
*/
169165

170166
/**
171167
* Arguments for setting xctestrun file

lib/xcodebuild.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ export class XcodeBuild {
110110
this.noSessionProxy = noSessionProxy;
111111

112112
if (this.useXctestrunFile) {
113-
/** @type {import('./utils').DeviceInfo} */
113+
/** @type {import('./types').DeviceInfo} */
114114
const deviceInfo = {
115115
isRealDevice: !!this.realDevice,
116116
udid: this.device.udid,

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
"lint:fix": "npm run lint -- --fix",
1414
"prepare": "npm run build",
1515
"version": "npm run sync-wda-version",
16-
"test": "mocha --exit --timeout 1m \"./test/unit/**/*-specs.js\"",
17-
"e2e-test": "mocha --exit --timeout 10m \"./test/functional/**/*-specs.js\"",
16+
"test": "mocha --exit --timeout 1m \"./test/unit/**/*-specs.ts\"",
17+
"e2e-test": "mocha --exit --timeout 10m \"./test/functional/**/*-specs.ts\"",
1818
"bundle": "npm run bundle:ios && npm run bundle:tv",
1919
"bundle:ios": "TARGET=runner SDK=sim node ./Scripts/build-webdriveragent.js",
2020
"bundle:tv": "TARGET=tv_runner SDK=tv_sim node ./Scripts/build-webdriveragent.js",

test/functional/desired.js

Lines changed: 0 additions & 5 deletions
This file was deleted.

test/functional/desired.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { util } from '@appium/support';
2+
3+
export const PLATFORM_VERSION: string = process.env.PLATFORM_VERSION
4+
? process.env.PLATFORM_VERSION : '11.3';
5+
export const DEVICE_NAME: string = process.env.DEVICE_NAME
6+
|| (util.compareVersions(PLATFORM_VERSION, '>=', '13.0') ? 'iPhone X' : 'iPhone 6');
7+
Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import { Simctl } from 'node-simctl';
33
import { retryInterval } from 'asyncbox';
44
import { killAllSimulators as simKill } from 'appium-ios-simulator';
55
import { resetTestProcesses } from '../../../lib/utils';
6+
import type { AppleDevice } from '../../../lib/types';
67

7-
8-
async function killAllSimulators () {
8+
export async function killAllSimulators (): Promise<void> {
99
if (process.env.CLOUD) {
1010
return;
1111
}
@@ -24,18 +24,15 @@ async function killAllSimulators () {
2424
await simKill();
2525
}
2626

27-
async function shutdownSimulator (device) {
27+
export async function shutdownSimulator (device: AppleDevice): Promise<void> {
2828
// stop XCTest processes if running to avoid unexpected side effects
2929
await resetTestProcesses(device.udid, true);
3030
await device.shutdown();
3131
}
3232

33-
async function deleteDeviceWithRetry (udid) {
33+
export async function deleteDeviceWithRetry (udid: string): Promise<void> {
3434
const simctl = new Simctl({udid});
3535
try {
3636
await retryInterval(10, 1000, simctl.deleteDevice.bind(simctl));
3737
} catch {}
3838
}
39-
40-
41-
export { killAllSimulators, shutdownSimulator, deleteDeviceWithRetry };

test/functional/webdriveragent-e2e-specs.js renamed to test/functional/webdriveragent-e2e-specs.ts

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,27 @@
1+
import chai, { expect } from 'chai';
2+
import chaiAsPromised from 'chai-as-promised';
13
import { Simctl } from 'node-simctl';
24
import { getVersion } from 'appium-xcode';
5+
import type { XcodeVersion } from 'appium-xcode';
36
import { getSimulator } from 'appium-ios-simulator';
47
import { killAllSimulators, shutdownSimulator } from './helpers/simulator';
58
import { SubProcess } from 'teen_process';
69
import { PLATFORM_VERSION, DEVICE_NAME } from './desired';
710
import { retryInterval } from 'asyncbox';
811
import { WebDriverAgent } from '../../lib/webdriveragent';
912
import axios from 'axios';
13+
import type { AppleDevice } from '../../lib/types';
14+
15+
chai.use(chaiAsPromised);
1016

1117
const MOCHA_TIMEOUT_MS = 60 * 1000 * 5;
1218

1319
const SIM_DEVICE_NAME = 'webDriverAgentTest';
1420
const SIM_STARTUP_TIMEOUT_MS = MOCHA_TIMEOUT_MS;
1521

16-
let testUrl = 'http://localhost:8100/tree';
22+
const testUrl = 'http://localhost:8100/tree';
1723

18-
function getStartOpts (device) {
24+
function getStartOpts (device: AppleDevice) {
1925
return {
2026
device,
2127
platformVersion: PLATFORM_VERSION,
@@ -30,16 +36,9 @@ function getStartOpts (device) {
3036

3137
describe('WebDriverAgent', function () {
3238
this.timeout(MOCHA_TIMEOUT_MS);
33-
let chai;
34-
let xcodeVersion;
39+
let xcodeVersion: XcodeVersion | undefined;
3540

3641
before(async function () {
37-
chai = await import('chai');
38-
const chaiAsPromised = await import('chai-as-promised');
39-
40-
chai.should();
41-
chai.use(chaiAsPromised.default);
42-
4342
// Don't do these tests on Sauce Labs
4443
if (process.env.CLOUD) {
4544
this.skip();
@@ -48,8 +47,8 @@ describe('WebDriverAgent', function () {
4847
xcodeVersion = await getVersion(true);
4948
});
5049
describe('with fresh sim', function () {
51-
let device;
52-
let simctl;
50+
let device: AppleDevice;
51+
let simctl: Simctl;
5352

5453
before(async function () {
5554
simctl = new Simctl();
@@ -67,7 +66,9 @@ describe('WebDriverAgent', function () {
6766
showXcodeLog: true,
6867
device,
6968
});
70-
await wda.xcodebuild.start(true);
69+
if (wda.xcodebuild) {
70+
await wda.xcodebuild.start(true);
71+
}
7172
});
7273

7374
after(async function () {
@@ -96,7 +97,7 @@ describe('WebDriverAgent', function () {
9697
const agent = new WebDriverAgent(xcodeVersion, getStartOpts(device));
9798

9899
await agent.launch('sessionId');
99-
await axios({url: testUrl}).should.be.eventually.rejected;
100+
await expect(axios({url: testUrl})).to.be.rejected;
100101
await agent.quit();
101102
});
102103

@@ -106,8 +107,11 @@ describe('WebDriverAgent', function () {
106107

107108
const agent = new WebDriverAgent(xcodeVersion, getStartOpts(device));
108109

110+
if (!agent.xcodebuild) {
111+
throw new Error('xcodebuild is null');
112+
}
109113
agent.xcodebuild.createSubProcess = async function () {
110-
let args = [
114+
const args = [
111115
'-workspace',
112116
`${this.agentPath}dfgs`,
113117
// '-scheme',
@@ -119,11 +123,12 @@ describe('WebDriverAgent', function () {
119123
return new SubProcess('xcodebuild', args, {detached: true});
120124
};
121125

122-
await agent.launch('sessionId')
123-
.should.eventually.be.rejectedWith('xcodebuild failed');
126+
await expect(agent.launch('sessionId'))
127+
.to.be.rejectedWith('xcodebuild failed');
124128

125129
await agent.quit();
126130
});
127131
});
128132
});
129133
});
134+
Lines changed: 35 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,29 @@
1+
import chai, { expect } from 'chai';
2+
import chaiAsPromised from 'chai-as-promised';
13
import { getXctestrunFilePath, getAdditionalRunContent, getXctestrunFileName } from '../../lib/utils';
24
import { PLATFORM_NAME_IOS, PLATFORM_NAME_TVOS } from '../../lib/constants';
35
import { fs } from '@appium/support';
46
import path from 'path';
57
import { fail } from 'assert';
68
import { arch } from 'os';
79
import sinon from 'sinon';
10+
import type { DeviceInfo } from '../../lib/types';
811

9-
function get_arch() {
12+
chai.use(chaiAsPromised);
13+
14+
function get_arch(): string {
1015
return arch() === 'arm64' ? 'arm64' : 'x86_64';
1116
}
1217

1318
describe('utils', function () {
14-
let chai;
15-
16-
before(async function() {
17-
chai = await import('chai');
18-
const chaiAsPromised = await import('chai-as-promised');
19-
20-
chai.should();
21-
chai.use(chaiAsPromised.default);
22-
});
2319

2420
describe('#getXctestrunFilePath', function () {
2521
const platformVersion = '12.0';
2622
const sdkVersion = '12.2';
2723
const udid = 'xxxxxyyyyyyzzzzzz';
2824
const bootstrapPath = 'path/to/data';
2925
const platformName = PLATFORM_NAME_IOS;
30-
let sandbox;
26+
let sandbox: sinon.SinonSandbox;
3127

3228
beforeEach(function () {
3329
sandbox = sinon.createSandbox();
@@ -42,9 +38,9 @@ describe('utils', function () {
4238
.withArgs(path.resolve(`${bootstrapPath}/${udid}_${sdkVersion}.xctestrun`))
4339
.resolves(true);
4440
sandbox.stub(fs, 'copyFile');
45-
const deviceInfo = {isRealDevice: true, udid, platformVersion, platformName};
46-
await getXctestrunFilePath(deviceInfo, sdkVersion, bootstrapPath)
47-
.should.eventually.equal(path.resolve(`${bootstrapPath}/${udid}_${sdkVersion}.xctestrun`));
41+
const deviceInfo: DeviceInfo = {isRealDevice: true, udid, platformVersion, platformName};
42+
await expect(getXctestrunFilePath(deviceInfo, sdkVersion, bootstrapPath))
43+
.to.eventually.equal(path.resolve(`${bootstrapPath}/${udid}_${sdkVersion}.xctestrun`));
4844
sandbox.assert.notCalled(fs.copyFile);
4945
});
5046

@@ -58,9 +54,9 @@ describe('utils', function () {
5854
path.resolve(`${bootstrapPath}/${udid}_${sdkVersion}.xctestrun`)
5955
)
6056
.resolves();
61-
const deviceInfo = {isRealDevice: true, udid, platformVersion};
62-
await getXctestrunFilePath(deviceInfo, sdkVersion, bootstrapPath)
63-
.should.eventually.equal(path.resolve(`${bootstrapPath}/${udid}_${sdkVersion}.xctestrun`));
57+
const deviceInfo: DeviceInfo = {isRealDevice: true, udid, platformVersion, platformName: PLATFORM_NAME_IOS};
58+
await expect(getXctestrunFilePath(deviceInfo, sdkVersion, bootstrapPath))
59+
.to.eventually.equal(path.resolve(`${bootstrapPath}/${udid}_${sdkVersion}.xctestrun`));
6460
});
6561

6662
it('should return platform based path', async function () {
@@ -69,9 +65,9 @@ describe('utils', function () {
6965
existsStub.withArgs(path.resolve(`${bootstrapPath}/WebDriverAgentRunner_iphonesimulator${sdkVersion}-${get_arch()}.xctestrun`)).resolves(false);
7066
existsStub.withArgs(path.resolve(`${bootstrapPath}/${udid}_${platformVersion}.xctestrun`)).resolves(true);
7167
sandbox.stub(fs, 'copyFile');
72-
const deviceInfo = {isRealDevice: false, udid, platformVersion};
73-
await getXctestrunFilePath(deviceInfo, sdkVersion, bootstrapPath)
74-
.should.eventually.equal(path.resolve(`${bootstrapPath}/${udid}_${platformVersion}.xctestrun`));
68+
const deviceInfo: DeviceInfo = {isRealDevice: false, udid, platformVersion, platformName: PLATFORM_NAME_IOS};
69+
await expect(getXctestrunFilePath(deviceInfo, sdkVersion, bootstrapPath))
70+
.to.eventually.equal(path.resolve(`${bootstrapPath}/${udid}_${platformVersion}.xctestrun`));
7571
sandbox.assert.notCalled(fs.copyFile);
7672
});
7773

@@ -88,38 +84,36 @@ describe('utils', function () {
8884
)
8985
.resolves();
9086

91-
const deviceInfo = {isRealDevice: false, udid, platformVersion};
92-
await getXctestrunFilePath(deviceInfo, sdkVersion, bootstrapPath)
93-
.should.eventually.equal(path.resolve(`${bootstrapPath}/${udid}_${platformVersion}.xctestrun`));
87+
const deviceInfo: DeviceInfo = {isRealDevice: false, udid, platformVersion, platformName: PLATFORM_NAME_IOS};
88+
await expect(getXctestrunFilePath(deviceInfo, sdkVersion, bootstrapPath))
89+
.to.eventually.equal(path.resolve(`${bootstrapPath}/${udid}_${platformVersion}.xctestrun`));
9490
});
9591

9692
it('should raise an exception because of no files', async function () {
9793
const expected = path.resolve(`${bootstrapPath}/WebDriverAgentRunner_iphonesimulator${sdkVersion}-${get_arch()}.xctestrun`);
9894
sandbox.stub(fs, 'exists').resolves(false);
9995

100-
const deviceInfo = {isRealDevice: false, udid, platformVersion};
96+
const deviceInfo: DeviceInfo = {isRealDevice: false, udid, platformVersion, platformName: PLATFORM_NAME_IOS};
10197
try {
10298
await getXctestrunFilePath(deviceInfo, sdkVersion, bootstrapPath);
10399
fail();
104-
} catch (err) {
105-
err.message.should.equal(`If you are using 'useXctestrunFile' capability then you need to have a xctestrun file (expected: '${expected}')`);
100+
} catch (err: any) {
101+
expect(err.message).to.equal(`If you are using 'useXctestrunFile' capability then you need to have a xctestrun file (expected: '${expected}')`);
106102
}
107103
});
108104
});
109105

110106
describe('#getAdditionalRunContent', function () {
111107
it('should return ios format', function () {
112108
const wdaPort = getAdditionalRunContent(PLATFORM_NAME_IOS, 8000);
113-
wdaPort.WebDriverAgentRunner
114-
.EnvironmentVariables.USE_PORT
115-
.should.equal('8000');
109+
expect(wdaPort.WebDriverAgentRunner
110+
.EnvironmentVariables.USE_PORT).to.equal('8000');
116111
});
117112

118113
it('should return tvos format', function () {
119114
const wdaPort = getAdditionalRunContent(PLATFORM_NAME_TVOS, '9000');
120-
wdaPort.WebDriverAgentRunner_tvOS
121-
.EnvironmentVariables.USE_PORT
122-
.should.equal('9000');
115+
expect(wdaPort.WebDriverAgentRunner_tvOS
116+
.EnvironmentVariables.USE_PORT).to.equal('9000');
123117
});
124118
});
125119

@@ -129,34 +123,35 @@ describe('utils', function () {
129123

130124
it('should return ios format, real device', function () {
131125
const platformName = 'iOs';
132-
const deviceInfo = {isRealDevice: true, udid, platformVersion, platformName};
126+
const deviceInfo: DeviceInfo = {isRealDevice: true, udid, platformVersion, platformName};
133127

134-
getXctestrunFileName(deviceInfo, '10.2.0').should.equal(
128+
expect(getXctestrunFileName(deviceInfo, '10.2.0')).to.equal(
135129
'WebDriverAgentRunner_iphoneos10.2.0-arm64.xctestrun');
136130
});
137131

138132
it('should return ios format, simulator', function () {
139133
const platformName = 'ios';
140-
const deviceInfo = {isRealDevice: false, udid, platformVersion, platformName};
134+
const deviceInfo: DeviceInfo = {isRealDevice: false, udid, platformVersion, platformName};
141135

142-
getXctestrunFileName(deviceInfo, '10.2.0').should.equal(
136+
expect(getXctestrunFileName(deviceInfo, '10.2.0')).to.equal(
143137
`WebDriverAgentRunner_iphonesimulator10.2.0-${get_arch()}.xctestrun`);
144138
});
145139

146140
it('should return tvos format, real device', function () {
147141
const platformName = 'tVos';
148-
const deviceInfo = {isRealDevice: true, udid, platformVersion, platformName};
142+
const deviceInfo: DeviceInfo = {isRealDevice: true, udid, platformVersion, platformName};
149143

150-
getXctestrunFileName(deviceInfo, '10.2.0').should.equal(
144+
expect(getXctestrunFileName(deviceInfo, '10.2.0')).to.equal(
151145
'WebDriverAgentRunner_tvOS_appletvos10.2.0-arm64.xctestrun');
152146
});
153147

154148
it('should return tvos format, simulator', function () {
155149
const platformName = 'tvOS';
156-
const deviceInfo = {isRealDevice: false, udid, platformVersion, platformName};
150+
const deviceInfo: DeviceInfo = {isRealDevice: false, udid, platformVersion, platformName};
157151

158-
getXctestrunFileName(deviceInfo, '10.2.0').should.equal(
152+
expect(getXctestrunFileName(deviceInfo, '10.2.0')).to.equal(
159153
`WebDriverAgentRunner_tvOS_appletvsimulator10.2.0-${get_arch()}.xctestrun`);
160154
});
161155
});
162156
});
157+

0 commit comments

Comments
 (0)