|
| 1 | +import { Platform } from '@expo/eas-build-job'; |
| 2 | + |
| 3 | +import { BuildContext } from '../../context'; |
| 4 | +import * as expoUpdates from '../expoUpdates'; |
| 5 | +import { uploadEmbeddedBundleAsync } from '../expoUpdatesEmbedded'; |
| 6 | +import * as easCli from '../easCli'; |
| 7 | +import * as artifacts from '../artifacts'; |
| 8 | + |
| 9 | +jest.mock('../expoUpdates'); |
| 10 | +jest.mock('../easCli'); |
| 11 | +jest.mock('../artifacts'); |
| 12 | + |
| 13 | +const mockZipEntries = jest.fn(); |
| 14 | +const mockZipExtract = jest.fn(); |
| 15 | +const mockZipClose = jest.fn(); |
| 16 | + |
| 17 | +jest.mock('node-stream-zip', () => ({ |
| 18 | + __esModule: true, |
| 19 | + default: { |
| 20 | + async: jest.fn(() => ({ |
| 21 | + entries: mockZipEntries, |
| 22 | + extract: mockZipExtract, |
| 23 | + close: mockZipClose, |
| 24 | + })), |
| 25 | + }, |
| 26 | +})); |
| 27 | + |
| 28 | +function zipEntryMap(entries: Record<string, true>): Record<string, { name: string }> { |
| 29 | + return Object.fromEntries(Object.keys(entries).map(name => [name, { name }])); |
| 30 | +} |
| 31 | + |
| 32 | +function makeCtx(overrides: { |
| 33 | + platform: Platform; |
| 34 | + simulator?: boolean; |
| 35 | + channel?: string; |
| 36 | + env?: Record<string, string>; |
| 37 | +}): BuildContext<any> { |
| 38 | + const job = |
| 39 | + overrides.platform === Platform.IOS |
| 40 | + ? { |
| 41 | + platform: Platform.IOS, |
| 42 | + simulator: overrides.simulator ?? false, |
| 43 | + updates: overrides.channel ? { channel: overrides.channel } : undefined, |
| 44 | + } |
| 45 | + : { |
| 46 | + platform: Platform.ANDROID, |
| 47 | + updates: overrides.channel ? { channel: overrides.channel } : undefined, |
| 48 | + }; |
| 49 | + |
| 50 | + return { |
| 51 | + job, |
| 52 | + env: overrides.env ?? {}, |
| 53 | + appConfig: Promise.resolve({ |
| 54 | + updates: { url: 'https://u.expo.dev/project-id' }, |
| 55 | + }), |
| 56 | + logger: { |
| 57 | + info: jest.fn(), |
| 58 | + warn: jest.fn(), |
| 59 | + }, |
| 60 | + markBuildPhaseSkipped: jest.fn(), |
| 61 | + markBuildPhaseHasWarnings: jest.fn(), |
| 62 | + getReactNativeProjectDirectory: () => '/project', |
| 63 | + } as any; |
| 64 | +} |
| 65 | + |
| 66 | +describe('uploadEmbeddedBundleAsync', () => { |
| 67 | + beforeEach(() => { |
| 68 | + jest.mocked(expoUpdates.isEASUpdateConfigured).mockResolvedValue(true); |
| 69 | + jest.mocked(easCli.runEasCliCommand).mockResolvedValue({} as any); |
| 70 | + jest.mocked(artifacts.findArtifacts).mockResolvedValue([]); |
| 71 | + mockZipEntries.mockResolvedValue({}); |
| 72 | + mockZipExtract.mockResolvedValue(undefined); |
| 73 | + mockZipClose.mockResolvedValue(undefined); |
| 74 | + }); |
| 75 | + |
| 76 | + afterEach(() => { |
| 77 | + jest.clearAllMocks(); |
| 78 | + jest.restoreAllMocks(); |
| 79 | + }); |
| 80 | + |
| 81 | + it('skips when EAS Update is not configured', async () => { |
| 82 | + jest.mocked(expoUpdates.isEASUpdateConfigured).mockResolvedValue(false); |
| 83 | + const ctx = makeCtx({ platform: Platform.ANDROID, channel: 'production' }); |
| 84 | + |
| 85 | + await uploadEmbeddedBundleAsync(ctx); |
| 86 | + |
| 87 | + expect(ctx.markBuildPhaseSkipped).toHaveBeenCalled(); |
| 88 | + expect(artifacts.findArtifacts).not.toHaveBeenCalled(); |
| 89 | + }); |
| 90 | + |
| 91 | + it('warns when no channel is configured and does not look for the archive', async () => { |
| 92 | + const ctx = makeCtx({ platform: Platform.ANDROID }); |
| 93 | + |
| 94 | + await uploadEmbeddedBundleAsync(ctx); |
| 95 | + |
| 96 | + expect(ctx.logger.warn).toHaveBeenCalledWith( |
| 97 | + 'Skipping embedded bundle upload: no channel configured for this build profile.' |
| 98 | + ); |
| 99 | + expect(ctx.markBuildPhaseHasWarnings).toHaveBeenCalled(); |
| 100 | + expect(artifacts.findArtifacts).not.toHaveBeenCalled(); |
| 101 | + }); |
| 102 | + |
| 103 | + it('throws for an unsupported platform', async () => { |
| 104 | + const ctx = makeCtx({ platform: Platform.ANDROID, channel: 'production' }); |
| 105 | + (ctx.job as { platform: string }).platform = 'web'; |
| 106 | + |
| 107 | + await expect(uploadEmbeddedBundleAsync(ctx)).rejects.toThrow( |
| 108 | + 'Uploading embedded updates is not supported for the web platform.' |
| 109 | + ); |
| 110 | + expect(artifacts.findArtifacts).not.toHaveBeenCalled(); |
| 111 | + }); |
| 112 | + |
| 113 | + it('uploads from Android APK archives', async () => { |
| 114 | + jest.mocked(artifacts.findArtifacts).mockResolvedValue(['/tmp/app-release.apk']); |
| 115 | + mockZipEntries.mockResolvedValue( |
| 116 | + zipEntryMap({ |
| 117 | + 'assets/index.android.bundle': true, |
| 118 | + 'assets/app.manifest': true, |
| 119 | + }) |
| 120 | + ); |
| 121 | + const ctx = makeCtx({ |
| 122 | + platform: Platform.ANDROID, |
| 123 | + channel: 'production', |
| 124 | + env: { EAS_BUILD_ID: 'build-123' }, |
| 125 | + }); |
| 126 | + |
| 127 | + await uploadEmbeddedBundleAsync(ctx); |
| 128 | + |
| 129 | + expect(mockZipExtract).toHaveBeenCalledWith( |
| 130 | + 'assets/index.android.bundle', |
| 131 | + expect.stringContaining('index.android.bundle') |
| 132 | + ); |
| 133 | + expect(easCli.runEasCliCommand).toHaveBeenCalledWith( |
| 134 | + expect.objectContaining({ |
| 135 | + args: expect.arrayContaining([ |
| 136 | + 'update:embedded:upload', |
| 137 | + '--platform', |
| 138 | + Platform.ANDROID, |
| 139 | + '--channel', |
| 140 | + 'production', |
| 141 | + '--build-id', |
| 142 | + 'build-123', |
| 143 | + ]), |
| 144 | + }) |
| 145 | + ); |
| 146 | + }); |
| 147 | + |
| 148 | + it('uploads from Android AAB archives', async () => { |
| 149 | + jest.mocked(artifacts.findArtifacts).mockResolvedValue(['/tmp/app-release.aab']); |
| 150 | + mockZipEntries.mockResolvedValue( |
| 151 | + zipEntryMap({ |
| 152 | + 'base/assets/index.android.bundle': true, |
| 153 | + 'base/assets/app.manifest': true, |
| 154 | + }) |
| 155 | + ); |
| 156 | + const ctx = makeCtx({ platform: Platform.ANDROID, channel: 'production' }); |
| 157 | + |
| 158 | + await uploadEmbeddedBundleAsync(ctx); |
| 159 | + |
| 160 | + expect(mockZipExtract).toHaveBeenCalledWith( |
| 161 | + 'base/assets/index.android.bundle', |
| 162 | + expect.stringContaining('index.android.bundle') |
| 163 | + ); |
| 164 | + expect(easCli.runEasCliCommand).toHaveBeenCalled(); |
| 165 | + }); |
| 166 | + |
| 167 | + it('uploads from iOS IPA archives', async () => { |
| 168 | + jest.mocked(artifacts.findArtifacts).mockResolvedValue(['/tmp/App.ipa']); |
| 169 | + mockZipEntries.mockResolvedValue( |
| 170 | + zipEntryMap({ |
| 171 | + 'Payload/App.app/main.jsbundle': true, |
| 172 | + 'Payload/App.app/EXUpdates.bundle/app.manifest': true, |
| 173 | + }) |
| 174 | + ); |
| 175 | + const ctx = makeCtx({ platform: Platform.IOS, channel: 'production' }); |
| 176 | + |
| 177 | + await uploadEmbeddedBundleAsync(ctx); |
| 178 | + |
| 179 | + expect(easCli.runEasCliCommand).toHaveBeenCalledWith( |
| 180 | + expect.objectContaining({ |
| 181 | + args: expect.arrayContaining(['--platform', Platform.IOS]), |
| 182 | + }) |
| 183 | + ); |
| 184 | + }); |
| 185 | + |
| 186 | + it('skips simulator builds', async () => { |
| 187 | + const ctx = makeCtx({ platform: Platform.IOS, simulator: true, channel: 'preview' }); |
| 188 | + |
| 189 | + await uploadEmbeddedBundleAsync(ctx); |
| 190 | + |
| 191 | + expect(ctx.markBuildPhaseSkipped).toHaveBeenCalled(); |
| 192 | + expect(artifacts.findArtifacts).not.toHaveBeenCalled(); |
| 193 | + }); |
| 194 | + |
| 195 | + it('warns when bundle or manifest is missing from the archive', async () => { |
| 196 | + jest.mocked(artifacts.findArtifacts).mockResolvedValue(['/tmp/app-release.apk']); |
| 197 | + mockZipEntries.mockResolvedValue( |
| 198 | + zipEntryMap({ |
| 199 | + 'assets/app.manifest': true, |
| 200 | + }) |
| 201 | + ); |
| 202 | + const ctx = makeCtx({ platform: Platform.ANDROID, channel: 'production' }); |
| 203 | + |
| 204 | + await uploadEmbeddedBundleAsync(ctx); |
| 205 | + |
| 206 | + expect(ctx.logger.warn).toHaveBeenCalledWith( |
| 207 | + 'Skipping embedded bundle upload: bundle or manifest not found in archive.' |
| 208 | + ); |
| 209 | + expect(easCli.runEasCliCommand).not.toHaveBeenCalled(); |
| 210 | + }); |
| 211 | + |
| 212 | + it('warns when build archive is not found', async () => { |
| 213 | + jest.mocked(artifacts.findArtifacts).mockResolvedValue([]); |
| 214 | + const ctx = makeCtx({ platform: Platform.ANDROID, channel: 'production' }); |
| 215 | + |
| 216 | + await uploadEmbeddedBundleAsync(ctx); |
| 217 | + |
| 218 | + expect(ctx.logger.warn).toHaveBeenCalledWith( |
| 219 | + 'Skipping embedded bundle upload: build archive not found.' |
| 220 | + ); |
| 221 | + expect(ctx.markBuildPhaseHasWarnings).toHaveBeenCalled(); |
| 222 | + expect(easCli.runEasCliCommand).not.toHaveBeenCalled(); |
| 223 | + }); |
| 224 | + |
| 225 | + it('treats findArtifacts errors as no archive found', async () => { |
| 226 | + jest.mocked(artifacts.findArtifacts).mockRejectedValue(new Error('glob failed')); |
| 227 | + const ctx = makeCtx({ platform: Platform.ANDROID, channel: 'production' }); |
| 228 | + |
| 229 | + await uploadEmbeddedBundleAsync(ctx); |
| 230 | + |
| 231 | + expect(ctx.logger.warn).toHaveBeenCalledWith( |
| 232 | + 'Skipping embedded bundle upload: build archive not found.' |
| 233 | + ); |
| 234 | + expect(ctx.markBuildPhaseHasWarnings).toHaveBeenCalled(); |
| 235 | + expect(easCli.runEasCliCommand).not.toHaveBeenCalled(); |
| 236 | + }); |
| 237 | + |
| 238 | + it('warns and continues when CLI upload throws', async () => { |
| 239 | + jest.mocked(artifacts.findArtifacts).mockResolvedValue(['/tmp/app-release.apk']); |
| 240 | + mockZipEntries.mockResolvedValue( |
| 241 | + zipEntryMap({ |
| 242 | + 'assets/index.android.bundle': true, |
| 243 | + 'assets/app.manifest': true, |
| 244 | + }) |
| 245 | + ); |
| 246 | + jest.mocked(easCli.runEasCliCommand).mockRejectedValue(new Error('upload failed')); |
| 247 | + const ctx = makeCtx({ platform: Platform.ANDROID, channel: 'production' }); |
| 248 | + |
| 249 | + await uploadEmbeddedBundleAsync(ctx); |
| 250 | + |
| 251 | + expect(ctx.logger.warn).toHaveBeenCalledWith( |
| 252 | + expect.objectContaining({ err: expect.any(Error) }), |
| 253 | + 'Failed to upload embedded bundle.' |
| 254 | + ); |
| 255 | + expect(ctx.markBuildPhaseHasWarnings).toHaveBeenCalled(); |
| 256 | + }); |
| 257 | + |
| 258 | + it('swallows zip.close() failures so they do not mask the upload result', async () => { |
| 259 | + jest.mocked(artifacts.findArtifacts).mockResolvedValue(['/tmp/app-release.apk']); |
| 260 | + mockZipEntries.mockResolvedValue( |
| 261 | + zipEntryMap({ |
| 262 | + 'assets/index.android.bundle': true, |
| 263 | + 'assets/app.manifest': true, |
| 264 | + }) |
| 265 | + ); |
| 266 | + mockZipClose.mockRejectedValue(new Error('close failed')); |
| 267 | + const ctx = makeCtx({ platform: Platform.ANDROID, channel: 'production' }); |
| 268 | + |
| 269 | + await expect(uploadEmbeddedBundleAsync(ctx)).resolves.toBeUndefined(); |
| 270 | + expect(easCli.runEasCliCommand).toHaveBeenCalled(); |
| 271 | + expect(mockZipClose).toHaveBeenCalled(); |
| 272 | + }); |
| 273 | +}); |
0 commit comments