|
| 1 | +import { BuilderContext, BuilderRun, ScheduleOptions, Target } from '@angular-devkit/architect/src'; |
| 2 | +import { JsonObject, logging } from '@angular-devkit/core'; |
| 3 | +import deploy from '../deploy/actions'; |
| 4 | +import { BuildTarget } from '../interfaces'; |
| 5 | +import { Schema } from '../deploy/schema'; |
| 6 | + |
| 7 | +/** |
| 8 | + * BUILD TARGET RESOLUTION TESTS |
| 9 | + * |
| 10 | + * Tests for Angular Builder-specific target parameters: |
| 11 | + * - buildTarget: Standard build target |
| 12 | + * - browserTarget: Legacy/alternative build target |
| 13 | + * - prerenderTarget: SSG/prerender build target |
| 14 | + * - noBuild: Skip build process |
| 15 | + * |
| 16 | + * These tests verify the builder correctly resolves which build target |
| 17 | + * to use and whether to trigger a build at all. |
| 18 | + */ |
| 19 | + |
| 20 | +describe('Build Target Resolution', () => { |
| 21 | + let context: BuilderContext; |
| 22 | + let scheduleTargetSpy: jest.SpyInstance; |
| 23 | + let capturedOptions: Schema | null = null; |
| 24 | + |
| 25 | + const PROJECT = 'test-project'; |
| 26 | + |
| 27 | + const mockEngine = { |
| 28 | + run: (dir: string, options: Schema, logger: logging.LoggerApi) => { |
| 29 | + capturedOptions = options; |
| 30 | + return Promise.resolve(); |
| 31 | + } |
| 32 | + }; |
| 33 | + |
| 34 | + beforeEach(() => { |
| 35 | + capturedOptions = null; |
| 36 | + context = createMockContext(); |
| 37 | + scheduleTargetSpy = jest.spyOn(context, 'scheduleTarget'); |
| 38 | + }); |
| 39 | + |
| 40 | + describe('buildTarget parameter', () => { |
| 41 | + it('should use buildTarget when specified', async () => { |
| 42 | + const buildTarget = `${PROJECT}:build:staging`; |
| 43 | + const expectedBuildTarget: BuildTarget = { name: buildTarget }; |
| 44 | + const options: Schema = { buildTarget, noBuild: false }; |
| 45 | + |
| 46 | + await deploy(mockEngine, context, expectedBuildTarget, options); |
| 47 | + |
| 48 | + expect(scheduleTargetSpy).toHaveBeenCalledTimes(1); |
| 49 | + expect(scheduleTargetSpy).toHaveBeenCalledWith( |
| 50 | + { |
| 51 | + project: PROJECT, |
| 52 | + target: 'build', |
| 53 | + configuration: 'staging' |
| 54 | + }, |
| 55 | + {} |
| 56 | + ); |
| 57 | + }); |
| 58 | + |
| 59 | + it('should parse buildTarget with project:target:configuration format', async () => { |
| 60 | + const buildTarget = 'myapp:build:production'; |
| 61 | + const expectedBuildTarget: BuildTarget = { name: buildTarget }; |
| 62 | + const options: Schema = { buildTarget, noBuild: false }; |
| 63 | + |
| 64 | + await deploy(mockEngine, context, expectedBuildTarget, options); |
| 65 | + |
| 66 | + expect(scheduleTargetSpy).toHaveBeenCalledWith( |
| 67 | + { |
| 68 | + project: 'myapp', |
| 69 | + target: 'build', |
| 70 | + configuration: 'production' |
| 71 | + }, |
| 72 | + {} |
| 73 | + ); |
| 74 | + }); |
| 75 | + |
| 76 | + it('should handle buildTarget without configuration', async () => { |
| 77 | + const buildTarget = `${PROJECT}:build`; |
| 78 | + const expectedBuildTarget: BuildTarget = { name: buildTarget }; |
| 79 | + const options: Schema = { buildTarget, noBuild: false }; |
| 80 | + |
| 81 | + await deploy(mockEngine, context, expectedBuildTarget, options); |
| 82 | + |
| 83 | + expect(scheduleTargetSpy).toHaveBeenCalledTimes(1); |
| 84 | + expect(scheduleTargetSpy).toHaveBeenCalledWith( |
| 85 | + { |
| 86 | + project: PROJECT, |
| 87 | + target: 'build', |
| 88 | + configuration: undefined |
| 89 | + }, |
| 90 | + {} |
| 91 | + ); |
| 92 | + }); |
| 93 | + |
| 94 | + it('should default to project:build:production when buildTarget not specified', async () => { |
| 95 | + const defaultTarget = `${PROJECT}:build:production`; |
| 96 | + const expectedBuildTarget: BuildTarget = { name: defaultTarget }; |
| 97 | + const options: Schema = { noBuild: false }; |
| 98 | + |
| 99 | + await deploy(mockEngine, context, expectedBuildTarget, options); |
| 100 | + |
| 101 | + expect(scheduleTargetSpy).toHaveBeenCalledWith( |
| 102 | + { |
| 103 | + project: PROJECT, |
| 104 | + target: 'build', |
| 105 | + configuration: 'production' |
| 106 | + }, |
| 107 | + {} |
| 108 | + ); |
| 109 | + }); |
| 110 | + }); |
| 111 | + |
| 112 | + describe('browserTarget parameter (legacy)', () => { |
| 113 | + it('should use browserTarget when specified', async () => { |
| 114 | + const browserTarget = `${PROJECT}:build:staging`; |
| 115 | + const expectedBuildTarget: BuildTarget = { name: browserTarget }; |
| 116 | + const options: Schema = { browserTarget, noBuild: false }; |
| 117 | + |
| 118 | + await deploy(mockEngine, context, expectedBuildTarget, options); |
| 119 | + |
| 120 | + expect(scheduleTargetSpy).toHaveBeenCalledTimes(1); |
| 121 | + expect(scheduleTargetSpy).toHaveBeenCalledWith( |
| 122 | + { |
| 123 | + project: PROJECT, |
| 124 | + target: 'build', |
| 125 | + configuration: 'staging' |
| 126 | + }, |
| 127 | + {} |
| 128 | + ); |
| 129 | + }); |
| 130 | + |
| 131 | + it('should parse browserTarget with project:target:configuration format', async () => { |
| 132 | + const browserTarget = 'legacy-app:build:development'; |
| 133 | + const expectedBuildTarget: BuildTarget = { name: browserTarget }; |
| 134 | + const options: Schema = { browserTarget, noBuild: false }; |
| 135 | + |
| 136 | + await deploy(mockEngine, context, expectedBuildTarget, options); |
| 137 | + |
| 138 | + expect(scheduleTargetSpy).toHaveBeenCalledWith( |
| 139 | + { |
| 140 | + project: 'legacy-app', |
| 141 | + target: 'build', |
| 142 | + configuration: 'development' |
| 143 | + }, |
| 144 | + {} |
| 145 | + ); |
| 146 | + }); |
| 147 | + }); |
| 148 | + |
| 149 | + describe('buildTarget vs browserTarget precedence', () => { |
| 150 | + it('should prefer browserTarget over buildTarget when both specified', async () => { |
| 151 | + // Note: In builder.ts line 25, browserTarget comes BEFORE buildTarget in the OR chain |
| 152 | + const browserTarget = `${PROJECT}:build:staging`; |
| 153 | + const buildTarget = `${PROJECT}:build:production`; |
| 154 | + const expectedBuildTarget: BuildTarget = { name: browserTarget }; |
| 155 | + const options: Schema = { browserTarget, buildTarget, noBuild: false }; |
| 156 | + |
| 157 | + await deploy(mockEngine, context, expectedBuildTarget, options); |
| 158 | + |
| 159 | + // Should use browserTarget (comes first in OR chain) |
| 160 | + expect(scheduleTargetSpy).toHaveBeenCalledWith( |
| 161 | + { |
| 162 | + project: PROJECT, |
| 163 | + target: 'build', |
| 164 | + configuration: 'staging' |
| 165 | + }, |
| 166 | + {} |
| 167 | + ); |
| 168 | + }); |
| 169 | + }); |
| 170 | + |
| 171 | + describe('prerenderTarget parameter', () => { |
| 172 | + it('should use prerenderTarget for SSG builds', async () => { |
| 173 | + const prerenderTarget = `${PROJECT}:prerender:production`; |
| 174 | + const expectedBuildTarget: BuildTarget = { name: prerenderTarget }; |
| 175 | + const options: Schema = { prerenderTarget, noBuild: false }; |
| 176 | + |
| 177 | + await deploy(mockEngine, context, expectedBuildTarget, options); |
| 178 | + |
| 179 | + expect(scheduleTargetSpy).toHaveBeenCalledTimes(1); |
| 180 | + expect(scheduleTargetSpy).toHaveBeenCalledWith( |
| 181 | + { |
| 182 | + project: PROJECT, |
| 183 | + target: 'prerender', |
| 184 | + configuration: 'production' |
| 185 | + }, |
| 186 | + {} |
| 187 | + ); |
| 188 | + }); |
| 189 | + |
| 190 | + it('should parse prerenderTarget with project:target:configuration format', async () => { |
| 191 | + const prerenderTarget = 'ssg-app:prerender:staging'; |
| 192 | + const expectedBuildTarget: BuildTarget = { name: prerenderTarget }; |
| 193 | + const options: Schema = { prerenderTarget, noBuild: false }; |
| 194 | + |
| 195 | + await deploy(mockEngine, context, expectedBuildTarget, options); |
| 196 | + |
| 197 | + expect(scheduleTargetSpy).toHaveBeenCalledWith( |
| 198 | + { |
| 199 | + project: 'ssg-app', |
| 200 | + target: 'prerender', |
| 201 | + configuration: 'staging' |
| 202 | + }, |
| 203 | + {} |
| 204 | + ); |
| 205 | + }); |
| 206 | + |
| 207 | + it('should prefer prerenderTarget over buildTarget when both specified', async () => { |
| 208 | + // Per builder.ts lines 45-47: prerenderTarget takes precedence |
| 209 | + const prerenderTarget = `${PROJECT}:prerender:production`; |
| 210 | + const buildTarget = `${PROJECT}:build:production`; |
| 211 | + const expectedPrerenderTarget: BuildTarget = { name: prerenderTarget }; |
| 212 | + const options: Schema = { prerenderTarget, buildTarget, noBuild: false }; |
| 213 | + |
| 214 | + await deploy(mockEngine, context, expectedPrerenderTarget, options); |
| 215 | + |
| 216 | + // Should use prerenderTarget (explicit precedence in code) |
| 217 | + expect(scheduleTargetSpy).toHaveBeenCalledWith( |
| 218 | + { |
| 219 | + project: PROJECT, |
| 220 | + target: 'prerender', |
| 221 | + configuration: 'production' |
| 222 | + }, |
| 223 | + {} |
| 224 | + ); |
| 225 | + }); |
| 226 | + |
| 227 | + it('should prefer prerenderTarget over browserTarget when both specified', async () => { |
| 228 | + const prerenderTarget = `${PROJECT}:prerender:production`; |
| 229 | + const browserTarget = `${PROJECT}:build:staging`; |
| 230 | + const expectedPrerenderTarget: BuildTarget = { name: prerenderTarget }; |
| 231 | + const options: Schema = { prerenderTarget, browserTarget, noBuild: false }; |
| 232 | + |
| 233 | + await deploy(mockEngine, context, expectedPrerenderTarget, options); |
| 234 | + |
| 235 | + // Should use prerenderTarget (highest precedence) |
| 236 | + expect(scheduleTargetSpy).toHaveBeenCalledWith( |
| 237 | + { |
| 238 | + project: PROJECT, |
| 239 | + target: 'prerender', |
| 240 | + configuration: 'production' |
| 241 | + }, |
| 242 | + {} |
| 243 | + ); |
| 244 | + }); |
| 245 | + |
| 246 | + it('should handle all three target types specified simultaneously', async () => { |
| 247 | + // Precedence: prerenderTarget > browserTarget > buildTarget > default |
| 248 | + const prerenderTarget = `${PROJECT}:prerender:production`; |
| 249 | + const browserTarget = `${PROJECT}:build:staging`; |
| 250 | + const buildTarget = `${PROJECT}:build:development`; |
| 251 | + const expectedPrerenderTarget: BuildTarget = { name: prerenderTarget }; |
| 252 | + const options: Schema = { prerenderTarget, browserTarget, buildTarget, noBuild: false }; |
| 253 | + |
| 254 | + await deploy(mockEngine, context, expectedPrerenderTarget, options); |
| 255 | + |
| 256 | + // Should use prerenderTarget (highest priority) |
| 257 | + expect(scheduleTargetSpy).toHaveBeenCalledWith( |
| 258 | + { |
| 259 | + project: PROJECT, |
| 260 | + target: 'prerender', |
| 261 | + configuration: 'production' |
| 262 | + }, |
| 263 | + {} |
| 264 | + ); |
| 265 | + }); |
| 266 | + }); |
| 267 | + |
| 268 | + describe('noBuild parameter', () => { |
| 269 | + it('should skip build when noBuild is true', async () => { |
| 270 | + const buildTarget = `${PROJECT}:build:production`; |
| 271 | + const expectedBuildTarget: BuildTarget = { name: buildTarget }; |
| 272 | + const options: Schema = { buildTarget, noBuild: true }; |
| 273 | + |
| 274 | + await deploy(mockEngine, context, expectedBuildTarget, options); |
| 275 | + |
| 276 | + // Should NOT call scheduleTarget when noBuild is true |
| 277 | + expect(scheduleTargetSpy).not.toHaveBeenCalled(); |
| 278 | + }); |
| 279 | + |
| 280 | + it('should trigger build when noBuild is false', async () => { |
| 281 | + const buildTarget = `${PROJECT}:build:production`; |
| 282 | + const expectedBuildTarget: BuildTarget = { name: buildTarget }; |
| 283 | + const options: Schema = { buildTarget, noBuild: false }; |
| 284 | + |
| 285 | + await deploy(mockEngine, context, expectedBuildTarget, options); |
| 286 | + |
| 287 | + // Should call scheduleTarget when noBuild is false |
| 288 | + expect(scheduleTargetSpy).toHaveBeenCalledTimes(1); |
| 289 | + }); |
| 290 | + |
| 291 | + it('should default to building when noBuild is not specified', async () => { |
| 292 | + const buildTarget = `${PROJECT}:build:production`; |
| 293 | + const expectedBuildTarget: BuildTarget = { name: buildTarget }; |
| 294 | + const options: Schema = { buildTarget }; |
| 295 | + |
| 296 | + await deploy(mockEngine, context, expectedBuildTarget, options); |
| 297 | + |
| 298 | + // Default is noBuild: false (should build) |
| 299 | + expect(scheduleTargetSpy).toHaveBeenCalledTimes(1); |
| 300 | + }); |
| 301 | + |
| 302 | + it('should skip build with noBuild=true even when prerenderTarget specified', async () => { |
| 303 | + const prerenderTarget = `${PROJECT}:prerender:production`; |
| 304 | + const expectedPrerenderTarget: BuildTarget = { name: prerenderTarget }; |
| 305 | + const options: Schema = { prerenderTarget, noBuild: true }; |
| 306 | + |
| 307 | + await deploy(mockEngine, context, expectedPrerenderTarget, options); |
| 308 | + |
| 309 | + // Should NOT build even with prerenderTarget |
| 310 | + expect(scheduleTargetSpy).not.toHaveBeenCalled(); |
| 311 | + }); |
| 312 | + |
| 313 | + it('should trigger build with noBuild=false when default target used', async () => { |
| 314 | + const defaultTarget = `${PROJECT}:build:production`; |
| 315 | + const expectedBuildTarget: BuildTarget = { name: defaultTarget }; |
| 316 | + const options: Schema = { noBuild: false }; |
| 317 | + |
| 318 | + await deploy(mockEngine, context, expectedBuildTarget, options); |
| 319 | + |
| 320 | + // Should build with default target |
| 321 | + expect(scheduleTargetSpy).toHaveBeenCalledTimes(1); |
| 322 | + expect(scheduleTargetSpy).toHaveBeenCalledWith( |
| 323 | + { |
| 324 | + project: PROJECT, |
| 325 | + target: 'build', |
| 326 | + configuration: 'production' |
| 327 | + }, |
| 328 | + {} |
| 329 | + ); |
| 330 | + }); |
| 331 | + }); |
| 332 | +}); |
| 333 | + |
| 334 | +function createMockContext(): BuilderContext { |
| 335 | + const mockContext: Partial<BuilderContext> = { |
| 336 | + target: { |
| 337 | + configuration: 'production', |
| 338 | + project: 'test-project', |
| 339 | + target: 'deploy' |
| 340 | + }, |
| 341 | + builder: { |
| 342 | + builderName: 'angular-cli-ghpages:deploy', |
| 343 | + description: 'Deploy to GitHub Pages', |
| 344 | + optionSchema: false |
| 345 | + }, |
| 346 | + currentDirectory: '/test', |
| 347 | + id: 1, |
| 348 | + logger: new logging.NullLogger(), |
| 349 | + workspaceRoot: '/test', |
| 350 | + addTeardown: _ => {}, |
| 351 | + validateOptions: <T extends JsonObject = JsonObject>(_options: JsonObject, _builderName: string) => Promise.resolve({} as T), |
| 352 | + getBuilderNameForTarget: () => Promise.resolve(''), |
| 353 | + getTargetOptions: (_: Target) => |
| 354 | + Promise.resolve({ |
| 355 | + outputPath: 'dist/test-project' |
| 356 | + } as JsonObject), |
| 357 | + reportProgress: (_: number, __?: number, ___?: string) => {}, |
| 358 | + reportStatus: (_: string) => {}, |
| 359 | + reportRunning: () => {}, |
| 360 | + scheduleBuilder: (_: string, __?: JsonObject, ___?: ScheduleOptions) => |
| 361 | + Promise.resolve({} as BuilderRun), |
| 362 | + scheduleTarget: (_: Target, __?: JsonObject, ___?: ScheduleOptions) => |
| 363 | + Promise.resolve({ |
| 364 | + result: Promise.resolve({ |
| 365 | + success: true, |
| 366 | + error: '', |
| 367 | + info: {}, |
| 368 | + target: {} as Target |
| 369 | + }) |
| 370 | + } as BuilderRun) |
| 371 | + }; |
| 372 | + |
| 373 | + return mockContext as BuilderContext; |
| 374 | +} |
0 commit comments