|
| 1 | +import { BuildContext, Builders } from '@expo/build-tools'; |
| 2 | +import { Platform } from '@expo/eas-build-job'; |
| 3 | + |
| 4 | +import { build, warnOnUnknownEnvironment } from '../build'; |
| 5 | +import { Analytics } from '../external/analytics'; |
| 6 | + |
| 7 | +jest.mock('@expo/build-tools', () => { |
| 8 | + const actual = jest.requireActual('@expo/build-tools'); |
| 9 | + return { |
| 10 | + ...actual, |
| 11 | + Builders: { |
| 12 | + androidBuilder: jest.fn(async () => ({})), |
| 13 | + iosBuilder: jest.fn(async () => ({})), |
| 14 | + }, |
| 15 | + }; |
| 16 | +}); |
| 17 | +jest.mock('../displayRuntimeInfo', () => ({ displayWorkerRuntimeInfo: jest.fn() })); |
| 18 | +jest.mock('../workingdir', () => ({ cleanUpWorkingdir: jest.fn() })); |
| 19 | +jest.mock('../external/analytics', () => ({ |
| 20 | + ...jest.requireActual('../external/analytics'), |
| 21 | + logProjectDependenciesAsync: jest.fn(), |
| 22 | +})); |
| 23 | + |
| 24 | +function createEnvironmentWarningCtx(environment?: string) { |
| 25 | + return { |
| 26 | + metadata: environment === undefined ? undefined : { environment }, |
| 27 | + markBuildPhaseHasWarnings: jest.fn(), |
| 28 | + logger: { warn: jest.fn() }, |
| 29 | + }; |
| 30 | +} |
| 31 | + |
| 32 | +describe(warnOnUnknownEnvironment.name, () => { |
| 33 | + it('warns and marks the phase for an unknown environment', () => { |
| 34 | + const ctx = createEnvironmentWarningCtx('staging'); |
| 35 | + warnOnUnknownEnvironment(ctx); |
| 36 | + expect(ctx.markBuildPhaseHasWarnings).toHaveBeenCalled(); |
| 37 | + expect(ctx.logger.warn).toHaveBeenCalledWith( |
| 38 | + expect.stringContaining('Unknown environment "staging"') |
| 39 | + ); |
| 40 | + }); |
| 41 | + |
| 42 | + it.each(['development', 'preview', 'production'])('does not warn for %s', environment => { |
| 43 | + const ctx = createEnvironmentWarningCtx(environment); |
| 44 | + warnOnUnknownEnvironment(ctx); |
| 45 | + expect(ctx.markBuildPhaseHasWarnings).not.toHaveBeenCalled(); |
| 46 | + expect(ctx.logger.warn).not.toHaveBeenCalled(); |
| 47 | + }); |
| 48 | + |
| 49 | + it('does not warn when no environment is set', () => { |
| 50 | + const ctx = createEnvironmentWarningCtx(undefined); |
| 51 | + warnOnUnknownEnvironment(ctx); |
| 52 | + expect(ctx.logger.warn).not.toHaveBeenCalled(); |
| 53 | + }); |
| 54 | +}); |
| 55 | + |
| 56 | +describe(build.name, () => { |
| 57 | + it('warns during spin-up when the job environment is unknown', async () => { |
| 58 | + const warn = jest.fn(); |
| 59 | + const markBuildPhaseHasWarnings = jest.fn(); |
| 60 | + const ctx = { |
| 61 | + metadata: { environment: 'staging' }, |
| 62 | + job: { platform: Platform.ANDROID }, |
| 63 | + logger: { info: jest.fn(), warn, child: jest.fn() }, |
| 64 | + markBuildPhaseHasWarnings, |
| 65 | + runBuildPhase: jest.fn(async (_phase, callback) => callback()), |
| 66 | + } as unknown as BuildContext; |
| 67 | + const analytics = { logEvent: jest.fn(), flushEventsAsync: jest.fn() } as unknown as Analytics; |
| 68 | + |
| 69 | + await build({ ctx, buildId: 'build-id', analytics }); |
| 70 | + |
| 71 | + expect(Builders.androidBuilder).toHaveBeenCalled(); |
| 72 | + expect(markBuildPhaseHasWarnings).toHaveBeenCalled(); |
| 73 | + expect(warn).toHaveBeenCalledWith(expect.stringContaining('Unknown environment "staging"')); |
| 74 | + }); |
| 75 | +}); |
0 commit comments