Skip to content

Commit 6c12ea9

Browse files
test: Migrate tests to typescript (#1030)
1 parent c4d17fa commit 6c12ea9

23 files changed

+944
-1055
lines changed

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
"prepare": "npm run rebuild",
4141
"rebuild": "npm run clean; npm run build",
4242
"format": "prettier -w ./lib",
43-
"test": "mocha --exit --timeout 1m \"./test/unit/**/*-specs.js\""
43+
"test": "mocha --exit --timeout 1m \"./test/unit/**/*-specs.ts\""
4444
},
4545
"prettier": {
4646
"bracketSpacing": false,
@@ -82,7 +82,6 @@
8282
"@types/source-map-support": "^0.5.6",
8383
"@types/ws": "^8.5.4",
8484
"@xmldom/xmldom": "^0.x",
85-
"android-apidemos": "^5.0.0",
8685
"chai": "^6.0.0",
8786
"chai-as-promised": "^8.0.0",
8887
"conventional-changelog-conventionalcommits": "^9.0.0",

test/unit/commands/app-management-specs.js renamed to test/unit/commands/app-management-specs.ts

Lines changed: 95 additions & 95 deletions
Large diffs are not rendered by default.
Lines changed: 114 additions & 137 deletions
Large diffs are not rendered by default.
Lines changed: 86 additions & 95 deletions
Large diffs are not rendered by default.

test/unit/commands/emulator-actions-specs.js

Lines changed: 0 additions & 40 deletions
This file was deleted.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import sinon from 'sinon';
2+
import {AndroidDriver} from '../../../lib/driver';
3+
import { expect, use } from 'chai';
4+
import chaiAsPromised from 'chai-as-promised';
5+
6+
use(chaiAsPromised);
7+
8+
let driver: AndroidDriver;
9+
const sandbox = sinon.createSandbox();
10+
11+
describe('Emulator Actions', function () {
12+
beforeEach(function () {
13+
driver = new AndroidDriver();
14+
});
15+
afterEach(function () {
16+
sandbox.restore();
17+
});
18+
describe('sensorSet', function () {
19+
it('should call sensorSet', async function () {
20+
const sensorSetStub = sandbox.stub(driver, 'sensorSet');
21+
await driver.execute('mobile:sensorSet', [{sensorType: 'light', value: 0}]);
22+
expect(sensorSetStub.calledWith('light', 0)).to.be.true;
23+
});
24+
it('should be reject if arguments are missing', async function () {
25+
await expect(
26+
driver.execute('mobile: sensorSet', [{sensor: 'light', value: 0}])
27+
).to.eventually.be.rejectedWith(/sensorType/);
28+
await expect(
29+
driver.execute('mobile: sensorSet', [{sensorType: 'light', val: 0}])
30+
).to.eventually.be.rejectedWith(/value/);
31+
});
32+
});
33+
});
34+
Lines changed: 51 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,15 @@ import sinon from 'sinon';
22
import {AndroidDriver} from '../../../lib/driver';
33
import * as support from '@appium/support';
44
import {ADB} from 'appium-adb';
5+
import { expect, use } from 'chai';
6+
import chaiAsPromised from 'chai-as-promised';
57

6-
/** @type {AndroidDriver} */
7-
let driver;
8-
let sandbox = sinon.createSandbox();
8+
use(chaiAsPromised);
99

10-
describe('File Actions', function () {
11-
let chai;
12-
13-
before(async function () {
14-
chai = await import('chai');
15-
const chaiAsPromised = await import('chai-as-promised');
16-
17-
chai.should();
18-
chai.use(chaiAsPromised.default);
19-
});
10+
let driver: AndroidDriver;
11+
const sandbox = sinon.createSandbox();
2012

13+
describe('File Actions', function () {
2114
beforeEach(function () {
2215
driver = new AndroidDriver();
2316
driver.adb = new ADB();
@@ -28,104 +21,105 @@ describe('File Actions', function () {
2821

2922
describe('pullFile', function () {
3023
it('should be able to pull file from device', async function () {
31-
let localFile = 'local/tmp_file';
24+
const localFile = 'local/tmp_file';
3225
sandbox.stub(support.tempDir, 'path').returns(localFile);
33-
sandbox.stub(driver.adb, 'pull');
26+
const pullStub1 = sandbox.stub(driver.adb, 'pull');
3427
sandbox
3528
.stub(support.util, 'toInMemoryBase64')
3629
.withArgs(localFile)
3730
.returns(Buffer.from('YXBwaXVt', 'utf8'));
3831
sandbox.stub(support.fs, 'exists').withArgs(localFile).returns(true);
39-
sandbox.stub(support.fs, 'unlink');
40-
await driver.pullFile('remote_path').should.become('YXBwaXVt');
41-
driver.adb.pull.calledWithExactly('remote_path', localFile).should.be.true;
42-
support.fs.unlink.calledWithExactly(localFile).should.be.true;
32+
const unlinkStub4 = sandbox.stub(support.fs, 'unlink');
33+
await expect(driver.pullFile('remote_path')).to.become('YXBwaXVt');
34+
expect(pullStub1.calledWithExactly('remote_path', localFile)).to.be.true;
35+
expect(unlinkStub4.calledWithExactly(localFile)).to.be.true;
4336
});
4437

4538
it('should be able to pull file located in application container from the device', async function () {
46-
let localFile = 'local/tmp_file';
39+
const localFile = 'local/tmp_file';
4740
const packageId = 'com.myapp';
4841
const remotePath = 'path/in/container';
4942
const tmpPath = '/data/local/tmp/container';
5043
sandbox.stub(support.tempDir, 'path').returns(localFile);
51-
sandbox.stub(driver.adb, 'pull');
52-
sandbox.stub(driver.adb, 'shell');
44+
const pullStub = sandbox.stub(driver.adb, 'pull');
45+
const shellStub2 = sandbox.stub(driver.adb, 'shell');
5346
sandbox
5447
.stub(support.util, 'toInMemoryBase64')
5548
.withArgs(localFile)
5649
.returns(Buffer.from('YXBwaXVt', 'utf8'));
5750
sandbox.stub(support.fs, 'exists').withArgs(localFile).returns(true);
58-
sandbox.stub(support.fs, 'unlink');
59-
await driver.pullFile(`@${packageId}/${remotePath}`).should.become('YXBwaXVt');
60-
driver.adb.pull.calledWithExactly(tmpPath, localFile).should.be.true;
61-
driver.adb.shell.calledWithExactly([
51+
const unlinkStub3 = sandbox.stub(support.fs, 'unlink');
52+
await expect(driver.pullFile(`@${packageId}/${remotePath}`)).to.become('YXBwaXVt');
53+
expect(pullStub.calledWithExactly(tmpPath, localFile)).to.be.true;
54+
expect(shellStub2.calledWithExactly([
6255
'run-as',
6356
packageId,
6457
`chmod 777 '/data/data/${packageId}/${remotePath}'`,
65-
]).should.be.true;
66-
driver.adb.shell.calledWithExactly([
58+
])).to.be.true;
59+
expect(shellStub2.calledWithExactly([
6760
'run-as',
6861
packageId,
6962
`cp -f '/data/data/${packageId}/${remotePath}' '${tmpPath}'`,
70-
]).should.be.true;
71-
support.fs.unlink.calledWithExactly(localFile).should.be.true;
72-
driver.adb.shell.calledWithExactly(['rm', '-f', tmpPath]).should.be.true;
63+
])).to.be.true;
64+
expect(unlinkStub3.calledWithExactly(localFile)).to.be.true;
65+
expect(shellStub2.calledWithExactly(['rm', '-f', tmpPath])).to.be.true;
7366
});
7467
});
7568

7669
describe('pushFile', function () {
7770
it('should be able to push file to device', async function () {
78-
let localFile = 'local/tmp_file';
79-
let content = 'appium';
71+
const localFile = 'local/tmp_file';
72+
const content = 'appium';
8073
sandbox.stub(support.tempDir, 'path').returns(localFile);
81-
sandbox.stub(driver.adb, 'push');
74+
const pushStub1 = sandbox.stub(driver.adb, 'push');
8275
sandbox.stub(driver.adb, 'shell');
83-
sandbox.stub(support.fs, 'writeFile');
76+
const writeFileStub1 = sandbox.stub(support.fs, 'writeFile');
8477
sandbox.stub(support.fs, 'exists').withArgs(localFile).returns(true);
85-
sandbox.stub(support.fs, 'unlink');
78+
const unlinkStub1 = sandbox.stub(support.fs, 'unlink');
8679
await driver.pushFile('remote_path', 'YXBwaXVt');
87-
support.fs.writeFile.calledWithExactly(localFile, content, 'binary').should.be.true;
88-
support.fs.unlink.calledWithExactly(localFile).should.be.true;
89-
driver.adb.push.calledWithExactly(localFile, 'remote_path').should.be.true;
80+
expect(writeFileStub1.calledWithExactly(localFile, content, 'binary')).to.be.true;
81+
expect(unlinkStub1.calledWithExactly(localFile)).to.be.true;
82+
expect(pushStub1.calledWithExactly(localFile, 'remote_path')).to.be.true;
9083
});
9184

9285
it('should be able to push file located in application container to the device', async function () {
93-
let localFile = 'local/tmp_file';
94-
let content = 'appium';
86+
const localFile = 'local/tmp_file';
87+
const content = 'appium';
9588
const packageId = 'com.myapp';
9689
const remotePath = 'path/in/container';
9790
const tmpPath = '/data/local/tmp/container';
9891
sandbox.stub(support.tempDir, 'path').returns(localFile);
99-
sandbox.stub(driver.adb, 'push');
100-
sandbox.stub(driver.adb, 'shell');
101-
sandbox.stub(support.fs, 'writeFile');
92+
const pushStub2 = sandbox.stub(driver.adb, 'push');
93+
const writeFileStub = sandbox.stub(support.fs, 'writeFile');
10294
sandbox.stub(support.fs, 'exists').withArgs(localFile).returns(true);
103-
sandbox.stub(support.fs, 'unlink');
95+
const unlinkStub2 = sandbox.stub(support.fs, 'unlink');
96+
const shellStub = sandbox.stub(driver.adb, 'shell');
10497
await driver.pushFile(`@${packageId}/${remotePath}`, 'YXBwaXVt');
105-
support.fs.writeFile.calledWithExactly(localFile, content, 'binary').should.be.true;
106-
driver.adb.push.calledWithExactly(localFile, tmpPath).should.be.true;
107-
driver.adb.shell.calledWithExactly([
98+
expect(writeFileStub.calledWithExactly(localFile, content, 'binary')).to.be.true;
99+
expect(pushStub2.calledWithExactly(localFile, tmpPath)).to.be.true;
100+
expect(shellStub.calledWithExactly([
108101
'run-as',
109102
packageId,
110103
`mkdir -p '/data/data/${packageId}/path/in'`,
111-
]).should.be.true;
112-
driver.adb.shell.calledWithExactly([
104+
])).to.be.true;
105+
expect(shellStub.calledWithExactly([
113106
'run-as',
114107
packageId,
115108
`touch '/data/data/${packageId}/${remotePath}'`,
116-
]).should.be.true;
117-
driver.adb.shell.calledWithExactly([
109+
])).to.be.true;
110+
expect(shellStub.calledWithExactly([
118111
'run-as',
119112
packageId,
120113
`chmod 777 '/data/data/${packageId}/${remotePath}'`,
121-
]).should.be.true;
122-
driver.adb.shell.calledWithExactly([
114+
])).to.be.true;
115+
expect(shellStub.calledWithExactly([
123116
'run-as',
124117
packageId,
125118
`cp -f '${tmpPath}' '/data/data/${packageId}/${remotePath}'`,
126-
]).should.be.true;
127-
support.fs.unlink.calledWithExactly(localFile).should.be.true;
128-
driver.adb.shell.calledWithExactly(['rm', '-f', tmpPath]).should.be.true;
119+
])).to.be.true;
120+
expect(unlinkStub2.calledWithExactly(localFile)).to.be.true;
121+
expect(shellStub.calledWithExactly(['rm', '-f', tmpPath])).to.be.true;
129122
});
130123
});
131124
});
125+
Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,14 @@ import sinon from 'sinon';
22
import {ADB} from 'appium-adb';
33
import { AndroidDriver } from '../../../lib/driver';
44
import { setMockLocationApp } from '../../../lib/commands/geolocation';
5+
import { use } from 'chai';
6+
import chaiAsPromised from 'chai-as-promised';
57

6-
describe('Geolocation', function () {
7-
let driver;
8-
let sandbox = sinon.createSandbox();
9-
let chai;
10-
11-
before(async function () {
12-
chai = await import('chai');
13-
const chaiAsPromised = await import('chai-as-promised');
8+
use(chaiAsPromised);
149

15-
chai.should();
16-
chai.use(chaiAsPromised.default);
17-
});
10+
describe('Geolocation', function () {
11+
let driver: AndroidDriver;
12+
const sandbox = sinon.createSandbox();
1813

1914
beforeEach(function () {
2015
const adb = new ADB();
@@ -35,5 +30,5 @@ describe('Geolocation', function () {
3530
await setMockLocationApp.bind(driver)('io.appium.settings');
3631
});
3732
});
38-
3933
});
34+
Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,14 @@ import sinon from 'sinon';
22
import {AndroidDriver} from '../../../lib/driver';
33
import {ADB} from 'appium-adb';
44
import {errors} from 'appium/driver';
5+
import { expect, use } from 'chai';
6+
import chaiAsPromised from 'chai-as-promised';
57

6-
describe('IME', function () {
7-
/** @type {AndroidDriver} */
8-
let driver;
9-
let sandbox = sinon.createSandbox();
10-
let chai;
11-
12-
before(async function () {
13-
chai = await import('chai');
14-
const chaiAsPromised = await import('chai-as-promised');
8+
use(chaiAsPromised);
159

16-
chai.should();
17-
chai.use(chaiAsPromised.default);
18-
});
10+
describe('IME', function () {
11+
let driver: AndroidDriver;
12+
const sandbox = sinon.createSandbox();
1913

2014
beforeEach(function () {
2115
driver = new AndroidDriver();
@@ -26,41 +20,42 @@ describe('IME', function () {
2620
});
2721
describe('isIMEActivated', function () {
2822
it('should allways return true', async function () {
29-
await driver.isIMEActivated().should.eventually.be.true;
23+
await await expect(driver.isIMEActivated()).to.eventually.be.true;
3024
});
3125
});
3226
describe('availableIMEEngines', function () {
3327
it('should return available IMEEngines', async function () {
3428
sandbox.stub(driver.adb, 'availableIMEs').returns(['IME1', 'IME2']);
35-
await driver.availableIMEEngines().should.eventually.be.deep.equal(['IME1', 'IME2']);
29+
await await expect(driver.availableIMEEngines()).to.eventually.be.deep.equal(['IME1', 'IME2']);
3630
});
3731
});
3832
describe('getActiveIMEEngine', function () {
3933
it('should return active IME engine', async function () {
4034
sandbox.stub(driver.adb, 'defaultIME').returns('default_ime_engine');
41-
await driver.getActiveIMEEngine().should.become('default_ime_engine');
35+
await expect(driver.getActiveIMEEngine()).to.become('default_ime_engine');
4236
});
4337
});
4438
describe('activateIMEEngine', function () {
4539
it('should activate IME engine', async function () {
4640
sandbox.stub(driver.adb, 'availableIMEs').returns(['IME1', 'IME2']);
47-
sandbox.stub(driver.adb, 'enableIME');
48-
sandbox.stub(driver.adb, 'setIME');
49-
await driver.activateIMEEngine('IME2').should.be.fulfilled;
50-
driver.adb.enableIME.calledWithExactly('IME2').should.be.true;
51-
driver.adb.setIME.calledWithExactly('IME2').should.be.true;
41+
const enableIMEStub = sandbox.stub(driver.adb, 'enableIME');
42+
const setIMEStub = sandbox.stub(driver.adb, 'setIME');
43+
await expect(driver.activateIMEEngine('IME2')).to.be.fulfilled;
44+
expect(enableIMEStub.calledWithExactly('IME2')).to.be.true;
45+
expect(setIMEStub.calledWithExactly('IME2')).to.be.true;
5246
});
5347
it('should throws error if IME not found', async function () {
5448
sandbox.stub(driver.adb, 'availableIMEs').returns(['IME1', 'IME2']);
55-
await driver.activateIMEEngine('IME3').should.be.rejectedWith(errors.IMENotAvailableError);
49+
await expect(driver.activateIMEEngine('IME3')).to.be.rejectedWith(errors.IMENotAvailableError);
5650
});
5751
});
5852
describe('deactivateIMEEngine', function () {
5953
it('should deactivate IME engine', async function () {
6054
sandbox.stub(driver, 'getActiveIMEEngine').returns('active_ime_engine');
61-
sandbox.stub(driver.adb, 'disableIME');
62-
await driver.deactivateIMEEngine().should.be.fulfilled;
63-
driver.adb.disableIME.calledWithExactly('active_ime_engine');
55+
const disableIMEStub = sandbox.stub(driver.adb, 'disableIME');
56+
await expect(driver.deactivateIMEEngine()).to.be.fulfilled;
57+
expect(disableIMEStub.calledWithExactly('active_ime_engine')).to.be.true;
6458
});
6559
});
6660
});
61+

0 commit comments

Comments
 (0)