Skip to content

Commit 4dd47ec

Browse files
test: Migrate tests to typescript
1 parent 1a291cf commit 4dd47ec

File tree

10 files changed

+110
-129
lines changed

10 files changed

+110
-129
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@
6767
"lint": "eslint .",
6868
"lint:fix": "npm run lint -- --fix",
6969
"prepare": "npm run build",
70-
"test": "mocha --exit --timeout 1m \"./test/unit/**/*-specs.js\"",
71-
"e2e-test": "mocha --exit --timeout 10m \"./test/e2e/**/*-specs.js\""
70+
"test": "mocha --exit --timeout 1m \"./test/unit/**/*-specs.ts\"",
71+
"e2e-test": "mocha --exit --timeout 10m \"./test/e2e/**/*-specs.ts\""
7272
},
7373
"peerDependencies": {
7474
"appium": "^3.0.0-rc.2"
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
import { buildWdIoOptions } from '../helpers';
22
import { remote as wdio } from 'webdriverio';
3+
import type { Browser } from 'webdriverio';
4+
import { expect } from 'chai';
5+
import * as chai from 'chai';
6+
import * as chaiAsPromised from 'chai-as-promised';
7+
8+
chai.use(chaiAsPromised.default);
39

410
describe('context', function () {
5-
let chai;
6-
/** @type {import('webdriverio').Browser} */
7-
let driver;
11+
let driver: Browser | null = null;
812

913
before(async function () {
10-
chai = await import('chai');
11-
const chaiAsPromised = await import('chai-as-promised');
12-
13-
chai.should();
14-
chai.use(chaiAsPromised.default);
15-
1614
driver = await wdio(buildWdIoOptions('Root'));
1715
});
1816

@@ -27,13 +25,13 @@ describe('context', function () {
2725
});
2826

2927
it('should support context api', async function () {
30-
(await driver.getAppiumContext()).should.equal('NATIVE_APP');
31-
(await driver.getAppiumContexts()).should.eql(['NATIVE_APP']);
32-
await driver.switchAppiumContext('NATIVE_APP');
28+
expect(await driver!.getAppiumContext()).to.equal('NATIVE_APP');
29+
expect(await driver!.getAppiumContexts()).to.eql(['NATIVE_APP']);
30+
await driver!.switchAppiumContext('NATIVE_APP');
3331
});
3432

3533
it('should throw an error if invalid context', async function () {
36-
await driver.switchAppiumContext('INVALID_CONTEXT').should.rejected;
34+
await expect(driver!.switchAppiumContext('INVALID_CONTEXT')).to.be.rejected;
3735
});
3836

3937
});

test/e2e/commands/file-movement-e2e-specs.js renamed to test/e2e/commands/file-movement-e2e-specs.ts

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
11
import { remote as wdio } from 'webdriverio';
2+
import type { Browser } from 'webdriverio';
23
import path from 'path';
34
import { tempDir, fs } from 'appium/support';
45
import { isAdmin } from '../../../lib/installer';
56
import { buildWdIoOptions } from '../helpers';
7+
import { expect } from 'chai';
8+
import * as chai from 'chai';
9+
import * as chaiAsPromised from 'chai-as-promised';
610

7-
describe('file movement', function () {
8-
let driver;
9-
let remotePath;
10-
let chai;
11-
12-
before(async function () {
13-
chai = await import('chai');
14-
const chaiAsPromised = await import('chai-as-promised');
11+
chai.use(chaiAsPromised.default);
1512

16-
chai.should();
17-
chai.use(chaiAsPromised.default);
18-
});
13+
describe('file movement', function () {
14+
let driver: Browser | null = null;
15+
let remotePath: string | null = null;
1916

2017
beforeEach(async function () {
2118
if (process.env.CI || !await isAdmin()) {
@@ -46,27 +43,27 @@ describe('file movement', function () {
4643
const base64Data = Buffer.from(stringData).toString('base64');
4744
remotePath = await tempDir.path({ prefix: 'appium', suffix: '.tmp' });
4845

49-
await driver.pushFile(remotePath, base64Data);
46+
await driver!.pushFile(remotePath, base64Data);
5047

5148
// get the file and its contents, to check
52-
const remoteData64 = await driver.pullFile(remotePath);
49+
const remoteData64 = await driver!.pullFile(remotePath);
5350
const remoteData = Buffer.from(remoteData64, 'base64').toString();
54-
remoteData.should.equal(stringData);
51+
expect(remoteData).to.equal(stringData);
5552
});
5653

5754
it('should be able to delete a file', async function () {
5855
const stringData = `random string data ${Math.random()}`;
5956
const base64Data = Buffer.from(stringData).toString('base64');
6057
remotePath = await tempDir.path({ prefix: 'appium', suffix: '.tmp' });
6158

62-
await driver.pushFile(remotePath, base64Data);
59+
await driver!.pushFile(remotePath, base64Data);
6360

64-
const remoteData64 = await driver.pullFile(remotePath);
61+
const remoteData64 = await driver!.pullFile(remotePath);
6562
const remoteData = Buffer.from(remoteData64, 'base64').toString();
66-
remoteData.should.equal(stringData);
63+
expect(remoteData).to.equal(stringData);
6764

68-
await driver.execute('windows: deleteFile', { remotePath });
65+
await driver!.execute('windows: deleteFile', { remotePath });
6966

70-
await driver.pullFile(remotePath).should.eventually.be.rejectedWith(/does not exist/);
67+
await expect(driver!.pullFile(remotePath)).to.eventually.be.rejectedWith(/does not exist/);
7168
});
7269
});
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
import { buildWdIoOptions } from '../helpers';
22
import { remote as wdio } from 'webdriverio';
3+
import type { Browser } from 'webdriverio';
4+
import { expect } from 'chai';
5+
import * as chai from 'chai';
6+
import * as chaiAsPromised from 'chai-as-promised';
7+
8+
chai.use(chaiAsPromised.default);
39

410
describe('log', function () {
5-
let chai;
6-
/** @type {import('webdriverio').Browser} */
7-
let driver;
11+
let driver: Browser | null = null;
812

913
before(async function () {
10-
chai = await import('chai');
11-
const chaiAsPromised = await import('chai-as-promised');
12-
13-
chai.should();
14-
chai.use(chaiAsPromised.default);
15-
1614
driver = await wdio(buildWdIoOptions('Root'));
1715
});
1816

@@ -27,14 +25,14 @@ describe('log', function () {
2725
});
2826

2927
it('should get the list of available logs', async function () {
30-
(await driver.getLogTypes()).should.eql(['server']);
28+
expect(await driver!.getLogTypes()).to.eql(['server']);
3129
});
3230

3331
it('should throw an error when an invalid type is given', async function () {
34-
await driver.getLogs('INVALID_LOG_TYPE').should.be.rejected;
32+
await expect(driver!.getLogs('INVALID_LOG_TYPE')).to.be.rejected;
3533
});
3634

3735
it('should get server logs', async function () {
38-
(await driver.getLogs('server')).should.be.an('array');
36+
expect(await driver!.getLogs('server')).to.be.an('array');
3937
});
4038
});
Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
import { buildWdIoOptions } from '../helpers';
22
import { remote as wdio } from 'webdriverio';
3+
import type { Browser } from 'webdriverio';
4+
import { expect } from 'chai';
5+
import * as chai from 'chai';
6+
import * as chaiAsPromised from 'chai-as-promised';
7+
8+
chai.use(chaiAsPromised.default);
39

410
describe('winapi', function () {
5-
let chai;
6-
/** @type {import('webdriverio').Browser} */
7-
let driver;
11+
let driver: Browser | null = null;
812

913
before(async function () {
10-
chai = await import('chai');
11-
const chaiAsPromised = await import('chai-as-promised');
12-
13-
chai.should();
14-
chai.use(chaiAsPromised.default);
15-
1614
driver = await wdio(buildWdIoOptions('Root'));
1715
});
1816

@@ -28,31 +26,31 @@ describe('winapi', function () {
2826

2927
describe('mouseClick', function () {
3028
it('performs single click with Shift+Ctrl', async function () {
31-
await driver.execute('windows: click', {
29+
await driver!.execute('windows: click', {
3230
x: 100,
3331
y: 100,
3432
modifierKeys: ['shift', 'ctrl'],
3533
});
3634
});
3735

3836
it('performs long click', async function () {
39-
await driver.execute('windows: click', {
37+
await driver!.execute('windows: click', {
4038
x: 100,
4139
y: 100,
4240
durationMs: 500,
4341
});
4442
});
4543

4644
it('performs double click', async function () {
47-
await driver.execute('windows: click', {
45+
await driver!.execute('windows: click', {
4846
x: 100,
4947
y: 100,
5048
times: 2,
5149
});
5250
});
5351

5452
it('performs context click', async function () {
55-
await driver.execute('windows: click', {
53+
await driver!.execute('windows: click', {
5654
x: 100,
5755
y: 100,
5856
button: 'right',
@@ -92,14 +90,14 @@ describe('winapi', function () {
9290
];
9391

9492
for (const errData of errDatas) {
95-
await driver.execute('windows: click', errData).should.be.rejected;
93+
await expect(driver!.execute('windows: click', errData)).to.be.rejected;
9694
}
9795
});
9896
});
9997

10098
describe('mouseScroll', function () {
10199
it('performs vertical scroll gesture with Ctrl+Alt depressed', async function () {
102-
await driver.execute('windows: scroll', {
100+
await driver!.execute('windows: scroll', {
103101
x: 600,
104102
y: 300,
105103
deltaY: 200,
@@ -108,15 +106,15 @@ describe('winapi', function () {
108106
});
109107

110108
it('performs horizontal scroll gesture', async function () {
111-
await driver.execute('windows: scroll', {
109+
await driver!.execute('windows: scroll', {
112110
x: 600,
113111
y: 300,
114112
deltaX: -200,
115113
});
116114
});
117115

118116
it('does nothing if zero delta is provided', async function () {
119-
await driver.execute('windows: scroll', {
117+
await driver!.execute('windows: scroll', {
120118
x: 100,
121119
y: 100,
122120
deltaY: 0,
@@ -144,14 +142,14 @@ describe('winapi', function () {
144142
];
145143

146144
for (const errData of errDatas) {
147-
await driver.execute('windows: scroll', errData).should.be.rejected;
145+
await expect(driver!.execute('windows: scroll', errData)).to.be.rejected;
148146
}
149147
});
150148
});
151149

152150
describe('mouseClickAndDrag', function () {
153151
it('performs drag gesture with Ctrl+Shift depressed', async function () {
154-
await driver.execute('windows: clickAndDrag', {
152+
await driver!.execute('windows: clickAndDrag', {
155153
startX: 600,
156154
startY: 300,
157155
endX: 500,
@@ -163,7 +161,7 @@ describe('winapi', function () {
163161

164162
describe('windowsHover', function () {
165163
it('performs hover gesture with Ctrl+Shift depressed', async function () {
166-
await driver.execute('windows: clickAndDrag', {
164+
await driver!.execute('windows: clickAndDrag', {
167165
startX: 600,
168166
startY: 300,
169167
endX: 500,
@@ -175,7 +173,7 @@ describe('winapi', function () {
175173

176174
describe('keys', function () {
177175
it('performs complex key input', async function () {
178-
await driver.execute('windows: keys', {
176+
await driver!.execute('windows: keys', {
179177
actions: [
180178
{virtualKeyCode: 0x10, down: true},
181179
{pause: 100},
@@ -207,7 +205,7 @@ describe('winapi', function () {
207205
];
208206

209207
for (const errData of errDatas) {
210-
await driver.execute('windows: keys', {actions: [errData]}).should.be.rejected;
208+
await expect(driver!.execute('windows: keys', {actions: [errData]})).to.be.rejected;
211209
}
212210
});
213211
});
Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
import { remote as wdio } from 'webdriverio';
2+
import type { Browser } from 'webdriverio';
23
import { isAdmin } from '../../lib/installer';
34
import { buildWdIoOptions } from './helpers';
5+
import { expect } from 'chai';
6+
import * as chai from 'chai';
7+
import * as chaiAsPromised from 'chai-as-promised';
48

5-
describe('Driver', function () {
6-
let driver;
7-
let chai;
8-
9-
before(async function () {
10-
chai = await import('chai');
11-
const chaiAsPromised = await import('chai-as-promised');
9+
chai.use(chaiAsPromised.default);
1210

13-
chai.should();
14-
chai.use(chaiAsPromised.default);
15-
});
11+
describe('Driver', function () {
12+
let driver: Browser | null = null;
1613

1714
beforeEach(async function () {
1815
if (process.env.CI || !await isAdmin()) {
@@ -33,6 +30,6 @@ describe('Driver', function () {
3330
});
3431

3532
it('should run a basic session using a real client', async function () {
36-
await driver.source().should.eventually.be.not.empty;
33+
await expect((driver as any).source()).to.eventually.be.not.empty;
3734
});
3835
});
Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
export const TEST_PORT = parseInt(process.env.APPIUM_TEST_SERVER_PORT || 4788, 10);
2-
export const TEST_HOST = process.env.APPIUM_TEST_SERVER_HOST || '127.0.0.1';
1+
import type { remote } from 'webdriverio';
32

3+
export const TEST_PORT = parseInt(process.env.APPIUM_TEST_SERVER_PORT || '4788', 10);
4+
export const TEST_HOST = process.env.APPIUM_TEST_SERVER_HOST || '127.0.0.1';
45

56
/**
6-
*
7-
* @param {string} app
8-
* @returns {Record<string, any>}
7+
* Build WebdriverIO options for testing
8+
* @param app - The app identifier
9+
* @returns WebdriverIO options object
910
*/
10-
export function buildWdIoOptions(app) {
11+
export function buildWdIoOptions(app: string): Parameters<typeof remote>[0] {
1112
return {
1213
hostname: TEST_HOST,
1314
port: TEST_PORT,
@@ -19,3 +20,4 @@ export function buildWdIoOptions(app) {
1920
}
2021
};
2122
}
23+

0 commit comments

Comments
 (0)