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', () => {