Skip to content

Commit 17c114c

Browse files
test: Migrate tests to typescript (#277)
1 parent 68636dc commit 17c114c

4 files changed

Lines changed: 138 additions & 131 deletions

File tree

package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@
5252
"lint": "eslint .",
5353
"lint:fix": "npm run lint -- --fix",
5454
"prepare": "npm run build",
55-
"test": "mocha --exit --timeout 1m \"./test/unit/**/*-specs.*js\"",
56-
"e2e-test": "mocha --exit --timeout 5m \"./test/e2e/**/*-specs.js\""
55+
"test": "mocha --exit --timeout 1m \"./test/unit/**/*-specs.ts\"",
56+
"e2e-test": "mocha --exit --timeout 5m \"./test/e2e/**/*-specs.ts\""
5757
},
5858
"prettier": {
5959
"bracketSpacing": false,
@@ -77,7 +77,6 @@
7777
"conventional-changelog-conventionalcommits": "^9.0.0",
7878
"mocha": "^11.0.1",
7979
"prettier": "^3.0.0",
80-
"proxyquire": "^2.1.3",
8180
"semantic-release": "^25.0.2",
8281
"sinon": "^21.0.0",
8382
"ts-node": "^10.9.1",
Lines changed: 53 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -7,35 +7,29 @@ import { uuidV4 } from '../../lib/helpers';
77
import path from 'path';
88
import os from 'os';
99
import fs from 'fs/promises';
10+
import { expect, use } from 'chai';
11+
import chaiAsPromised from 'chai-as-promised';
12+
13+
use(chaiAsPromised);
1014

1115
describe('simctl', function () {
12-
const DEVICE_NAME = process.env.DEVICE_NAME || 'iPhone X';
16+
const DEVICE_NAME = process.env.DEVICE_NAME || 'iPhone 17';
1317
const MOCHA_TIMEOUT = 200000;
1418
this.timeout(MOCHA_TIMEOUT);
1519

16-
let chai;
17-
let chaiAsPromised;
18-
let expect;
19-
let should;
20-
let randName;
21-
let validSdks = [];
22-
let sdk;
23-
let simctl;
20+
let randName: string;
21+
let validSdks: string[] = [];
22+
let sdk: string;
23+
let simctl: Simctl;
2424

2525
before(async function () {
26-
chai = await import('chai');
27-
chaiAsPromised = await import('chai-as-promised');
28-
29-
chai.use(chaiAsPromised.default);
30-
expect = chai.expect;
31-
should = chai.should();
3226

3327
simctl = new Simctl();
3428
const devices = await simctl.getDevices();
3529
console.log(`Found devices: ${JSON.stringify(devices, null, 2)}`); // eslint-disable-line no-console
3630
validSdks = _.keys(devices)
3731
.filter((key) => !_.isEmpty(devices[key]))
38-
.sort((a, b) => a - b);
32+
.sort((a, b) => a.localeCompare(b));
3933
if (!validSdks.length) {
4034
throw new Error('No valid SDKs');
4135
}
@@ -45,11 +39,11 @@ describe('simctl', function () {
4539
// need to find a random name that does not already exist
4640
// give it 5 tries
4741
for (let i = 0; i < 5; i++) {
48-
let randNum = parseInt(Math.random() * 100, 10);
42+
const randNum = parseInt((Math.random() * 100).toString(), 10);
4943
randName = `device${randNum}`;
5044

5145
let nameFound = false;
52-
for (let list of _.values(devices)) {
46+
for (const list of _.values(devices)) {
5347
if (_.includes(_.map(list, 'name'), randName)) {
5448
// need to find another random name
5549
nameFound = true;
@@ -64,47 +58,47 @@ describe('simctl', function () {
6458
const devices = (await simctl.getDevices())[sdk];
6559
const firstDevice = devices[0];
6660
const expectedList = ['name', 'sdk', 'state', 'udid'];
67-
firstDevice.should.have.any.keys(...expectedList);
61+
expect(firstDevice).to.have.any.keys(...expectedList);
6862
});
6963

7064
describe('createDevice', function () {
7165
after(async function () {
7266
if (simctl.udid) {
73-
await simctl.deleteDevice(16000);
67+
await simctl.deleteDevice();
7468
simctl.udid = null;
7569
}
7670
});
7771

7872
it('should create a device', async function () {
7973
simctl.udid = await simctl.createDevice(randName, DEVICE_NAME, sdk);
80-
(typeof simctl.udid).should.equal('string');
81-
simctl.udid.length.should.equal(36);
74+
expect(typeof simctl.udid).to.equal('string');
75+
expect(simctl.udid.length).to.equal(36);
8276
});
8377

8478
it('should create a device and be able to see it in devices list right away', async function () {
8579
const numSimsBefore = (await simctl.getDevices())[sdk].length;
8680
simctl.udid = await simctl.createDevice('node-simctl test', DEVICE_NAME, sdk);
8781
const numSimsAfter = (await simctl.getDevices())[sdk].length;
88-
numSimsAfter.should.equal(numSimsBefore + 1);
82+
expect(numSimsAfter).to.equal(numSimsBefore + 1);
8983
});
9084
});
9185

9286
describe('device manipulation', function () {
93-
let simctl;
87+
let simctl: Simctl;
9488
const name = 'node-simctl test';
9589
beforeEach(async function () {
9690
simctl = new Simctl();
9791
simctl.udid = await simctl.createDevice('node-simctl test', DEVICE_NAME, sdk);
9892
});
9993
afterEach(async function () {
10094
if (simctl.udid) {
101-
await simctl.deleteDevice(simctl.udid, 16000);
95+
await simctl.deleteDevice();
10296
simctl.udid = null;
10397
}
10498
});
10599
it('should get devices', async function () {
106100
const sdkDevices = await simctl.getDevices(sdk);
107-
_.map(sdkDevices, 'name').should.include(name);
101+
expect(_.map(sdkDevices, 'name')).to.include(name);
108102
});
109103

110104
it('should erase devices', async function () {
@@ -114,43 +108,47 @@ describe('simctl', function () {
114108
it('should delete devices', async function () {
115109
await simctl.deleteDevice();
116110
const sdkDevices = await simctl.getDevices(sdk);
117-
_.map(sdkDevices, 'name').should.not.include(simctl.udid);
111+
expect(_.map(sdkDevices, 'name')).to.not.include(simctl.udid);
118112

119113
// so we do not delete again
120114
simctl.udid = null;
121115
});
122116

123117
it('should not fail to shutdown a shutdown simulator', async function () {
124-
await simctl.shutdownDevice().should.eventually.not.be.rejected;
118+
await expect(simctl.shutdownDevice()).to.eventually.not.be.rejected;
125119
});
126120
});
127121

128122
it('should return a nice error for invalid usage', async function () {
129-
let err = null;
123+
let err: Error | null = null;
130124
try {
131125
await simctl.createDevice('foo', 'bar', 'baz');
132126
} catch (e) {
133-
err = e;
127+
err = e as Error;
134128
}
135-
should.exist(err);
136-
err.message.should.include(`Unable to parse version 'baz'`);
129+
expect(err).to.exist;
130+
expect(err!.message).to.include(`Unable to parse version 'baz'`);
137131
});
138132

139133
describe('on running Simulator', function () {
140134
if (process.env.TRAVIS) {
141135
this.retries(3);
142136
}
143137

144-
let major, minor;
138+
let major: number, minor: number;
145139

146140
before(async function () {
147-
({major, minor} = await xcode.getVersion(true));
141+
const version = await xcode.getVersion(true);
142+
if (typeof version === 'string') {
143+
return this.skip();
144+
}
145+
({major, minor} = version);
148146
if (major < 8 || (major === 8 && minor < 1)) {
149147
return this.skip();
150148
}
151149

152150
const sdk = process.env.IOS_SDK || _.last(validSdks);
153-
simctl.udid = await simctl.createDevice('runningSimTest', DEVICE_NAME, sdk);
151+
simctl.udid = await simctl.createDevice('runningSimTest', DEVICE_NAME, sdk!);
154152

155153
await simctl.bootDevice();
156154
await simctl.startBootMonitor({timeout: MOCHA_TIMEOUT});
@@ -170,7 +168,7 @@ describe('simctl', function () {
170168
if (major < 8 || (major === 8 && minor < 1)) {
171169
return this.skip();
172170
}
173-
await simctl.startBootMonitor().should.eventually.be.fulfilled;
171+
await expect(simctl.startBootMonitor()).to.eventually.be.fulfilled;
174172
});
175173
it('should fail to monitor booting of non-existing simulator', async function () {
176174
if (major < 8 || (major === 8 && minor < 1)) {
@@ -179,7 +177,7 @@ describe('simctl', function () {
179177
const udid = simctl.udid;
180178
try {
181179
simctl.udid = 'blabla';
182-
await simctl.startBootMonitor({timeout: 1000}).should.eventually.be.rejected;
180+
await expect(simctl.startBootMonitor({timeout: 1000})).to.eventually.be.rejected;
183181
} finally {
184182
simctl.udid = udid;
185183
}
@@ -207,14 +205,14 @@ describe('simctl', function () {
207205

208206
await retryInterval(pbRetries, 1000, async () => {
209207
await simctl.setPasteboard(pbContent, encoding);
210-
(await simctl.getPasteboard(encoding)).should.eql(pbContent);
208+
expect(await simctl.getPasteboard(encoding)).to.eql(pbContent);
211209
});
212210
});
213211
});
214212

215213
describe('add media', function () {
216214
const BASE64_PNG = 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==';
217-
let picturePath;
215+
let picturePath: string | undefined;
218216
before(async function () {
219217
if (major < 8 || (major === 8 && minor < 1)) {
220218
return this.skip();
@@ -228,47 +226,47 @@ describe('simctl', function () {
228226
}
229227
});
230228
it('should add media files', async function () {
231-
(await simctl.addMedia(picturePath)).code.should.eql(0);
229+
expect((await simctl.addMedia(picturePath!)).code).to.eql(0);
232230
});
233231
});
234232

235233
it('should extract applications information', async function () {
236-
(await simctl.appInfo('com.apple.springboard')).should.include('ApplicationType');
234+
expect(await simctl.appInfo('com.apple.springboard')).to.include('ApplicationType');
237235
});
238236

239237
describe('getEnv', function () {
240238
it('should get env variable value', async function () {
241239
const udid = await simctl.getEnv('SIMULATOR_UDID');
242-
udid.length.should.be.above(0);
240+
expect(udid!.length).to.be.above(0);
243241
});
244242
it('should return null if no var is found', async function () {
245243
const udid = await simctl.getEnv('SIMULATOR_UDD');
246-
_.isNull(udid).should.be.true;
244+
expect(_.isNull(udid)).to.be.true;
247245
});
248246
});
249247

250248
describe('getDeviceTypes', function () {
251249
it('should get device types', async function () {
252250
const deviceTypes = await simctl.getDeviceTypes();
253-
deviceTypes.should.have.length;
254-
deviceTypes.length.should.be.above(0);
251+
expect(deviceTypes).to.have.length;
252+
expect(deviceTypes.length).to.be.above(0);
255253
// at least one type, no matter the version of Xcode, should be an iPhone
256-
deviceTypes.filter((el) => el.includes('iPhone')).length.should.be.above(1);
254+
expect(deviceTypes.filter((el) => el.includes('iPhone')).length).to.be.above(1);
257255
});
258256
});
259257

260258
describe('list', function () {
261259
it('should get everything from xcrun simctl list', async function () {
262260
const fullList = await simctl.list();
263-
fullList.should.have.property('devicetypes');
264-
fullList.should.have.property('runtimes');
265-
fullList.should.have.property('devices');
266-
fullList.should.have.property('pairs');
267-
fullList.devicetypes.length.should.be.above(1);
261+
expect(fullList).to.have.property('devicetypes');
262+
expect(fullList).to.have.property('runtimes');
263+
expect(fullList).to.have.property('devices');
264+
expect(fullList).to.have.property('pairs');
265+
expect(fullList.devicetypes.length).to.be.above(1);
268266
// at least one type, no matter the version of Xcode, should be an iPhone
269-
fullList.devicetypes.filter((el) => el.identifier.includes('iPhone')).length.should.be.above(0);
267+
expect(fullList.devicetypes.filter((el) => el.identifier.includes('iPhone')).length).to.be.above(0);
270268
// at least one runtime should be iOS
271-
fullList.runtimes.filter((el) => el.identifier.includes('iOS')).length.should.be.above(0);
269+
expect(fullList.runtimes.filter((el) => el.identifier.includes('iOS')).length).to.be.above(0);
272270
});
273271
});
274272

@@ -296,8 +294,9 @@ describe('simctl', function () {
296294
}
297295
};
298296

299-
await simctl.pushNotification(payload).should.be.fulfilled;
297+
await expect(simctl.pushNotification(payload)).to.be.fulfilled;
300298
});
301299
});
302300
});
303301
});
302+

0 commit comments

Comments
 (0)