Skip to content

Commit 80d779d

Browse files
chore: Fix unit tests
Fix unit tests and use sinon to mock the timers. This way the execution of the tests for getting port is now around 50ms instead of 15 seconds
1 parent 640789d commit 80d779d

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

test/services/ios-debugger-port-service.ts

+14-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { IOSLogParserService } from "../../lib/services/ios-log-parser-service";
66
import { IOSSimulatorLogProvider } from "../../lib/common/mobile/ios/simulator/ios-simulator-log-provider";
77
import { Yok } from "../../lib/common/yok";
88
import { EventEmitter } from "events";
9+
import * as sinon from "sinon";
910

1011
class DeviceApplicationManagerMock extends EventEmitter { }
1112

@@ -50,6 +51,7 @@ function createTestInjector() {
5051
projectName: "test",
5152
projectId: appId
5253
});
54+
injector.register("iOSNotification", DeviceApplicationManagerMock);
5355

5456
return injector;
5557
}
@@ -82,11 +84,17 @@ function getMultilineDebuggerPortMessage(port: number) {
8284

8385
describe("iOSDebuggerPortService", () => {
8486
let injector: IInjector, iOSDebuggerPortService: IIOSDebuggerPortService, iosDeviceOperations: IIOSDeviceOperations;
87+
let clock: sinon.SinonFakeTimers = null;
8588

8689
beforeEach(() => {
8790
injector = createTestInjector();
8891
iOSDebuggerPortService = injector.resolve("iOSDebuggerPortService");
8992
iosDeviceOperations = injector.resolve("iosDeviceOperations");
93+
clock = sinon.useFakeTimers();
94+
});
95+
96+
afterEach(() => {
97+
clock.restore();
9098
});
9199

92100
function emitDeviceLog(message: string) {
@@ -142,7 +150,9 @@ describe("iOSDebuggerPortService", () => {
142150
emitDeviceLog(getDebuggerPortMessage(testCase.emittedPort));
143151
}
144152

145-
const port = await iOSDebuggerPortService.getPort({ deviceId: deviceId, appId: appId });
153+
const promise = iOSDebuggerPortService.getPort({ deviceId: deviceId, appId: appId });
154+
clock.tick(10000);
155+
const port = await promise;
146156
assert.deepEqual(port, testCase.emittedPort);
147157
});
148158
it(`${testCase.name} for multiline debugger port message.`, async () => {
@@ -154,7 +164,9 @@ describe("iOSDebuggerPortService", () => {
154164
emitDeviceLog(getMultilineDebuggerPortMessage(testCase.emittedPort));
155165
}
156166

157-
const port = await iOSDebuggerPortService.getPort({ deviceId: deviceId, appId: appId });
167+
const promise = iOSDebuggerPortService.getPort({ deviceId: deviceId, appId: appId });
168+
clock.tick(10000);
169+
const port = await promise;
158170
assert.deepEqual(port, testCase.emittedPort);
159171
});
160172
});

0 commit comments

Comments
 (0)