Repository: wix/Detox
Pull request #2775 (2021-05-11 11:34:37)

Apply some touch-ups following #2768
## Description

In this pull request, I have introduced some touch-ups as a follow-up to #2768.

<!--
Step 3: Please review the checklist below.
-->

---


> _For features/enhancements:_
 - [ ] I have added/updated the relevant references in the [documentation](https://github.com/wix/Detox/tree/master/docs) files.

> _For API changes:_
 - [ ] I have made the necessary changes in the [types index](https://github.com/wix/Detox/blob/master/detox/index.d.ts) file.



Patch:
diff --git a/detox/src/devices/drivers/android/genycloud/GenyCloudDriver.js b/detox/src/devices/drivers/android/genycloud/GenyCloudDriver.js
index 9f3c3c17e..f2f4554ae 100644
--- a/detox/src/devices/drivers/android/genycloud/GenyCloudDriver.js
+++ b/detox/src/devices/drivers/android/genycloud/GenyCloudDriver.js
@@ -32,10 +32,12 @@ class GenyCloudDriver extends AndroidDriver {
 
     const recipeService = new RecipesService(this._exec, logger);
     const instanceLookupService = new InstanceLookupService(this._exec, instanceNaming, deviceRegistry);
-    this._instanceLifecycleService = new InstanceLifecycleService(this._exec, instanceNaming);
-    this._instanceLauncher = new InstanceLauncher(this._instanceLifecycleService, deviceCleanupRegistry, this.emitter);
+    const instanceLifecycleService = new InstanceLifecycleService(this._exec, instanceNaming);
+    const instanceLauncher = new InstanceLauncher(this._instanceLifecycleService, deviceCleanupRegistry, this.emitter);
     this._recipeQuerying = new RecipeQuerying(recipeService);
-    this._instanceAllocation = new GenyCloudInstanceAllocation(deviceRegistry, instanceLookupService, this._instanceLifecycleService, this._instanceLauncher, this.emitter);
+    this._instanceAllocation = new GenyCloudInstanceAllocation({ deviceRegistry, instanceLookupService, instanceLifecycleService, instanceLauncher, eventEmitter: this.emitter });
+    this._instanceLifecycleService = instanceLifecycleService;
+    this._instanceLauncher = instanceLauncher;
 
     this._authService = new AuthService(this._exec);
   }
diff --git a/detox/src/devices/drivers/android/genycloud/helpers/GenyCloudInstanceAllocation.js b/detox/src/devices/drivers/android/genycloud/helpers/GenyCloudInstanceAllocation.js
index 077e66da8..49e25ee51 100644
--- a/detox/src/devices/drivers/android/genycloud/helpers/GenyCloudInstanceAllocation.js
+++ b/detox/src/devices/drivers/android/genycloud/helpers/GenyCloudInstanceAllocation.js
@@ -6,7 +6,7 @@ const logger = require('../../../../../utils/logger').child({ __filename });
 const { ALLOCATE_DEVICE_LOG_EVT } = AndroidDeviceAllocation;
 
 class GenyCloudInstanceAllocation extends AndroidDeviceAllocation {
-  constructor(deviceRegistry, instanceLookupService, instanceLifecycleService, instanceLauncher, eventEmitter) {
+  constructor({ deviceRegistry, instanceLookupService, instanceLifecycleService, instanceLauncher, eventEmitter }) {
     super(deviceRegistry, eventEmitter, logger);
 
     this._instanceLookupService = instanceLookupService;


Test patch:
diff --git a/detox/src/devices/drivers/android/AndroidDriver.test.js b/detox/src/devices/drivers/android/AndroidDriver.test.js
index bf27259d0..a46c3ca34 100644
--- a/detox/src/devices/drivers/android/AndroidDriver.test.js
+++ b/detox/src/devices/drivers/android/AndroidDriver.test.js
@@ -239,11 +239,7 @@ describe('Android driver', () => {
 
       it('should not send the data if device prep fails', async () => {
         fileXferObj().prepareDestinationDir.mockRejectedValue(new Error())
-        try {
-          await uut.launchApp(deviceId, bundleId, notificationArgs, '');
-          fail('Expected an error');
-        } catch (e) {
-        }
+        await expect(uut.launchApp(deviceId, bundleId, notificationArgs, '')).rejects.toThrowError();
       });
 
       it('should launch instrumentation with a modified notification data URL arg', async () => {
@@ -332,12 +328,10 @@ describe('Android driver', () => {
       setTimeout(() => waitForCrashReject(crashError), 1);
 
       try {
-        await promise;
-        fail('Expected an error and none was thrown');
-      } catch (e) {
-        expect(e).toEqual(crashError);
+        await expect(promise).rejects.toThrowError(crashError);
       } finally {
         clientWaitResolve();
+
       }
     }, 2000);
 
@@ -401,12 +395,9 @@ describe('Android driver', () => {
 
       fs.existsSync.mockReturnValue(false);
 
-      try {
-        await uut.installApp(deviceId, binaryPath, undefined);
-        fail('Expected an error');
-      } catch (err) {
-        expect(err.message).toContain(`'${expectedTestBinPath}'`);
-      }
+      await expect(uut.installApp(deviceId, binaryPath, undefined))
+        .rejects
+        .toThrowError(`'${expectedTestBinPath}'`);
     });
   });
 
diff --git a/detox/src/devices/drivers/android/emulator/helpers/EmulatorDeviceAllocation.test.js b/detox/src/devices/drivers/android/emulator/helpers/EmulatorDeviceAllocation.test.js
index 58bb1d815..c34d44c66 100644
--- a/detox/src/devices/drivers/android/emulator/helpers/EmulatorDeviceAllocation.test.js
+++ b/detox/src/devices/drivers/android/emulator/helpers/EmulatorDeviceAllocation.test.js
@@ -48,6 +48,7 @@ describe('Android emulator device allocation', () => {
 
   const givenFreeDevice = (adbName) => freeDeviceFinder.findFreeDevice.mockResolvedValue(adbName);
   const givenNoFreeDevices = () => freeDeviceFinder.findFreeDevice.mockResolvedValue(null);
+  const givenEmulatorLaunchError = () => emulatorLauncher.launch.mockRejectedValue(new Error());
   const givenRandomFuncResult = (result) => randomFunc.mockReturnValue(result);
   const givenDeviceBootCompleted = () => adb.isBootComplete.mockResolvedValue(true);
   const givenDeviceBootIncomplete = () => adb.isBootComplete.mockResolvedValue(false);
@@ -110,14 +111,9 @@ describe('Android emulator device allocation', () => {
 
     it('should fail if emulator launch fails', async () => {
       givenNoFreeDevices();
-      emulatorLauncher.launch.mockRejectedValue(new Error());
+      givenEmulatorLaunchError();
 
-      try {
-        await uut.allocateDevice(avdName);
-      } catch (e) {
-        return;
-      }
-      throw new Error('Expected an error');
+      await expect(uut.allocateDevice(avdName)).rejects.toThrowError();
     });
 
     it('should randomize a custom port for a newly launched emulator, in the 10000-20000 range', async () => {
@@ -253,13 +249,7 @@ describe('Android emulator device allocation', () => {
 
     it('should fail if registry fails', async () => {
       deviceRegistry.disposeDevice.mockRejectedValue(new Error());
-
-      try {
-        await uut.deallocateDevice(adbName);
-      } catch (e) {
-        return;
-      }
-      throw new Error('Expected an error');
+      await expect(uut.deallocateDevice(adbName)).rejects.toThrowError();
     });
   });
 });
diff --git a/detox/src/devices/drivers/android/emulator/helpers/EmulatorLauncher.test.js b/detox/src/devices/drivers/android/emulator/helpers/EmulatorLauncher.test.js
index f186bc954..637af4597 100644
--- a/detox/src/devices/drivers/android/emulator/helpers/EmulatorLauncher.test.js
+++ b/detox/src/devices/drivers/android/emulator/helpers/EmulatorLauncher.test.js
@@ -57,14 +57,9 @@ describe('Emulator launcher', () => {
     it('should not emit shutdownDevice prematurely', async () => {
       emulatorTelnet.kill.mockRejectedValue(new Error());
 
-      try {
-        await uut.shutdown(adbName);
-      } catch (e) {
-        expect(eventEmitter.emit).toHaveBeenCalledTimes(1);
-        expect(eventEmitter.emit).not.toHaveBeenCalledWith('shutdownDevice', expect.any(Object));
-        return;
-      }
-      throw new Error('Expected an error');
+      await expect(uut.shutdown(adbName)).rejects.toThrowError();
+      expect(eventEmitter.emit).toHaveBeenCalledTimes(1);
+      expect(eventEmitter.emit).not.toHaveBeenCalledWith('shutdownDevice', expect.any(Object));
     });
 
     it('should resort to a default telnet generator func', async () => {
diff --git a/detox/src/devices/drivers/android/genycloud/GenyCloudDriver.test.js b/detox/src/devices/drivers/android/genycloud/GenyCloudDriver.test.js
index 5dbc51d38..dc616942b 100644
--- a/detox/src/devices/drivers/android/genycloud/GenyCloudDriver.test.js
+++ b/detox/src/devices/drivers/android/genycloud/GenyCloudDriver.test.js
@@ -131,12 +131,13 @@ describe('Genymotion-cloud driver', () => {
 
         try {
           await uut.prepare();
-          fail('Expected an error');
         } catch (e) {
           expect(e.constructor.name).toEqual('DetoxRuntimeError');
           expect(e.toString()).toContain(`Your Genymotion-Cloud executable (found in ${MOCK_GMSAAS_PATH}) is too old! (version 1.5.9)`);
           expect(e.toString()).toContain(`HINT: Detox requires version 1.6.0, or newer. To use 'android.genycloud' type devices, you must upgrade it, first.`);
+          return;
         }
+        throw new Error('Expected an error');
       });
 
       it('should accept the gmsaas exec if version is sufficiently new', async () => {
@@ -155,12 +156,9 @@ describe('Genymotion-cloud driver', () => {
         givenProperGmsaasLogin();
         givenGmsaasExecVersion('0.6.0');
 
-        try {
-          await uut.prepare();
-          fail('Expected an error');
-        } catch (e) {
-          expect(e.toString()).toContain(`Your Genymotion-Cloud executable (found in ${MOCK_GMSAAS_PATH}) is too old! (version 0.6.0)`);
-        }
+        await expect(uut.prepare())
+          .rejects
+          .toThrowError(`Your Genymotion-Cloud executable (found in ${MOCK_GMSAAS_PATH}) is too old! (version 0.6.0)`);
       });
 
       it('should throw an error if not logged-in to gmsaas', async () => {
@@ -169,12 +167,13 @@ describe('Genymotion-cloud driver', () => {
 
         try {
           await uut.prepare();
-          fail('Expected an error');
         } catch (e) {
           expect(e.constructor.name).toEqual('DetoxRuntimeError');
           expect(e.toString()).toContain(`Cannot run tests using 'android.genycloud' type devices, because Genymotion was not logged-in to!`);
           expect(e.toString()).toContain(`HINT: Log-in to Genymotion-cloud by running this command (and following instructions):\n${MOCK_GMSAAS_PATH} auth login --help`);
+          return;
         }
+        throw new Error('Expected an error');
       });
 
       it('should not throw an error if properly logged in to gmsaas', async () => {
@@ -214,13 +213,14 @@ describe('Genymotion-cloud driver', () => {
 
         try {
           await uut.acquireFreeDevice(deviceQuery);
-          fail('Expected an error');
         } catch (e) {
           expect(e.toString()).toContain('No Genymotion-Cloud template found to match the configured lookup query');
           expect(e.toString()).toContain(JSON.stringify(deviceQuery));
           expect(e.toString()).toContain('HINT: Revisit your detox configuration');
           expect(e.toString()).toContain('https://cloud.geny.io/app/shared-devices');
+          return;
         }
+        throw new Error('Expected an error');
       });
 
       it('should return a descriptive device name in post-allocation state', async () => {
diff --git a/detox/src/devices/drivers/android/genycloud/exec/GenyCloudExec.test.js b/detox/src/devices/drivers/android/genycloud/exec/GenyCloudExec.test.js
index 021a87004..e589f5a30 100644
--- a/detox/src/devices/drivers/android/genycloud/exec/GenyCloudExec.test.js
+++ b/detox/src/devices/drivers/android/genycloud/exec/GenyCloudExec.test.js
@@ -114,12 +114,7 @@ describe('Genymotion-cloud executable', () => {
       it('should fail upon an error result', async () => {
         givenErrorResult();
 
-        try {
-          await testCase.commandExecFn();
-          fail('Expected an error');
-        } catch (e) {
-          expect(e.message).toEqual(JSON.stringify(failResponse));
-        }
+        await expect(testCase.commandExecFn()).rejects.toThrowError(JSON.stringify(failResponse));
       });
     });
   });
diff --git a/detox/src/devices/drivers/android/genycloud/helpers/GenyCloudInstanceAllocation.test.js b/detox/src/devices/drivers/android/genycloud/helpers/GenyCloudInstanceAllocation.test.js
index 89c2f3021..9351b2169 100644
--- a/detox/src/devices/drivers/android/genycloud/helpers/GenyCloudInstanceAllocation.test.js
+++ b/detox/src/devices/drivers/android/genycloud/helpers/GenyCloudInstanceAllocation.test.js
@@ -38,7 +38,7 @@ describe('Genymotion-Cloud instance allocation', () => {
     GenyInstance = jest.genMockFromModule('../services/dto/GenyInstance');
 
     const InstanceAllocation = require('./GenyCloudInstanceAllocation');
-    uut = new InstanceAllocation(deviceRegistry, instanceLookupService, instanceLifecycleService, instanceLauncher, eventEmitter);
+    uut = new InstanceAllocation({ deviceRegistry, instanceLookupService, instanceLifecycleService, instanceLauncher, eventEmitter });
   });
 
   const aRecipe = () => ({
@@ -172,12 +172,9 @@ describe('Genymotion-Cloud instance allocation', () => {
       givenCreatedInstance(instance);
       givenRefreshedInstance(instance);
 
-      try {
-        await uut.allocateDevice(aRecipe());
-        fail('Expected an error');
-      } catch (e) {
-        expect(e.toString()).toContain(`Timeout waiting for instance ${instance.uuid} to be ready`);
-      }
+      await expect(uut.allocateDevice(aRecipe()))
+        .rejects
+        .toThrowError(`Timeout waiting for instance ${instance.uuid} to be ready`);
     });
 
     it('should not wait for created instance to become online if already online', async () => {
@@ -281,10 +278,7 @@ describe('Genymotion-Cloud instance allocation', () => {
 
       eventEmitter.emit.mockRejectedValue(new Error());
 
-      try {
-        await uut.allocateDevice(aRecipe);
-        fail('Expected an error');
-      } catch(e) {}
+      await expect(uut.allocateDevice(aRecipe())).rejects.toThrowError();
     });
 
     it('should emit boot-device event *after* post-allocate log', async () => {
@@ -293,12 +287,8 @@ describe('Genymotion-Cloud instance allocation', () => {
 
       eventEmitter.emit.mockRejectedValue(new Error());
 
-      try {
-        await uut.allocateDevice(aRecipe());
-        fail('Expected an error');
-      } catch(e) {
-        expect(eventEmitter.emit).toHaveBeenCalledTimes(1);
-      }
+      await expect(uut.allocateDevice(aRecipe())).rejects.toThrowError();
+      expect(eventEmitter.emit).toHaveBeenCalledTimes(1);
     });
   });
 
@@ -313,12 +303,7 @@ describe('Genymotion-Cloud instance allocation', () => {
       const instance = anOnlineInstance();
       deviceRegistry.disposeDevice.mockRejectedValue(new Error());
 
-      try {
-        await uut.deallocateDevice(instance.uuid);
-      } catch (e) {
-        return;
-      }
-      throw new Error('Expected an error');
+      await expect(uut.deallocateDevice(instance.uuid)).rejects.toThrowError();
     });
   });
 });
diff --git a/detox/src/devices/drivers/android/genycloud/helpers/GenyCloudInstanceLauncher.test.js b/detox/src/devices/drivers/android/genycloud/helpers/GenyCloudInstanceLauncher.test.js
index d5b4a8ff4..816f7d1d7 100644
--- a/detox/src/devices/drivers/android/genycloud/helpers/GenyCloudInstanceLauncher.test.js
+++ b/detox/src/devices/drivers/android/genycloud/helpers/GenyCloudInstanceLauncher.test.js
@@ -51,11 +51,8 @@ describe('Genymotion-Cloud instance launcher', () => {
     it('should fail if deletion fails', async () => {
       givenAnInstanceDeletionError();
 
-      try {
-        const instance = anInstance();
-        await uut.shutdown(instance);
-        fail('Expected an error');
-      } catch (e) {}
+      const instance = anInstance();
+      await expect(uut.shutdown(instance)).rejects.toThrowError();
     });
 
     it('should remove the instance from the cleanup registry', async () => {
@@ -77,12 +74,8 @@ describe('Genymotion-Cloud instance launcher', () => {
     it('should not emit shutdownDevice prematurely', async () => {
       givenAnInstanceDeletionError();
 
-      try {
-        const instance = anInstance();
-        await uut.shutdown(instance);
-        fail('Expected an error');
-      } catch (e) {}
-
+      const instance = anInstance();
+      await expect(uut.shutdown(instance)).rejects.toThrowError();
       expect(eventEmitter.emit).toHaveBeenCalledTimes(1);
       expect(eventEmitter.emit).not.toHaveBeenCalledWith('shutdownDevice', expect.any(Object));
     });
diff --git a/detox/src/devices/drivers/android/tools/AppUninstallHelper.test.js b/detox/src/devices/drivers/android/tools/AppUninstallHelper.test.js
index 78aaafafe..af7973126 100644
--- a/detox/src/devices/drivers/android/tools/AppUninstallHelper.test.js
+++ b/detox/src/devices/drivers/android/tools/AppUninstallHelper.test.js
@@ -23,10 +23,7 @@ describe('Android app uninstall helper', () => {
 
   it('should fail if app uninstall fails', async () => {
     adb.uninstall.mockRejectedValue(new Error('mocked error in adb.uninstall'));
-    try {
-      await uut.uninstall(deviceId, bundleId);
-      fail('expected an error');
-    } catch (err) {}
+    await expect(uut.uninstall(deviceId, bundleId)).rejects.toThrowError();
   });
 
   it('should avoid uninstalling app if not already installed', async () => {
@@ -48,10 +45,7 @@ describe('Android app uninstall helper', () => {
       .mockResolvedValueOnce(true)
       .mockRejectedValueOnce(new Error('mocked error in adb.uninstall'));
 
-    try {
-      await uut.uninstall(deviceId, bundleId);
-      fail('expected an error');
-    } catch (err) {}
+    await expect(uut.uninstall(deviceId, bundleId)).rejects.toThrowError();
   });
 
   it('should avoid uninstalling test binary if not already installed', async () => {
diff --git a/detox/src/devices/drivers/android/tools/EmulatorHandle.test.js b/detox/src/devices/drivers/android/tools/EmulatorHandle.test.js
index a2346a774..7e96aefc7 100644
--- a/detox/src/devices/drivers/android/tools/EmulatorHandle.test.js
+++ b/detox/src/devices/drivers/android/tools/EmulatorHandle.test.js
@@ -36,10 +36,7 @@ describe('Emulator handle', () => {
 
     it('should fail if telnet-connect fails', async () => {
       telnet.connect.mockRejectedValue(new Error('mock telnet error'));
-      try {
-        await uut.queryName();
-        fail('Expected an error');
-      } catch (e) {}
+      await expect(uut.queryName()).rejects.toThrowError();
     });
 
     it('should return name from telnet command', async () => {
@@ -57,10 +54,8 @@ describe('Emulator handle', () => {
 
     it('should close telnet even if name resolution fails', async () => {
       telnet.avdName.mockRejectedValue(new Error('mock name resolution error'));
-      try {
-        await uut.queryName();
-        fail('Expected an error');
-      } catch (e) {}
+
+      await expect(uut.queryName()).rejects.toThrowError();
       expect(telnet.quit).toHaveBeenCalled();
     });
 
diff --git a/detox/src/utils/trace.test.js b/detox/src/utils/trace.test.js
index 5913ed569..56c6e7491 100644
--- a/detox/src/utils/trace.test.js
+++ b/detox/src/utils/trace.test.js
@@ -116,18 +116,13 @@ describe('Trace util', () => {
       const error = new Error('error mock');
       const functionCall = () => Promise.reject(error);
 
-      try {
-        trace.init();
-        await traceCall(sectionName, functionCall);
-        fail('Expected an error');
-      } catch (e) {
-        expect(e).toEqual(error);
-        expect(trace.events).toEqual([
-          expect.any(Object),
-          expect.objectContaining(startEventTraits),
-          expect.objectContaining(aFailEndEventTraits(error)),
-        ]);
-      }
+      trace.init();
+      await expect(traceCall(sectionName, functionCall)).rejects.toThrowError(error);
+      expect(trace.events).toEqual([
+        expect.any(Object),
+        expect.objectContaining(startEventTraits),
+        expect.objectContaining(aFailEndEventTraits(error)),
+      ]);
     });
   });
 });