Skip to content

Commit

Permalink
fix: Error when no simulators are available (#1470)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
dpogue authored Aug 20, 2024
1 parent 1aea8e7 commit 4f8992a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
7 changes: 4 additions & 3 deletions lib/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
15 changes: 15 additions & 0 deletions tests/spec/unit/build.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down

0 comments on commit 4f8992a

Please sign in to comment.