From 4f8992a4ad9b3f4c0e9ea99d0edd1b8d736cdadc Mon Sep 17 00:00:00 2001 From: Darryl Pogue Date: Mon, 19 Aug 2024 19:07:43 -0700 Subject: [PATCH] fix: Error when no simulators are available (#1470) This should at least return a useful and actionable error message to the end user, rather than "Cannot read property name of undefined". Closes GH-526. Closes GH-1366. Closes GH-1377. Closes GH-1469. --- lib/build.js | 7 ++++--- tests/spec/unit/build.spec.js | 15 +++++++++++++++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/lib/build.js b/lib/build.js index cfb6a8947..73d57fa7f 100644 --- a/lib/build.js +++ b/lib/build.js @@ -85,10 +85,11 @@ function getDefaultSimulatorTarget () { events.emit('log', 'Select last emulator from list as default.'); return require('./listEmulatorBuildTargets').run() .then(emulators => { - let targetEmulator; - if (emulators.length > 0) { - targetEmulator = emulators[0]; + if (emulators.length === 0) { + return Promise.reject(new CordovaError('Could not find any iOS simulators. Use Xcode to install simulator devices for testing.')); } + + let targetEmulator = emulators[0]; emulators.forEach(emulator => { if (emulator.name.indexOf('iPhone') === 0) { targetEmulator = emulator; diff --git a/tests/spec/unit/build.spec.js b/tests/spec/unit/build.spec.js index 6e0b1da42..ad7282fad 100644 --- a/tests/spec/unit/build.spec.js +++ b/tests/spec/unit/build.spec.js @@ -418,6 +418,21 @@ describe('build', () => { }); }); }); + + it('should handle the case of no simulators being available', () => { + // This method will require a module that supports the run method. + build.__set__('require', () => ({ + run: () => Promise.resolve([]) + })); + + const getDefaultSimulatorTarget = build.__get__('getDefaultSimulatorTarget'); + + return getDefaultSimulatorTarget().then(sim => { + return Promise.reject(new Error('Should not resolve if no simulators are present')); + }, (err) => { + expect(err).toBeInstanceOf(CordovaError); + }); + }); }); describe('findXCodeProjectIn method', () => {