diff --git a/packages/cli-platform-ios/src/commands/runIOS/__tests__/findMatchingSimulator.test.ts b/packages/cli-platform-ios/src/commands/runIOS/__tests__/findMatchingSimulator.test.ts index 93979f556..89a7d38d9 100644 --- a/packages/cli-platform-ios/src/commands/runIOS/__tests__/findMatchingSimulator.test.ts +++ b/packages/cli-platform-ios/src/commands/runIOS/__tests__/findMatchingSimulator.test.ts @@ -1020,4 +1020,48 @@ describe('findMatchingSimulator', () => { version: 'iOS 16.0', }); }); + + it('should return picked simulator instead of last booted simulator in list (multi ios versions)', () => { + expect( + findMatchingSimulator( + { + devices: { + 'com.apple.CoreSimulator.SimRuntime.iOS-16-0': [ + { + udid: 'E1C0E452-2671-4EB5-B875-58E3DDC6EE81', + isAvailable: false, + state: 'Shutdown', + name: 'iPhone SE (3rd generation)', + }, + { + lastBootedAt: '2022-09-21T11:38:28Z', + udid: '3AA90A75-D9C3-41A6-8DE1-43BE74A0C32B', + isAvailable: true, + state: 'Shutdown', + name: 'iPhone 14', + }, + { + udid: '6F2FA108-AC7D-4D3C-BD13-56C5E7FCEDFE', + isAvailable: true, + state: 'Shutdown', + name: 'iPhone 14 Plus', + }, + { + udid: 'D87B6D9E-F5B0-486F-BBE3-6EEC5A6D0C22', + isAvailable: false, + state: 'Shutdown', + name: 'iPhone 14 Pro', + }, + ], + }, + }, + {simulator: 'iPhone 14 Plus'}, + ), + ).toEqual({ + udid: '6F2FA108-AC7D-4D3C-BD13-56C5E7FCEDFE', + name: 'iPhone 14 Plus', + booted: false, + version: 'iOS 16.0', + }); + }); }); diff --git a/packages/cli-platform-ios/src/commands/runIOS/findMatchingSimulator.ts b/packages/cli-platform-ios/src/commands/runIOS/findMatchingSimulator.ts index 96a03dd34..6041bf8ff 100644 --- a/packages/cli-platform-ios/src/commands/runIOS/findMatchingSimulator.ts +++ b/packages/cli-platform-ios/src/commands/runIOS/findMatchingSimulator.ts @@ -51,6 +51,8 @@ function findMatchingSimulator( } let match; + let fallbackMatch; + for (const versionDescriptor in devices) { const device = devices[versionDescriptor]; let version = versionDescriptor; @@ -102,7 +104,7 @@ function findMatchingSimulator( } // If no match found, use first available simulator that was booted before if (!!lastBootedAt && !match) { - match = simulatorDescriptor; + fallbackMatch = simulatorDescriptor; } // Keeps track of the first available simulator for use if we can't find one above. if (simulatorName === null && !match) { @@ -112,7 +114,7 @@ function findMatchingSimulator( } } - return match ?? null; + return match ?? fallbackMatch ?? null; } export default findMatchingSimulator;