From 2a4450536aa74ae931ed695dc9e9c965274607ec Mon Sep 17 00:00:00 2001 From: Alan Agius <17563226+alan-agius4@users.noreply.github.com> Date: Wed, 9 Jul 2025 09:36:20 +0000 Subject: [PATCH] refactor: use `executeWithCases` instead of RXJS boillerplate Clean up several tests --- .../testing/builder/src/jasmine-helpers.ts | 32 +- .../tests/behavior/rebuild-assets_spec.ts | 78 +-- .../behavior/rebuild-component_styles_spec.ts | 56 +-- .../tests/behavior/rebuild-errors_spec.ts | 473 ++++++++---------- .../tests/behavior/rebuild-general_spec.ts | 111 ++-- .../behavior/rebuild-global_styles_spec.ts | 189 +++---- .../tests/behavior/rebuild-index-html_spec.ts | 60 +-- .../behavior/rebuild-web-workers_spec.ts | 145 +++--- .../behavior/typescript-rebuild-lazy_spec.ts | 71 ++- .../typescript-rebuild-touch-file_spec.ts | 44 +- .../options/inline-style-language_spec.ts | 83 ++- .../tests/behavior/build-errors_spec.ts | 63 +-- .../build_localize_replaced_watch_spec.ts | 40 +- .../behavior/build_translation_watch_spec.ts | 51 +- .../serve-live-reload-proxies_spec.ts | 151 ++---- .../behavior/serve_service-worker_spec.ts | 81 ++- .../karma/tests/behavior/rebuilds_spec.ts | 94 +--- .../src/builders/browser/specs/styles_spec.ts | 21 +- .../tests/behavior/index_watch_spec.ts | 45 +- .../tests/behavior/localize_watch_spec.ts | 41 +- .../tests/behavior/rebuild-errors_spec.ts | 333 ++++++------ .../options/inline-style-language_spec.ts | 76 ++- .../browser/tests/options/verbose_spec.ts | 48 +- .../browser/tests/options/watch_spec.ts | 42 +- .../build_localize_replaced_watch_spec.ts | 40 +- .../behavior/build_translation_watch_spec.ts | 49 +- .../serve-live-reload-proxies_spec.ts | 195 ++++---- .../behavior/serve_service-worker_spec.ts | 79 ++- .../dev-server/tests/options/watch_spec.ts | 108 ++-- .../src/builders/dev-server/tests/setup.ts | 6 - .../karma/tests/behavior/rebuilds_spec.ts | 67 +-- 31 files changed, 1212 insertions(+), 1760 deletions(-) diff --git a/modules/testing/builder/src/jasmine-helpers.ts b/modules/testing/builder/src/jasmine-helpers.ts index 15045a2f56d5..94fdbeb38fe1 100644 --- a/modules/testing/builder/src/jasmine-helpers.ts +++ b/modules/testing/builder/src/jasmine-helpers.ts @@ -9,15 +9,19 @@ import { BuilderHandlerFn } from '@angular-devkit/architect'; import { json } from '@angular-devkit/core'; import { readFileSync } from 'node:fs'; -import { concatMap, count, firstValueFrom, take, timeout } from 'rxjs'; -import { BuilderHarness, BuilderHarnessExecutionResult } from './builder-harness'; +import { concatMap, count, debounceTime, firstValueFrom, take, timeout } from 'rxjs'; +import { + BuilderHarness, + BuilderHarnessExecutionOptions, + BuilderHarnessExecutionResult, +} from './builder-harness'; import { host } from './test-utils'; /** * Maximum time for single build/rebuild * This accounts for CI variability. */ -export const BUILD_TIMEOUT = 25_000; +export const BUILD_TIMEOUT = 30_000; const optionSchemaCache = new Map(); @@ -62,10 +66,12 @@ export class JasmineBuilderHarness extends BuilderHarness { executionResult: BuilderHarnessExecutionResult, index: number, ) => void | Promise)[], + options?: Partial & { timeout?: number }, ): Promise { const executionCount = await firstValueFrom( - this.execute().pipe( - timeout(BUILD_TIMEOUT), + this.execute(options).pipe( + timeout(options?.timeout ?? BUILD_TIMEOUT), + debounceTime(100), // This is needed as sometimes 2 events for the same change fire with webpack. concatMap(async (result, index) => await cases[index](result, index)), take(cases.length), count(), @@ -118,13 +124,17 @@ export function expectFile(path: string, harness: BuilderHarness): Harness return { toExist() { const exists = harness.hasFile(path); - expect(exists).toBe(true, 'Expected file to exist: ' + path); + expect(exists) + .withContext('Expected file to exist: ' + path) + .toBeTrue(); return exists; }, toNotExist() { const exists = harness.hasFile(path); - expect(exists).toBe(false, 'Expected file to not exist: ' + path); + expect(exists) + .withContext('Expected file to exist: ' + path) + .toBeFalse(); return !exists; }, @@ -170,13 +180,17 @@ export function expectDirectory( return { toExist() { const exists = harness.hasDirectory(path); - expect(exists).toBe(true, 'Expected directory to exist: ' + path); + expect(exists) + .withContext('Expected directory to exist: ' + path) + .toBeTrue(); return exists; }, toNotExist() { const exists = harness.hasDirectory(path); - expect(exists).toBe(false, 'Expected directory to not exist: ' + path); + expect(exists) + .withContext('Expected directory to not exist: ' + path) + .toBeFalse(); return !exists; }, diff --git a/packages/angular/build/src/builders/application/tests/behavior/rebuild-assets_spec.ts b/packages/angular/build/src/builders/application/tests/behavior/rebuild-assets_spec.ts index a48c19fd1baf..7bfcca94d242 100644 --- a/packages/angular/build/src/builders/application/tests/behavior/rebuild-assets_spec.ts +++ b/packages/angular/build/src/builders/application/tests/behavior/rebuild-assets_spec.ts @@ -6,16 +6,9 @@ * found in the LICENSE file at https://angular.dev/license */ -import { concatMap, count, take, timeout } from 'rxjs'; import { buildApplication } from '../../index'; import { APPLICATION_BUILDER_INFO, BASE_OPTIONS, describeBuilder } from '../setup'; -/** - * Maximum time in milliseconds for single build/rebuild - * This accounts for CI variability. - */ -const BUILD_TIMEOUT = 10_000; - describeBuilder(buildApplication, APPLICATION_BUILDER_INFO, (harness) => { describe('Behavior: "Rebuilds when input asset changes"', () => { beforeEach(async () => { @@ -36,30 +29,18 @@ describeBuilder(buildApplication, APPLICATION_BUILDER_INFO, (harness) => { watch: true, }); - const buildCount = await harness - .execute({ outputLogsOnFailure: false }) - .pipe( - timeout(BUILD_TIMEOUT), - concatMap(async ({ result }, index) => { - switch (index) { - case 0: - expect(result?.success).toBeTrue(); - harness.expectFile('dist/browser/asset.txt').content.toContain('foo'); + await harness.executeWithCases([ + async ({ result }) => { + expect(result?.success).toBeTrue(); + harness.expectFile('dist/browser/asset.txt').content.toContain('foo'); - await harness.writeFile('public/asset.txt', 'bar'); - break; - case 1: - expect(result?.success).toBeTrue(); - harness.expectFile('dist/browser/asset.txt').content.toContain('bar'); - break; - } - }), - take(2), - count(), - ) - .toPromise(); - - expect(buildCount).toBe(2); + await harness.writeFile('public/asset.txt', 'bar'); + }, + ({ result }) => { + expect(result?.success).toBeTrue(); + harness.expectFile('dist/browser/asset.txt').content.toContain('bar'); + }, + ]); }); it('remove deleted asset from output', async () => { @@ -79,32 +60,21 @@ describeBuilder(buildApplication, APPLICATION_BUILDER_INFO, (harness) => { watch: true, }); - const buildCount = await harness - .execute({ outputLogsOnFailure: false }) - .pipe( - timeout(BUILD_TIMEOUT), - concatMap(async ({ result }, index) => { - switch (index) { - case 0: - expect(result?.success).toBeTrue(); - harness.expectFile('dist/browser/asset-one.txt').toExist(); - harness.expectFile('dist/browser/asset-two.txt').toExist(); + await harness.executeWithCases([ + async ({ result }) => { + expect(result?.success).toBeTrue(); + harness.expectFile('dist/browser/asset-one.txt').toExist(); + harness.expectFile('dist/browser/asset-two.txt').toExist(); - await harness.removeFile('public/asset-two.txt'); - break; - case 1: - expect(result?.success).toBeTrue(); - harness.expectFile('dist/browser/asset-one.txt').toExist(); - harness.expectFile('dist/browser/asset-two.txt').toNotExist(); - break; - } - }), - take(2), - count(), - ) - .toPromise(); + await harness.removeFile('public/asset-two.txt'); + }, - expect(buildCount).toBe(2); + ({ result }) => { + expect(result?.success).toBeTrue(); + harness.expectFile('dist/browser/asset-one.txt').toExist(); + harness.expectFile('dist/browser/asset-two.txt').toNotExist(); + }, + ]); }); }); }); diff --git a/packages/angular/build/src/builders/application/tests/behavior/rebuild-component_styles_spec.ts b/packages/angular/build/src/builders/application/tests/behavior/rebuild-component_styles_spec.ts index a252a0580d0b..26ae35a8221f 100644 --- a/packages/angular/build/src/builders/application/tests/behavior/rebuild-component_styles_spec.ts +++ b/packages/angular/build/src/builders/application/tests/behavior/rebuild-component_styles_spec.ts @@ -6,7 +6,6 @@ * found in the LICENSE file at https://angular.dev/license */ -import { concatMap, count, take, timeout } from 'rxjs'; import { buildApplication } from '../../index'; import { APPLICATION_BUILDER_INFO, BASE_OPTIONS, describeBuilder } from '../setup'; @@ -32,46 +31,31 @@ describeBuilder(buildApplication, APPLICATION_BUILDER_INFO, (harness) => { await harness.writeFile('src/app/app.component.scss', "@import './a';"); await harness.writeFile('src/app/a.scss', '$primary: aqua;\\nh1 { color: $primary; }'); - const buildCount = await harness - .execute() - .pipe( - timeout(30000), - concatMap(async ({ result }, index) => { - expect(result?.success).toBe(true); + await harness.executeWithCases([ + async ({ result }) => { + expect(result?.success).toBe(true); - switch (index) { - case 0: - harness.expectFile('dist/browser/main.js').content.toContain('color: aqua'); - harness.expectFile('dist/browser/main.js').content.not.toContain('color: blue'); + harness.expectFile('dist/browser/main.js').content.toContain('color: aqua'); + harness.expectFile('dist/browser/main.js').content.not.toContain('color: blue'); - await harness.writeFile( - 'src/app/a.scss', - '$primary: blue;\\nh1 { color: $primary; }', - ); - break; - case 1: - harness.expectFile('dist/browser/main.js').content.not.toContain('color: aqua'); - harness.expectFile('dist/browser/main.js').content.toContain('color: blue'); + await harness.writeFile('src/app/a.scss', '$primary: blue;\\nh1 { color: $primary; }'); + }, + async ({ result }) => { + expect(result?.success).toBe(true); - await harness.writeFile( - 'src/app/a.scss', - '$primary: green;\\nh1 { color: $primary; }', - ); - break; - case 2: - harness.expectFile('dist/browser/main.js').content.not.toContain('color: aqua'); - harness.expectFile('dist/browser/main.js').content.not.toContain('color: blue'); - harness.expectFile('dist/browser/main.js').content.toContain('color: green'); + harness.expectFile('dist/browser/main.js').content.not.toContain('color: aqua'); + harness.expectFile('dist/browser/main.js').content.toContain('color: blue'); - break; - } - }), - take(3), - count(), - ) - .toPromise(); + await harness.writeFile('src/app/a.scss', '$primary: green;\\nh1 { color: $primary; }'); + }, + ({ result }) => { + expect(result?.success).toBe(true); - expect(buildCount).toBe(3); + harness.expectFile('dist/browser/main.js').content.not.toContain('color: aqua'); + harness.expectFile('dist/browser/main.js').content.not.toContain('color: blue'); + harness.expectFile('dist/browser/main.js').content.toContain('color: green'); + }, + ]); }); } }); diff --git a/packages/angular/build/src/builders/application/tests/behavior/rebuild-errors_spec.ts b/packages/angular/build/src/builders/application/tests/behavior/rebuild-errors_spec.ts index 196cbf4e6b5d..0dde3b4be58f 100644 --- a/packages/angular/build/src/builders/application/tests/behavior/rebuild-errors_spec.ts +++ b/packages/angular/build/src/builders/application/tests/behavior/rebuild-errors_spec.ts @@ -7,7 +7,6 @@ */ import { logging } from '@angular-devkit/core'; -import { concatMap, count, take, timeout } from 'rxjs'; import { buildApplication } from '../../index'; import { APPLICATION_BUILDER_INFO, BASE_OPTIONS, describeBuilder } from '../setup'; @@ -73,85 +72,71 @@ describeBuilder(buildApplication, APPLICATION_BUILDER_INFO, (harness) => { `, ); - const buildCount = await harness - .execute({ outputLogsOnFailure: false }) - .pipe( - timeout(BUILD_TIMEOUT), - concatMap(async ({ result, logs }, index) => { - switch (index) { - case 0: - expect(result?.success).toBeTrue(); + await harness.executeWithCases( + [ + async ({ result }) => { + expect(result?.success).toBeTrue(); - // Update directive to use a different input type for 'foo' (number -> string) - // Should cause a template error - await harness.writeFile( - 'src/app/dir.ts', - ` + // Update directive to use a different input type for 'foo' (number -> string) + // Should cause a template error + await harness.writeFile( + 'src/app/dir.ts', + ` import { Directive, Input } from '@angular/core'; @Directive({ selector: 'dir', standalone: false }) export class Dir { @Input() foo: string; } `, - ); - - break; - case 1: - expect(result?.success).toBeFalse(); - expect(logs).toContain( - jasmine.objectContaining({ - message: jasmine.stringMatching(typeErrorText), - }), - ); - - // Make an unrelated change to verify error cache was updated - // Should persist error in the next rebuild - await harness.modifyFile('src/main.ts', (content) => content + '\n'); - - break; - case 2: - expect(result?.success).toBeFalse(); - expect(logs).toContain( - jasmine.objectContaining({ - message: jasmine.stringMatching(typeErrorText), - }), - ); - - // Revert the directive change that caused the error - // Should remove the error - await harness.writeFile('src/app/dir.ts', goodDirectiveContents); - - break; - case 3: - expect(result?.success).toBeTrue(); - expect(logs).not.toContain( - jasmine.objectContaining({ - message: jasmine.stringMatching(typeErrorText), - }), - ); - - // Make an unrelated change to verify error cache was updated - // Should continue showing no error - await harness.modifyFile('src/main.ts', (content) => content + '\n'); - - break; - case 4: - expect(result?.success).toBeTrue(); - expect(logs).not.toContain( - jasmine.objectContaining({ - message: jasmine.stringMatching(typeErrorText), - }), - ); - - break; - } - }), - take(5), - count(), - ) - .toPromise(); - - expect(buildCount).toBe(5); + ); + }, + async ({ result, logs }) => { + expect(result?.success).toBeFalse(); + expect(logs).toContain( + jasmine.objectContaining({ + message: jasmine.stringMatching(typeErrorText), + }), + ); + + // Make an unrelated change to verify error cache was updated + // Should persist error in the next rebuild + await harness.modifyFile('src/main.ts', (content) => content + '\n'); + }, + async ({ result, logs }) => { + expect(result?.success).toBeFalse(); + expect(logs).toContain( + jasmine.objectContaining({ + message: jasmine.stringMatching(typeErrorText), + }), + ); + + // Revert the directive change that caused the error + // Should remove the error + await harness.writeFile('src/app/dir.ts', goodDirectiveContents); + }, + async ({ result, logs }) => { + expect(result?.success).toBeTrue(); + expect(logs).not.toContain( + jasmine.objectContaining({ + message: jasmine.stringMatching(typeErrorText), + }), + ); + + // Make an unrelated change to verify error cache was updated + // Should continue showing no error + await harness.modifyFile('src/main.ts', (content) => content + '\n'); + }, + ({ result, logs }) => { + expect(result?.success).toBeTrue(); + expect(logs).not.toContain( + jasmine.objectContaining({ + message: jasmine.stringMatching(typeErrorText), + }), + ); + }, + ], + { outputLogsOnFailure: false }, + ); }); it('detects cumulative block syntax errors', async () => { @@ -160,104 +145,89 @@ describeBuilder(buildApplication, APPLICATION_BUILDER_INFO, (harness) => { watch: true, }); - const buildCount = await harness - .execute({ outputLogsOnFailure: false }) - .pipe( - timeout(BUILD_TIMEOUT), - concatMap(async ({ logs }, index) => { - switch (index) { - case 0: - // Add invalid block syntax - await harness.appendToFile('src/app/app.component.html', '@one'); - - break; - case 1: - expect(logs).toContain( - jasmine.objectContaining({ - message: jasmine.stringContaining('@one'), - }), - ); - - // Make an unrelated change to verify error cache was updated - // Should persist error in the next rebuild - await harness.modifyFile('src/main.ts', (content) => content + '\n'); - - break; - case 2: - expect(logs).toContain( - jasmine.objectContaining({ - message: jasmine.stringContaining('@one'), - }), - ); - - // Add more invalid block syntax - await harness.appendToFile('src/app/app.component.html', '@two'); - - break; - case 3: - expect(logs).toContain( - jasmine.objectContaining({ - message: jasmine.stringContaining('@one'), - }), - ); - expect(logs).toContain( - jasmine.objectContaining({ - message: jasmine.stringContaining('@two'), - }), - ); - - // Add more invalid block syntax - await harness.appendToFile('src/app/app.component.html', '@three'); - - break; - case 4: - expect(logs).toContain( - jasmine.objectContaining({ - message: jasmine.stringContaining('@one'), - }), - ); - expect(logs).toContain( - jasmine.objectContaining({ - message: jasmine.stringContaining('@two'), - }), - ); - expect(logs).toContain( - jasmine.objectContaining({ - message: jasmine.stringContaining('@three'), - }), - ); - - // Revert the changes that caused the error - // Should remove the error - await harness.writeFile('src/app/app.component.html', '

GOOD

'); - - break; - case 5: - expect(logs).not.toContain( - jasmine.objectContaining({ - message: jasmine.stringContaining('@one'), - }), - ); - expect(logs).not.toContain( - jasmine.objectContaining({ - message: jasmine.stringContaining('@two'), - }), - ); - expect(logs).not.toContain( - jasmine.objectContaining({ - message: jasmine.stringContaining('@three'), - }), - ); - - break; - } - }), - take(6), - count(), - ) - .toPromise(); - - expect(buildCount).toBe(6); + await harness.executeWithCases( + [ + async () => { + // Add invalid block syntax + await harness.appendToFile('src/app/app.component.html', '@one'); + }, + async ({ logs }) => { + expect(logs).toContain( + jasmine.objectContaining({ + message: jasmine.stringContaining('@one'), + }), + ); + + // Make an unrelated change to verify error cache was updated + // Should persist error in the next rebuild + await harness.modifyFile('src/main.ts', (content) => content + '\n'); + }, + async ({ logs }) => { + expect(logs).toContain( + jasmine.objectContaining({ + message: jasmine.stringContaining('@one'), + }), + ); + + // Add more invalid block syntax + await harness.appendToFile('src/app/app.component.html', '@two'); + }, + async ({ logs }) => { + expect(logs).toContain( + jasmine.objectContaining({ + message: jasmine.stringContaining('@one'), + }), + ); + expect(logs).toContain( + jasmine.objectContaining({ + message: jasmine.stringContaining('@two'), + }), + ); + + // Add more invalid block syntax + await harness.appendToFile('src/app/app.component.html', '@three'); + }, + async ({ logs }) => { + expect(logs).toContain( + jasmine.objectContaining({ + message: jasmine.stringContaining('@one'), + }), + ); + expect(logs).toContain( + jasmine.objectContaining({ + message: jasmine.stringContaining('@two'), + }), + ); + expect(logs).toContain( + jasmine.objectContaining({ + message: jasmine.stringContaining('@three'), + }), + ); + + // Revert the changes that caused the error + // Should remove the error + await harness.writeFile('src/app/app.component.html', '

GOOD

'); + }, + ({ logs }) => { + expect(logs).not.toContain( + jasmine.objectContaining({ + message: jasmine.stringContaining('@one'), + }), + ); + expect(logs).not.toContain( + jasmine.objectContaining({ + message: jasmine.stringContaining('@two'), + }), + ); + expect(logs).not.toContain( + jasmine.objectContaining({ + message: jasmine.stringContaining('@three'), + }), + ); + }, + ], + { outputLogsOnFailure: false }, + ); }); it('recovers from component stylesheet error', async () => { @@ -267,46 +237,34 @@ describeBuilder(buildApplication, APPLICATION_BUILDER_INFO, (harness) => { aot: false, }); - const buildCount = await harness - .execute({ outputLogsOnFailure: false }) - .pipe( - timeout(BUILD_TIMEOUT), - concatMap(async ({ result, logs }, index) => { - switch (index) { - case 0: - await harness.writeFile('src/app/app.component.css', 'invalid-css-content'); - - break; - case 1: - expect(logs).toContain( - jasmine.objectContaining({ - message: jasmine.stringMatching('invalid-css-content'), - }), - ); - - await harness.writeFile('src/app/app.component.css', 'p { color: green }'); - - break; - case 2: - expect(logs).not.toContain( - jasmine.objectContaining({ - message: jasmine.stringMatching('invalid-css-content'), - }), - ); - - harness - .expectFile('dist/browser/main.js') - .content.toContain('p {\\n color: green;\\n}'); - - break; - } - }), - take(3), - count(), - ) - .toPromise(); - - expect(buildCount).toBe(3); + await harness.executeWithCases( + [ + async () => { + await harness.writeFile('src/app/app.component.css', 'invalid-css-content'); + }, + async ({ logs }) => { + expect(logs).toContain( + jasmine.objectContaining({ + message: jasmine.stringMatching('invalid-css-content'), + }), + ); + + await harness.writeFile('src/app/app.component.css', 'p { color: green }'); + }, + ({ logs }) => { + expect(logs).not.toContain( + jasmine.objectContaining({ + message: jasmine.stringMatching('invalid-css-content'), + }), + ); + + harness + .expectFile('dist/browser/main.js') + .content.toContain('p {\\n color: green;\\n}'); + }, + ], + { outputLogsOnFailure: false }, + ); }); it('recovers from component template error', async () => { @@ -315,59 +273,46 @@ describeBuilder(buildApplication, APPLICATION_BUILDER_INFO, (harness) => { watch: true, }); - const buildCount = await harness - .execute({ outputLogsOnFailure: false }) - .pipe( - timeout(BUILD_TIMEOUT), - concatMap(async ({ result, logs }, index) => { - switch (index) { - case 0: - // Missing ending `>` on the div will cause an error - await harness.appendToFile('src/app/app.component.html', '
Hello, world!({ - message: jasmine.stringMatching('Unexpected character "EOF"'), - }), - ); - - await harness.appendToFile('src/app/app.component.html', '>'); - - break; - case 2: - expect(logs).not.toContain( - jasmine.objectContaining({ - message: jasmine.stringMatching('Unexpected character "EOF"'), - }), - ); - - harness.expectFile('dist/browser/main.js').content.toContain('Hello, world!'); - - // Make an additional valid change to ensure that rebuilds still trigger - await harness.appendToFile('src/app/app.component.html', '
Guten Tag
'); - - break; - case 3: - expect(logs).not.toContain( - jasmine.objectContaining({ - message: jasmine.stringMatching('invalid-css-content'), - }), - ); - - harness.expectFile('dist/browser/main.js').content.toContain('Hello, world!'); - harness.expectFile('dist/browser/main.js').content.toContain('Guten Tag'); - - break; - } - }), - take(4), - count(), - ) - .toPromise(); - - expect(buildCount).toBe(4); + await harness.executeWithCases( + [ + async () => { + // Missing ending `>` on the div will cause an error + await harness.appendToFile('src/app/app.component.html', '
Hello, world! { + expect(logs).toContain( + jasmine.objectContaining({ + message: jasmine.stringMatching('Unexpected character "EOF"'), + }), + ); + + await harness.appendToFile('src/app/app.component.html', '>'); + }, + async ({ logs }) => { + expect(logs).not.toContain( + jasmine.objectContaining({ + message: jasmine.stringMatching('Unexpected character "EOF"'), + }), + ); + + harness.expectFile('dist/browser/main.js').content.toContain('Hello, world!'); + + // Make an additional valid change to ensure that rebuilds still trigger + await harness.appendToFile('src/app/app.component.html', '
Guten Tag
'); + }, + ({ logs }) => { + expect(logs).not.toContain( + jasmine.objectContaining({ + message: jasmine.stringMatching('invalid-css-content'), + }), + ); + + harness.expectFile('dist/browser/main.js').content.toContain('Hello, world!'); + harness.expectFile('dist/browser/main.js').content.toContain('Guten Tag'); + }, + ], + { outputLogsOnFailure: false }, + ); }); }); }); diff --git a/packages/angular/build/src/builders/application/tests/behavior/rebuild-general_spec.ts b/packages/angular/build/src/builders/application/tests/behavior/rebuild-general_spec.ts index ca88f94e5b63..d9ea8870f687 100644 --- a/packages/angular/build/src/builders/application/tests/behavior/rebuild-general_spec.ts +++ b/packages/angular/build/src/builders/application/tests/behavior/rebuild-general_spec.ts @@ -6,7 +6,6 @@ * found in the LICENSE file at https://angular.dev/license */ -import { concatMap, count, take, timeout } from 'rxjs'; import { buildApplication } from '../../index'; import { APPLICATION_BUILDER_INFO, BASE_OPTIONS, describeBuilder } from '../setup'; @@ -45,68 +44,54 @@ describeBuilder(buildApplication, APPLICATION_BUILDER_INFO, (harness) => { `, ); - const buildCount = await harness - .execute({ outputLogsOnFailure: false }) - .pipe( - timeout(BUILD_TIMEOUT), - concatMap(async ({ result, logs }, index) => { - switch (index) { - case 0: - expect(result?.success).toBeTrue(); - harness.expectFile('dist/browser/main.js').content.toContain('FILE-A'); - - // Delete the imported file - await harness.removeFile('src/app/file-a.ts'); - - break; - case 1: - // Should fail from missing import - expect(result?.success).toBeFalse(); - - // Remove the failing import - await harness.modifyFile('src/app/app.component.ts', (content) => - content.replace(`import './file-a';`, ''), - ); - - break; - case 2: - expect(result?.success).toBeTrue(); - - harness.expectFile('dist/browser/main.js').content.not.toContain('FILE-A'); - - // Recreate the file and the import - await harness.writeFile('src/app/file-a.ts', fileAContent); - await harness.modifyFile( - 'src/app/app.component.ts', - (content) => `import './file-a';\n` + content, - ); - - break; - case 3: - expect(result?.success).toBeTrue(); - - harness.expectFile('dist/browser/main.js').content.toContain('FILE-A'); - - // Change the imported file - await harness.modifyFile('src/app/file-a.ts', (content) => - content.replace('FILE-A', 'FILE-B'), - ); - - break; - case 4: - expect(result?.success).toBeTrue(); - - harness.expectFile('dist/browser/main.js').content.toContain('FILE-B'); - - break; - } - }), - take(5), - count(), - ) - .toPromise(); - - expect(buildCount).toBe(5); + await harness.executeWithCases( + [ + async ({ result }) => { + expect(result?.success).toBeTrue(); + harness.expectFile('dist/browser/main.js').content.toContain('FILE-A'); + + // Delete the imported file + await harness.removeFile('src/app/file-a.ts'); + }, + async ({ result }) => { + // Should fail from missing import + expect(result?.success).toBeFalse(); + + // Remove the failing import + await harness.modifyFile('src/app/app.component.ts', (content) => + content.replace(`import './file-a';`, ''), + ); + }, + async ({ result }) => { + expect(result?.success).toBeTrue(); + + harness.expectFile('dist/browser/main.js').content.not.toContain('FILE-A'); + + // Recreate the file and the import + await harness.writeFile('src/app/file-a.ts', fileAContent); + await harness.modifyFile( + 'src/app/app.component.ts', + (content) => `import './file-a';\n` + content, + ); + }, + async ({ result }) => { + expect(result?.success).toBeTrue(); + + harness.expectFile('dist/browser/main.js').content.toContain('FILE-A'); + + // Change the imported file + await harness.modifyFile('src/app/file-a.ts', (content) => + content.replace('FILE-A', 'FILE-B'), + ); + }, + ({ result }) => { + expect(result?.success).toBeTrue(); + + harness.expectFile('dist/browser/main.js').content.toContain('FILE-B'); + }, + ], + { outputLogsOnFailure: false }, + ); }); }); }); diff --git a/packages/angular/build/src/builders/application/tests/behavior/rebuild-global_styles_spec.ts b/packages/angular/build/src/builders/application/tests/behavior/rebuild-global_styles_spec.ts index e58b2e031a90..22c4c32202bd 100644 --- a/packages/angular/build/src/builders/application/tests/behavior/rebuild-global_styles_spec.ts +++ b/packages/angular/build/src/builders/application/tests/behavior/rebuild-global_styles_spec.ts @@ -6,16 +6,9 @@ * found in the LICENSE file at https://angular.dev/license */ -import { concatMap, count, take, timeout } from 'rxjs'; import { buildApplication } from '../../index'; import { APPLICATION_BUILDER_INFO, BASE_OPTIONS, describeBuilder } from '../setup'; -/** - * Maximum time in milliseconds for single build/rebuild - * This accounts for CI variability. - */ -export const BUILD_TIMEOUT = 30_000; - describeBuilder(buildApplication, APPLICATION_BUILDER_INFO, (harness) => { describe('Behavior: "Rebuilds when global stylesheets change"', () => { beforeEach(async () => { @@ -33,41 +26,31 @@ describeBuilder(buildApplication, APPLICATION_BUILDER_INFO, (harness) => { await harness.writeFile('src/styles.scss', "@import './a';"); await harness.writeFile('src/a.scss', '$primary: aqua;\\nh1 { color: $primary; }'); - const buildCount = await harness - .execute({ outputLogsOnFailure: false }) - .pipe( - timeout(30000), - concatMap(async ({ result }, index) => { - switch (index) { - case 0: - expect(result?.success).toBe(true); - harness.expectFile('dist/browser/styles.css').content.toContain('color: aqua'); - harness.expectFile('dist/browser/styles.css').content.not.toContain('color: blue'); - - await harness.writeFile( - 'src/a.scss', - 'invalid-invalid-invalid\\nh1 { color: $primary; }', - ); - break; - case 1: - expect(result?.success).toBe(false); - - await harness.writeFile('src/a.scss', '$primary: blue;\\nh1 { color: $primary; }'); - break; - case 2: - expect(result?.success).toBe(true); - harness.expectFile('dist/browser/styles.css').content.not.toContain('color: aqua'); - harness.expectFile('dist/browser/styles.css').content.toContain('color: blue'); - - break; - } - }), - take(3), - count(), - ) - .toPromise(); - - expect(buildCount).toBe(3); + await harness.executeWithCases( + [ + async ({ result }) => { + expect(result?.success).toBe(true); + harness.expectFile('dist/browser/styles.css').content.toContain('color: aqua'); + harness.expectFile('dist/browser/styles.css').content.not.toContain('color: blue'); + + await harness.writeFile( + 'src/a.scss', + 'invalid-invalid-invalid\\nh1 { color: $primary; }', + ); + }, + async ({ result }) => { + expect(result?.success).toBe(false); + + await harness.writeFile('src/a.scss', '$primary: blue;\\nh1 { color: $primary; }'); + }, + ({ result }) => { + expect(result?.success).toBe(true); + harness.expectFile('dist/browser/styles.css').content.not.toContain('color: aqua'); + harness.expectFile('dist/browser/styles.css').content.toContain('color: blue'); + }, + ], + { outputLogsOnFailure: false }, + ); }); it('rebuilds Sass stylesheet after error on initial build from import', async () => { @@ -80,37 +63,28 @@ describeBuilder(buildApplication, APPLICATION_BUILDER_INFO, (harness) => { await harness.writeFile('src/styles.scss', "@import './a';"); await harness.writeFile('src/a.scss', 'invalid-invalid-invalid\\nh1 { color: $primary; }'); - const buildCount = await harness - .execute({ outputLogsOnFailure: false }) - .pipe( - timeout(30000), - concatMap(async ({ result }, index) => { - switch (index) { - case 0: - expect(result?.success).toBe(false); - - await harness.writeFile('src/a.scss', '$primary: aqua;\\nh1 { color: $primary; }'); - break; - case 1: - expect(result?.success).toBe(true); - harness.expectFile('dist/browser/styles.css').content.toContain('color: aqua'); - harness.expectFile('dist/browser/styles.css').content.not.toContain('color: blue'); - - await harness.writeFile('src/a.scss', '$primary: blue;\\nh1 { color: $primary; }'); - break; - case 2: - expect(result?.success).toBe(true); - harness.expectFile('dist/browser/styles.css').content.not.toContain('color: aqua'); - harness.expectFile('dist/browser/styles.css').content.toContain('color: blue'); - break; - } - }), - take(3), - count(), - ) - .toPromise(); - - expect(buildCount).toBe(3); + await harness.executeWithCases( + [ + async ({ result }) => { + expect(result?.success).toBe(false); + + await harness.writeFile('src/a.scss', '$primary: aqua;\\nh1 { color: $primary; }'); + }, + async ({ result }) => { + expect(result?.success).toBe(true); + harness.expectFile('dist/browser/styles.css').content.toContain('color: aqua'); + harness.expectFile('dist/browser/styles.css').content.not.toContain('color: blue'); + + await harness.writeFile('src/a.scss', '$primary: blue;\\nh1 { color: $primary; }'); + }, + ({ result }) => { + expect(result?.success).toBe(true); + harness.expectFile('dist/browser/styles.css').content.not.toContain('color: aqua'); + harness.expectFile('dist/browser/styles.css').content.toContain('color: blue'); + }, + ], + { outputLogsOnFailure: false }, + ); }); it('rebuilds dependent Sass stylesheets after error on initial build from import', async () => { @@ -127,45 +101,36 @@ describeBuilder(buildApplication, APPLICATION_BUILDER_INFO, (harness) => { await harness.writeFile('src/other.scss', "@import './a'; h1 { color: green; }"); await harness.writeFile('src/a.scss', 'invalid-invalid-invalid\\nh1 { color: $primary; }'); - const buildCount = await harness - .execute({ outputLogsOnFailure: false }) - .pipe( - timeout(30000), - concatMap(async ({ result }, index) => { - switch (index) { - case 0: - expect(result?.success).toBe(false); - - await harness.writeFile('src/a.scss', '$primary: aqua;\\nh1 { color: $primary; }'); - break; - case 1: - expect(result?.success).toBe(true); - harness.expectFile('dist/browser/styles.css').content.toContain('color: aqua'); - harness.expectFile('dist/browser/styles.css').content.not.toContain('color: blue'); - - harness.expectFile('dist/browser/other.css').content.toContain('color: green'); - harness.expectFile('dist/browser/other.css').content.toContain('color: aqua'); - harness.expectFile('dist/browser/other.css').content.not.toContain('color: blue'); - - await harness.writeFile('src/a.scss', '$primary: blue;\\nh1 { color: $primary; }'); - break; - case 2: - expect(result?.success).toBe(true); - harness.expectFile('dist/browser/styles.css').content.not.toContain('color: aqua'); - harness.expectFile('dist/browser/styles.css').content.toContain('color: blue'); - - harness.expectFile('dist/browser/other.css').content.toContain('color: green'); - harness.expectFile('dist/browser/other.css').content.not.toContain('color: aqua'); - harness.expectFile('dist/browser/other.css').content.toContain('color: blue'); - break; - } - }), - take(3), - count(), - ) - .toPromise(); - - expect(buildCount).toBe(3); + await harness.executeWithCases( + [ + async ({ result }) => { + expect(result?.success).toBe(false); + + await harness.writeFile('src/a.scss', '$primary: aqua;\\nh1 { color: $primary; }'); + }, + async ({ result }) => { + expect(result?.success).toBe(true); + harness.expectFile('dist/browser/styles.css').content.toContain('color: aqua'); + harness.expectFile('dist/browser/styles.css').content.not.toContain('color: blue'); + + harness.expectFile('dist/browser/other.css').content.toContain('color: green'); + harness.expectFile('dist/browser/other.css').content.toContain('color: aqua'); + harness.expectFile('dist/browser/other.css').content.not.toContain('color: blue'); + + await harness.writeFile('src/a.scss', '$primary: blue;\\nh1 { color: $primary; }'); + }, + ({ result }) => { + expect(result?.success).toBe(true); + harness.expectFile('dist/browser/styles.css').content.not.toContain('color: aqua'); + harness.expectFile('dist/browser/styles.css').content.toContain('color: blue'); + + harness.expectFile('dist/browser/other.css').content.toContain('color: green'); + harness.expectFile('dist/browser/other.css').content.not.toContain('color: aqua'); + harness.expectFile('dist/browser/other.css').content.toContain('color: blue'); + }, + ], + { outputLogsOnFailure: false }, + ); }); }); }); diff --git a/packages/angular/build/src/builders/application/tests/behavior/rebuild-index-html_spec.ts b/packages/angular/build/src/builders/application/tests/behavior/rebuild-index-html_spec.ts index df9dbc6f0c93..99603bc98cee 100644 --- a/packages/angular/build/src/builders/application/tests/behavior/rebuild-index-html_spec.ts +++ b/packages/angular/build/src/builders/application/tests/behavior/rebuild-index-html_spec.ts @@ -6,7 +6,6 @@ * found in the LICENSE file at https://angular.dev/license */ -import { concatMap, count, take, timeout } from 'rxjs'; import { buildApplication } from '../../index'; import { APPLICATION_BUILDER_INFO, BASE_OPTIONS, describeBuilder } from '../setup'; @@ -29,43 +28,28 @@ describeBuilder(buildApplication, APPLICATION_BUILDER_INFO, (harness) => { watch: true, }); - const buildCount = await harness - .execute({ outputLogsOnFailure: false }) - .pipe( - timeout(30000), - concatMap(async ({ result }, index) => { - switch (index) { - case 0: - expect(result?.success).toBe(true); - harness.expectFile('dist/browser/index.html').content.toContain('charset="utf-8"'); - - await harness.modifyFile('src/index.html', (content) => - content.replace('charset="utf-8"', 'abc'), - ); - break; - case 1: - expect(result?.success).toBe(true); - harness - .expectFile('dist/browser/index.html') - .content.not.toContain('charset="utf-8"'); - - await harness.modifyFile('src/index.html', (content) => - content.replace('abc', 'charset="utf-8"'), - ); - break; - case 2: - expect(result?.success).toBe(true); - harness.expectFile('dist/browser/index.html').content.toContain('charset="utf-8"'); - - break; - } - }), - take(3), - count(), - ) - .toPromise(); - - expect(buildCount).toBe(3); + await harness.executeWithCases([ + async ({ result }) => { + expect(result?.success).toBe(true); + harness.expectFile('dist/browser/index.html').content.toContain('charset="utf-8"'); + + await harness.modifyFile('src/index.html', (content) => + content.replace('charset="utf-8"', 'abc'), + ); + }, + async ({ result }) => { + expect(result?.success).toBe(true); + harness.expectFile('dist/browser/index.html').content.not.toContain('charset="utf-8"'); + + await harness.modifyFile('src/index.html', (content) => + content.replace('abc', 'charset="utf-8"'), + ); + }, + ({ result }) => { + expect(result?.success).toBe(true); + harness.expectFile('dist/browser/index.html').content.toContain('charset="utf-8"'); + }, + ]); }); }); }); diff --git a/packages/angular/build/src/builders/application/tests/behavior/rebuild-web-workers_spec.ts b/packages/angular/build/src/builders/application/tests/behavior/rebuild-web-workers_spec.ts index 421e51f99f5b..4e167f2994c6 100644 --- a/packages/angular/build/src/builders/application/tests/behavior/rebuild-web-workers_spec.ts +++ b/packages/angular/build/src/builders/application/tests/behavior/rebuild-web-workers_spec.ts @@ -7,16 +7,9 @@ */ import { logging } from '@angular-devkit/core'; -import { concatMap, count, take, timeout } from 'rxjs'; import { buildApplication } from '../../index'; import { APPLICATION_BUILDER_INFO, BASE_OPTIONS, describeBuilder } from '../setup'; -/** - * Maximum time in milliseconds for single build/rebuild - * This accounts for CI variability. - */ -export const BUILD_TIMEOUT = 30_000; - /** * A regular expression used to check if a built worker is correctly referenced in application code. */ @@ -56,84 +49,66 @@ describeBuilder(buildApplication, APPLICATION_BUILDER_INFO, (harness) => { `, ); - const buildCount = await harness - .execute({ outputLogsOnFailure: false }) - .pipe( - timeout(BUILD_TIMEOUT), - concatMap(async ({ result, logs }, index) => { - switch (index) { - case 0: - expect(result?.success).toBeTrue(); - - // Ensure built worker is referenced in the application code - harness - .expectFile('dist/browser/main.js') - .content.toMatch(REFERENCED_WORKER_REGEXP); - - // Update the worker file to be invalid syntax - await harness.writeFile('src/app/worker.ts', `asd;fj$3~kls;kd^(*fjlk;sdj---flk`); - - break; - case 1: - expect(result?.success).toBeFalse(); - expect(logs).toContain( - jasmine.objectContaining({ - message: jasmine.stringMatching(errorText), - }), - ); - - // Make an unrelated change to verify error cache was updated - // Should persist error in the next rebuild - await harness.modifyFile('src/main.ts', (content) => content + '\n'); - - break; - case 2: - expect(logs).toContain( - jasmine.objectContaining({ - message: jasmine.stringMatching(errorText), - }), - ); - - // Revert the change that caused the error - // Should remove the error - await harness.writeFile('src/app/worker.ts', workerCodeFile); - - break; - case 3: - expect(result?.success).toBeTrue(); - expect(logs).not.toContain( - jasmine.objectContaining({ - message: jasmine.stringMatching(errorText), - }), - ); - - // Make an unrelated change to verify error cache was updated - // Should continue showing no error - await harness.modifyFile('src/main.ts', (content) => content + '\n'); - - break; - case 4: - expect(result?.success).toBeTrue(); - expect(logs).not.toContain( - jasmine.objectContaining({ - message: jasmine.stringMatching(errorText), - }), - ); - - // Ensure built worker is referenced in the application code - harness - .expectFile('dist/browser/main.js') - .content.toMatch(REFERENCED_WORKER_REGEXP); - - break; - } - }), - take(5), - count(), - ) - .toPromise(); - - expect(buildCount).toBe(5); + await harness.executeWithCases( + [ + async ({ result }) => { + expect(result?.success).toBeTrue(); + + // Ensure built worker is referenced in the application code + harness.expectFile('dist/browser/main.js').content.toMatch(REFERENCED_WORKER_REGEXP); + + // Update the worker file to be invalid syntax + await harness.writeFile('src/app/worker.ts', `asd;fj$3~kls;kd^(*fjlk;sdj---flk`); + }, + async ({ result, logs }) => { + expect(result?.success).toBeFalse(); + expect(logs).toContain( + jasmine.objectContaining({ + message: jasmine.stringMatching(errorText), + }), + ); + + // Make an unrelated change to verify error cache was updated + // Should persist error in the next rebuild + await harness.modifyFile('src/main.ts', (content) => content + '\n'); + }, + async ({ logs }) => { + expect(logs).toContain( + jasmine.objectContaining({ + message: jasmine.stringMatching(errorText), + }), + ); + + // Revert the change that caused the error + // Should remove the error + await harness.writeFile('src/app/worker.ts', workerCodeFile); + }, + async ({ result, logs }) => { + expect(result?.success).toBeTrue(); + expect(logs).not.toContain( + jasmine.objectContaining({ + message: jasmine.stringMatching(errorText), + }), + ); + + // Make an unrelated change to verify error cache was updated + // Should continue showing no error + await harness.modifyFile('src/main.ts', (content) => content + '\n'); + }, + ({ result, logs }) => { + expect(result?.success).toBeTrue(); + expect(logs).not.toContain( + jasmine.objectContaining({ + message: jasmine.stringMatching(errorText), + }), + ); + + // Ensure built worker is referenced in the application code + harness.expectFile('dist/browser/main.js').content.toMatch(REFERENCED_WORKER_REGEXP); + }, + ], + { outputLogsOnFailure: false }, + ); }); }); }); diff --git a/packages/angular/build/src/builders/application/tests/behavior/typescript-rebuild-lazy_spec.ts b/packages/angular/build/src/builders/application/tests/behavior/typescript-rebuild-lazy_spec.ts index c8dd39bfae5d..1f1efafaf3c5 100644 --- a/packages/angular/build/src/builders/application/tests/behavior/typescript-rebuild-lazy_spec.ts +++ b/packages/angular/build/src/builders/application/tests/behavior/typescript-rebuild-lazy_spec.ts @@ -7,7 +7,6 @@ */ import type { logging } from '@angular-devkit/core'; -import { concatMap, count, firstValueFrom, take, timeout } from 'rxjs'; import { buildApplication } from '../../index'; import { OutputHashing } from '../../schema'; import { APPLICATION_BUILDER_INFO, BASE_OPTIONS, describeBuilder } from '../setup'; @@ -42,51 +41,39 @@ describeBuilder(buildApplication, APPLICATION_BUILDER_INFO, (harness) => { ssr: true, }); - const buildCount = await firstValueFrom( - harness.execute({ outputLogsOnFailure: false }).pipe( - timeout(30_000), - concatMap(async ({ result, logs }, index) => { - switch (index) { - case 0: - expect(result?.success).toBeTrue(); + await harness.executeWithCases( + [ + async ({ result }) => { + expect(result?.success).toBeTrue(); - // Add valid code - await harness.appendToFile('src/lazy.ts', `console.log('foo');`); + // Add valid code + await harness.appendToFile('src/lazy.ts', `console.log('foo');`); + }, + async ({ result }) => { + expect(result?.success).toBeTrue(); - break; - case 1: - expect(result?.success).toBeTrue(); + // Update type of 'foo' to invalid (number -> string) + await harness.writeFile('src/lazy.ts', `export const foo: string = 1;`); + }, + async ({ result, logs }) => { + expect(result?.success).toBeFalse(); + expect(logs).toContain( + jasmine.objectContaining({ + message: jasmine.stringMatching( + `Type 'number' is not assignable to type 'string'.`, + ), + }), + ); - // Update type of 'foo' to invalid (number -> string) - await harness.writeFile('src/lazy.ts', `export const foo: string = 1;`); - - break; - case 2: - expect(result?.success).toBeFalse(); - expect(logs).toContain( - jasmine.objectContaining({ - message: jasmine.stringMatching( - `Type 'number' is not assignable to type 'string'.`, - ), - }), - ); - - // Fix TS error - await harness.writeFile('src/lazy.ts', `export const foo: string = "1";`); - - break; - case 3: - expect(result?.success).toBeTrue(); - - break; - } - }), - take(4), - count(), - ), + // Fix TS error + await harness.writeFile('src/lazy.ts', `export const foo: string = "1";`); + }, + ({ result }) => { + expect(result?.success).toBeTrue(); + }, + ], + { outputLogsOnFailure: false }, ); - - expect(buildCount).toBe(4); }); }); }); diff --git a/packages/angular/build/src/builders/application/tests/behavior/typescript-rebuild-touch-file_spec.ts b/packages/angular/build/src/builders/application/tests/behavior/typescript-rebuild-touch-file_spec.ts index 65f0540f2d1b..eeb160ebef47 100644 --- a/packages/angular/build/src/builders/application/tests/behavior/typescript-rebuild-touch-file_spec.ts +++ b/packages/angular/build/src/builders/application/tests/behavior/typescript-rebuild-touch-file_spec.ts @@ -6,7 +6,6 @@ * found in the LICENSE file at https://angular.dev/license */ -import { concatMap, count, take, timeout } from 'rxjs'; import { buildApplication } from '../../index'; import { APPLICATION_BUILDER_INFO, BASE_OPTIONS, describeBuilder } from '../setup'; @@ -20,32 +19,23 @@ describeBuilder(buildApplication, APPLICATION_BUILDER_INFO, (harness) => { aot, }); - const buildCount = await harness - .execute({ outputLogsOnFailure: false }) - .pipe( - timeout(30_000), - concatMap(async ({ result }, index) => { - switch (index) { - case 0: - expect(result?.success).toBeTrue(); - // Touch a file without doing any changes. - await harness.modifyFile('src/app/app.component.ts', (content) => content); - break; - case 1: - expect(result?.success).toBeTrue(); - await harness.removeFile('src/app/app.component.ts'); - break; - case 2: - expect(result?.success).toBeFalse(); - break; - } - }), - take(3), - count(), - ) - .toPromise(); - - expect(buildCount).toBe(3); + await harness.executeWithCases( + [ + async ({ result }) => { + expect(result?.success).toBeTrue(); + // Touch a file without doing any changes. + await harness.modifyFile('src/app/app.component.ts', (content) => content); + }, + async ({ result }) => { + expect(result?.success).toBeTrue(); + await harness.removeFile('src/app/app.component.ts'); + }, + ({ result }) => { + expect(result?.success).toBeFalse(); + }, + ], + { outputLogsOnFailure: false }, + ); }); } }); diff --git a/packages/angular/build/src/builders/application/tests/options/inline-style-language_spec.ts b/packages/angular/build/src/builders/application/tests/options/inline-style-language_spec.ts index 632bc6f1db7b..21a905c792d6 100644 --- a/packages/angular/build/src/builders/application/tests/options/inline-style-language_spec.ts +++ b/packages/angular/build/src/builders/application/tests/options/inline-style-language_spec.ts @@ -6,7 +6,6 @@ * found in the LICENSE file at https://angular.dev/license */ -import { concatMap, count, take, timeout } from 'rxjs'; import { buildApplication } from '../../index'; import { InlineStyleLanguage } from '../../schema'; import { APPLICATION_BUILDER_INFO, BASE_OPTIONS, describeBuilder } from '../setup'; @@ -87,56 +86,38 @@ describeBuilder(buildApplication, APPLICATION_BUILDER_INFO, (harness) => { content.replace('__STYLE_MARKER__', '$primary: indianred;\\nh1 { color: $primary; }'), ); - const buildCount = await harness - .execute() - .pipe( - timeout(30000), - concatMap(async ({ result }, index) => { - expect(result?.success).toBe(true); - - switch (index) { - case 0: - harness - .expectFile('dist/browser/main.js') - .content.toContain('color: indianred'); - harness.expectFile('dist/browser/main.js').content.not.toContain('color: aqua'); - - await harness.modifyFile('src/app/app.component.ts', (content) => - content.replace( - '$primary: indianred;\\nh1 { color: $primary; }', - '$primary: aqua;\\nh1 { color: $primary; }', - ), - ); - break; - case 1: - harness - .expectFile('dist/browser/main.js') - .content.not.toContain('color: indianred'); - harness.expectFile('dist/browser/main.js').content.toContain('color: aqua'); - - await harness.modifyFile('src/app/app.component.ts', (content) => - content.replace( - '$primary: aqua;\\nh1 { color: $primary; }', - '$primary: blue;\\nh1 { color: $primary; }', - ), - ); - break; - case 2: - harness - .expectFile('dist/browser/main.js') - .content.not.toContain('color: indianred'); - harness.expectFile('dist/browser/main.js').content.not.toContain('color: aqua'); - harness.expectFile('dist/browser/main.js').content.toContain('color: blue'); - - break; - } - }), - take(3), - count(), - ) - .toPromise(); - - expect(buildCount).toBe(3); + await harness.executeWithCases([ + async ({ result }) => { + expect(result?.success).toBe(true); + harness.expectFile('dist/browser/main.js').content.toContain('color: indianred'); + harness.expectFile('dist/browser/main.js').content.not.toContain('color: aqua'); + + await harness.modifyFile('src/app/app.component.ts', (content) => + content.replace( + '$primary: indianred;\\nh1 { color: $primary; }', + '$primary: aqua;\\nh1 { color: $primary; }', + ), + ); + }, + async ({ result }) => { + expect(result?.success).toBe(true); + harness.expectFile('dist/browser/main.js').content.not.toContain('color: indianred'); + harness.expectFile('dist/browser/main.js').content.toContain('color: aqua'); + + await harness.modifyFile('src/app/app.component.ts', (content) => + content.replace( + '$primary: aqua;\\nh1 { color: $primary; }', + '$primary: blue;\\nh1 { color: $primary; }', + ), + ); + }, + ({ result }) => { + expect(result?.success).toBe(true); + harness.expectFile('dist/browser/main.js').content.not.toContain('color: indianred'); + harness.expectFile('dist/browser/main.js').content.not.toContain('color: aqua'); + harness.expectFile('dist/browser/main.js').content.toContain('color: blue'); + }, + ]); }); }); } diff --git a/packages/angular/build/src/builders/dev-server/tests/behavior/build-errors_spec.ts b/packages/angular/build/src/builders/dev-server/tests/behavior/build-errors_spec.ts index 82467da0d249..3bf4aa5fed6e 100644 --- a/packages/angular/build/src/builders/dev-server/tests/behavior/build-errors_spec.ts +++ b/packages/angular/build/src/builders/dev-server/tests/behavior/build-errors_spec.ts @@ -6,11 +6,10 @@ * found in the LICENSE file at https://angular.dev/license */ -import { concatMap, count, take, timeout } from 'rxjs'; +import { logging } from '@angular-devkit/core'; import { executeDevServer } from '../../index'; import { describeServeBuilder } from '../jasmine-helpers'; -import { BASE_OPTIONS, BUILD_TIMEOUT, DEV_SERVER_BUILDER_INFO } from '../setup'; -import { logging } from '@angular-devkit/core'; +import { BASE_OPTIONS, DEV_SERVER_BUILDER_INFO } from '../setup'; describeServeBuilder(executeDevServer, DEV_SERVER_BUILDER_INFO, (harness, setupTarget) => { describe('Behavior: "Rebuild Error Detection"', () => { @@ -27,40 +26,30 @@ describeServeBuilder(executeDevServer, DEV_SERVER_BUILDER_INFO, (harness, setupT // Missing ending `>` on the div will cause an error await harness.appendToFile('src/app/app.component.html', '
Hello, world! { - switch (index) { - case 0: - expect(result?.success).toBeFalse(); - debugger; - expect(logs).toContain( - jasmine.objectContaining({ - message: jasmine.stringMatching('Unexpected character "EOF"'), - }), - ); - - await harness.appendToFile('src/app/app.component.html', '>'); - - break; - case 1: - expect(result?.success).toBeTrue(); - expect(logs).not.toContain( - jasmine.objectContaining({ - message: jasmine.stringMatching('Unexpected character "EOF"'), - }), - ); - break; - } - }), - take(2), - count(), - ) - .toPromise(); - - expect(buildCount).toBe(2); + await harness.executeWithCases( + [ + async ({ result, logs }) => { + expect(result?.success).toBeFalse(); + debugger; + expect(logs).toContain( + jasmine.objectContaining({ + message: jasmine.stringMatching('Unexpected character "EOF"'), + }), + ); + + await harness.appendToFile('src/app/app.component.html', '>'); + }, + ({ result, logs }) => { + expect(result?.success).toBeTrue(); + expect(logs).not.toContain( + jasmine.objectContaining({ + message: jasmine.stringMatching('Unexpected character "EOF"'), + }), + ); + }, + ], + { outputLogsOnFailure: false }, + ); }); }); }); diff --git a/packages/angular/build/src/builders/dev-server/tests/behavior/build_localize_replaced_watch_spec.ts b/packages/angular/build/src/builders/dev-server/tests/behavior/build_localize_replaced_watch_spec.ts index 9bc326ebe087..210dc01fc454 100644 --- a/packages/angular/build/src/builders/dev-server/tests/behavior/build_localize_replaced_watch_spec.ts +++ b/packages/angular/build/src/builders/dev-server/tests/behavior/build_localize_replaced_watch_spec.ts @@ -6,10 +6,9 @@ * found in the LICENSE file at https://angular.dev/license */ -import { concatMap, count, take, timeout } from 'rxjs'; import { executeDevServer } from '../../index'; import { describeServeBuilder } from '../jasmine-helpers'; -import { BASE_OPTIONS, BUILD_TIMEOUT, DEV_SERVER_BUILDER_INFO } from '../setup'; +import { BASE_OPTIONS, DEV_SERVER_BUILDER_INFO } from '../setup'; describeServeBuilder(executeDevServer, DEV_SERVER_BUILDER_INFO, (harness, setupTarget) => { describe('Behavior: "i18n $localize calls are replaced during watching"', () => { @@ -45,31 +44,24 @@ describeServeBuilder(executeDevServer, DEV_SERVER_BUILDER_INFO, (harness, setupT `, ); - const buildCount = await harness - .execute() - .pipe( - timeout(BUILD_TIMEOUT * 2), - concatMap(async ({ result }, index) => { - expect(result?.success).toBe(true); + await harness.executeWithCases([ + async ({ result }) => { + expect(result?.success).toBe(true); - const response = await fetch(new URL('main.js', `${result?.baseUrl}`)); - expect(await response?.text()).not.toContain('$localize`:'); + const response = await fetch(new URL('main.js', `${result?.baseUrl}`)); + expect(await response?.text()).not.toContain('$localize`:'); - switch (index) { - case 0: { - await harness.modifyFile('src/app/app.component.html', (content) => - content.replace('introduction', 'intro'), - ); - break; - } - } - }), - take(2), - count(), - ) - .toPromise(); + await harness.modifyFile('src/app/app.component.html', (content) => + content.replace('introduction', 'intro'), + ); + }, + async ({ result }) => { + expect(result?.success).toBe(true); - expect(buildCount).toBe(2); + const response = await fetch(new URL('main.js', `${result?.baseUrl}`)); + expect(await response?.text()).not.toContain('$localize`:'); + }, + ]); }); }); }); diff --git a/packages/angular/build/src/builders/dev-server/tests/behavior/build_translation_watch_spec.ts b/packages/angular/build/src/builders/dev-server/tests/behavior/build_translation_watch_spec.ts index 00c652449db2..b7d65e52e966 100644 --- a/packages/angular/build/src/builders/dev-server/tests/behavior/build_translation_watch_spec.ts +++ b/packages/angular/build/src/builders/dev-server/tests/behavior/build_translation_watch_spec.ts @@ -7,11 +7,10 @@ */ /* eslint-disable max-len */ -import { concatMap, count, take, timeout } from 'rxjs'; import { URL } from 'node:url'; import { executeDevServer } from '../../index'; import { describeServeBuilder } from '../jasmine-helpers'; -import { BASE_OPTIONS, BUILD_TIMEOUT, DEV_SERVER_BUILDER_INFO } from '../setup'; +import { BASE_OPTIONS, DEV_SERVER_BUILDER_INFO } from '../setup'; describeServeBuilder( executeDevServer, @@ -30,7 +29,7 @@ describeServeBuilder( }, i18n: { locales: { - 'fr': 'src/locales/messages.fr.xlf', + fr: 'src/locales/messages.fr.xlf', }, }, }); @@ -53,38 +52,26 @@ describeServeBuilder( await harness.writeFile('src/locales/messages.fr.xlf', TRANSLATION_FILE_CONTENT); - const buildCount = await harness - .execute() - .pipe( - timeout(BUILD_TIMEOUT), - concatMap(async ({ result }, index) => { - expect(result?.success).toBe(true); + await harness.executeWithCases([ + async ({ result }) => { + expect(result?.success).toBe(true); - const mainUrl = new URL('main.js', `${result?.baseUrl}`); + const mainUrl = new URL('main.js', `${result?.baseUrl}`); + const response = await fetch(mainUrl); + expect(await response?.text()).toContain('Bonjour'); - switch (index) { - case 0: { - const response = await fetch(mainUrl); - expect(await response?.text()).toContain('Bonjour'); - - await harness.modifyFile('src/locales/messages.fr.xlf', (content) => - content.replace('Bonjour', 'Salut'), - ); - break; - } - case 1: { - const response = await fetch(mainUrl); - expect(await response?.text()).toContain('Salut'); - break; - } - } - }), - take(2), - count(), - ) - .toPromise(); + await harness.modifyFile('src/locales/messages.fr.xlf', (content) => + content.replace('Bonjour', 'Salut'), + ); + }, + async ({ result }) => { + expect(result?.success).toBe(true); - expect(buildCount).toBe(2); + const mainUrl = new URL('main.js', `${result?.baseUrl}`); + const response = await fetch(mainUrl); + expect(await response?.text()).toContain('Salut'); + }, + ]); }); }); }, diff --git a/packages/angular/build/src/builders/dev-server/tests/behavior/serve-live-reload-proxies_spec.ts b/packages/angular/build/src/builders/dev-server/tests/behavior/serve-live-reload-proxies_spec.ts index 7617e31b45af..083773529058 100644 --- a/packages/angular/build/src/builders/dev-server/tests/behavior/serve-live-reload-proxies_spec.ts +++ b/packages/angular/build/src/builders/dev-server/tests/behavior/serve-live-reload-proxies_spec.ts @@ -12,10 +12,9 @@ import { createServer } from 'node:http'; import { createProxyServer } from 'http-proxy'; import { AddressInfo } from 'node:net'; import puppeteer, { Browser, Page } from 'puppeteer'; -import { count, debounceTime, finalize, switchMap, take, timeout } from 'rxjs'; import { executeDevServer } from '../../index'; import { describeServeBuilder } from '../jasmine-helpers'; -import { BASE_OPTIONS, BUILD_TIMEOUT, DEV_SERVER_BUILDER_INFO } from '../setup'; +import { BASE_OPTIONS, DEV_SERVER_BUILDER_INFO } from '../setup'; // eslint-disable-next-line @typescript-eslint/no-explicit-any declare const document: any; @@ -190,38 +189,24 @@ describeServeBuilder( await harness.writeFile('src/app/app.component.html', '

{{ title }}

'); - const buildCount = await harness - .execute() - .pipe( - debounceTime(1000), - timeout(BUILD_TIMEOUT * 2), - switchMap(async ({ result }, index) => { - expect(result?.success).toBeTrue(); - if (typeof result?.baseUrl !== 'string') { - throw new Error('Expected "baseUrl" to be a string.'); - } - - switch (index) { - case 0: - await goToPageAndWaitForWS(page, result.baseUrl); - await harness.modifyFile('src/app/app.component.ts', (content) => - content.replace(`'app'`, `'app-live-reload'`), - ); - break; - case 1: - const innerText = await page.evaluate( - () => document.querySelector('p').innerText, - ); - expect(innerText).toBe('app-live-reload'); - break; - } - }), - take(2), - count(), - ) - .toPromise(); - - expect(buildCount).toBe(2); + await harness.executeWithCases([ + async ({ result }) => { + expect(result?.success).toBeTrue(); + if (typeof result?.baseUrl !== 'string') { + throw new Error('Expected "baseUrl" to be a string.'); + } + + await goToPageAndWaitForWS(page, result.baseUrl); + await harness.modifyFile('src/app/app.component.ts', (content) => + content.replace(`'app'`, `'app-live-reload'`), + ); + }, + async ({ result }) => { + expect(result?.success).toBeTrue(); + const innerText = await page.evaluate(() => document.querySelector('p').innerText); + expect(innerText).toBe('app-live-reload'); + }, + ]); }); it('works without http -> http proxy', async () => { @@ -232,42 +217,29 @@ describeServeBuilder( await harness.writeFile('src/app/app.component.html', '

{{ title }}

'); let proxy: ProxyInstance | undefined; - const buildCount = await harness - .execute() - .pipe( - debounceTime(1000), - timeout(BUILD_TIMEOUT * 2), - switchMap(async ({ result }, index) => { + try { + await harness.executeWithCases([ + async ({ result }) => { expect(result?.success).toBeTrue(); if (typeof result?.baseUrl !== 'string') { throw new Error('Expected "baseUrl" to be a string.'); } - switch (index) { - case 0: - proxy = await createProxy(result.baseUrl, false); - await goToPageAndWaitForWS(page, proxy.url); - await harness.modifyFile('src/app/app.component.ts', (content) => - content.replace(`'app'`, `'app-live-reload'`), - ); - break; - case 1: - const innerText = await page.evaluate( - () => document.querySelector('p').innerText, - ); - expect(innerText).toBe('app-live-reload'); - break; - } - }), - take(2), - count(), - finalize(() => { - proxy?.server.close(); - }), - ) - .toPromise(); - - expect(buildCount).toBe(2); + proxy = await createProxy(result.baseUrl, false); + await goToPageAndWaitForWS(page, proxy.url); + await harness.modifyFile('src/app/app.component.ts', (content) => + content.replace(`'app'`, `'app-live-reload'`), + ); + }, + async ({ result }) => { + expect(result?.success).toBeTrue(); + const innerText = await page.evaluate(() => document.querySelector('p').innerText); + expect(innerText).toBe('app-live-reload'); + }, + ]); + } finally { + proxy?.server.close(); + } }); it('works without https -> http proxy', async () => { @@ -278,42 +250,29 @@ describeServeBuilder( await harness.writeFile('src/app/app.component.html', '

{{ title }}

'); let proxy: ProxyInstance | undefined; - const buildCount = await harness - .execute() - .pipe( - debounceTime(1000), - timeout(BUILD_TIMEOUT * 2), - switchMap(async ({ result }, index) => { + try { + await harness.executeWithCases([ + async ({ result }) => { expect(result?.success).toBeTrue(); if (typeof result?.baseUrl !== 'string') { throw new Error('Expected "baseUrl" to be a string.'); } - switch (index) { - case 0: - proxy = await createProxy(result.baseUrl, true); - await goToPageAndWaitForWS(page, proxy.url); - await harness.modifyFile('src/app/app.component.ts', (content) => - content.replace(`'app'`, `'app-live-reload'`), - ); - break; - case 1: - const innerText = await page.evaluate( - () => document.querySelector('p').innerText, - ); - expect(innerText).toBe('app-live-reload'); - break; - } - }), - take(2), - count(), - finalize(() => { - proxy?.server.close(); - }), - ) - .toPromise(); - - expect(buildCount).toBe(2); + proxy = await createProxy(result.baseUrl, true); + await goToPageAndWaitForWS(page, proxy.url); + await harness.modifyFile('src/app/app.component.ts', (content) => + content.replace(`'app'`, `'app-live-reload'`), + ); + }, + async ({ result }) => { + expect(result?.success).toBeTrue(); + const innerText = await page.evaluate(() => document.querySelector('p').innerText); + expect(innerText).toBe('app-live-reload'); + }, + ]); + } finally { + proxy?.server.close(); + } }); }, ); diff --git a/packages/angular/build/src/builders/dev-server/tests/behavior/serve_service-worker_spec.ts b/packages/angular/build/src/builders/dev-server/tests/behavior/serve_service-worker_spec.ts index f0a237cae51a..b3b63c3a3093 100644 --- a/packages/angular/build/src/builders/dev-server/tests/behavior/serve_service-worker_spec.ts +++ b/packages/angular/build/src/builders/dev-server/tests/behavior/serve_service-worker_spec.ts @@ -6,11 +6,10 @@ * found in the LICENSE file at https://angular.dev/license */ -import { concatMap, count, take, timeout } from 'rxjs'; import { executeDevServer } from '../../index'; import { executeOnceAndFetch } from '../execute-fetch'; import { describeServeBuilder } from '../jasmine-helpers'; -import { BASE_OPTIONS, BUILD_TIMEOUT, DEV_SERVER_BUILDER_INFO } from '../setup'; +import { BASE_OPTIONS, DEV_SERVER_BUILDER_INFO } from '../setup'; const manifest = { index: '/index.html', @@ -57,7 +56,7 @@ describeServeBuilder( }, i18n: { sourceLocale: { - 'code': 'fr', + code: 'fr', }, }, }); @@ -176,48 +175,40 @@ describeServeBuilder( watch: true, }); - const buildCount = await harness - .execute() - .pipe( - timeout(BUILD_TIMEOUT), - concatMap(async ({ result }, index) => { - expect(result?.success).toBeTrue(); - const response = await fetch(new URL('ngsw.json', `${result?.baseUrl}`)); - const { hashTable } = (await response.json()) as { hashTable: object }; - const hashTableEntries = Object.keys(hashTable); - - switch (index) { - case 0: - expect(hashTableEntries).toEqual([ - '/assets/folder-asset.txt', - '/favicon.ico', - '/index.html', - '/media/spectrum.png', - ]); - - await harness.writeFile( - 'src/assets/folder-new-asset.txt', - harness.readFile('src/assets/folder-asset.txt'), - ); - break; - - case 1: - expect(hashTableEntries).toEqual([ - '/assets/folder-asset.txt', - '/assets/folder-new-asset.txt', - '/favicon.ico', - '/index.html', - '/media/spectrum.png', - ]); - break; - } - }), - take(2), - count(), - ) - .toPromise(); - - expect(buildCount).toBe(2); + await harness.executeWithCases([ + async ({ result }) => { + expect(result?.success).toBeTrue(); + const response = await fetch(new URL('ngsw.json', `${result?.baseUrl}`)); + const { hashTable } = (await response.json()) as { hashTable: object }; + const hashTableEntries = Object.keys(hashTable); + + expect(hashTableEntries).toEqual([ + '/assets/folder-asset.txt', + '/favicon.ico', + '/index.html', + '/media/spectrum.png', + ]); + + await harness.writeFile( + 'src/assets/folder-new-asset.txt', + harness.readFile('src/assets/folder-asset.txt'), + ); + }, + async ({ result }) => { + expect(result?.success).toBeTrue(); + const response = await fetch(new URL('ngsw.json', `${result?.baseUrl}`)); + const { hashTable } = (await response.json()) as { hashTable: object }; + const hashTableEntries = Object.keys(hashTable); + + expect(hashTableEntries).toEqual([ + '/assets/folder-asset.txt', + '/assets/folder-new-asset.txt', + '/favicon.ico', + '/index.html', + '/media/spectrum.png', + ]); + }, + ]); }); }); }, diff --git a/packages/angular/build/src/builders/karma/tests/behavior/rebuilds_spec.ts b/packages/angular/build/src/builders/karma/tests/behavior/rebuilds_spec.ts index 6ec02c2c28f1..a03dbf235982 100644 --- a/packages/angular/build/src/builders/karma/tests/behavior/rebuilds_spec.ts +++ b/packages/angular/build/src/builders/karma/tests/behavior/rebuilds_spec.ts @@ -6,10 +6,8 @@ * found in the LICENSE file at https://angular.dev/license */ -import { concatMap, count, debounceTime, distinctUntilChanged, take, timeout } from 'rxjs'; import { execute } from '../../index'; import { BASE_OPTIONS, KARMA_BUILDER_INFO, describeKarmaBuilder } from '../setup'; -import { BuilderOutput } from '@angular-devkit/architect'; import { randomBytes } from 'node:crypto'; describeKarmaBuilder(execute, KARMA_BUILDER_INFO, (harness, setupTarget) => { @@ -26,48 +24,29 @@ describeKarmaBuilder(execute, KARMA_BUILDER_INFO, (harness, setupTarget) => { const goodFile = await harness.readFile('src/app/app.component.spec.ts'); - interface OutputCheck { - (result: BuilderOutput | undefined): Promise; - } - - const expectedSequence: OutputCheck[] = [ - async (result) => { - // Karma run should succeed. - // Add a compilation error. - expect(result?.success).withContext('Initial test run should succeed').toBeTrue(); - // Add an syntax error to a non-main file. - await harness.appendToFile('src/app/app.component.spec.ts', `error`); - }, - async (result) => { - expect(result?.success) - .withContext('Test should fail after build error was introduced') - .toBeFalse(); - await harness.writeFile('src/app/app.component.spec.ts', goodFile); - }, - async (result) => { - expect(result?.success) - .withContext('Test should succeed again after build error was fixed') - .toBeTrue(); - }, - ]; - - const buildCount = await harness - .execute({ outputLogsOnFailure: false }) - .pipe( - timeout(60000), - debounceTime(500), - // There may be a sequence of {success:true} events that should be - // de-duplicated. - distinctUntilChanged((prev, current) => prev.result?.success === current.result?.success), - concatMap(async ({ result }, index) => { - await expectedSequence[index](result); - }), - take(expectedSequence.length), - count(), - ) - .toPromise(); - - expect(buildCount).toBe(expectedSequence.length); + await harness.executeWithCases( + [ + async ({ result }) => { + // Karma run should succeed. + // Add a compilation error. + expect(result?.success).withContext('Initial test run should succeed').toBeTrue(); + // Add an syntax error to a non-main file. + await harness.appendToFile('src/app/app.component.spec.ts', `error`); + }, + async ({ result }) => { + expect(result?.success) + .withContext('Test should fail after build error was introduced') + .toBeFalse(); + await harness.writeFile('src/app/app.component.spec.ts', goodFile); + }, + ({ result }) => { + expect(result?.success) + .withContext('Test should succeed again after build error was fixed') + .toBeTrue(); + }, + ], + { outputLogsOnFailure: false }, + ); }); it('correctly serves binary assets on rebuilds', async () => { @@ -89,12 +68,8 @@ describeKarmaBuilder(execute, KARMA_BUILDER_INFO, (harness, setupTarget) => { assets: ['src/random.bin'], }); - interface OutputCheck { - (result: BuilderOutput | undefined): Promise; - } - - const expectedSequence: OutputCheck[] = [ - async (result) => { + await harness.executeWithCases([ + async ({ result }) => { // Karma run should succeed. expect(result?.success).withContext('Initial test run should succeed').toBeTrue(); // Modify test file to trigger a rebuild @@ -103,25 +78,10 @@ describeKarmaBuilder(execute, KARMA_BUILDER_INFO, (harness, setupTarget) => { `\n;console.log('modified');`, ); }, - async (result) => { + ({ result }) => { expect(result?.success).withContext('Test should succeed again').toBeTrue(); }, - ]; - - const buildCount = await harness - .execute({ outputLogsOnFailure: true }) - .pipe( - timeout(60000), - debounceTime(500), - concatMap(async ({ result }, index) => { - await expectedSequence[index](result); - }), - take(expectedSequence.length), - count(), - ) - .toPromise(); - - expect(buildCount).toBe(expectedSequence.length); + ]); }); }); }); diff --git a/packages/angular_devkit/build_angular/src/builders/browser/specs/styles_spec.ts b/packages/angular_devkit/build_angular/src/builders/browser/specs/styles_spec.ts index 1c1aaaee202a..b91062b85f4d 100644 --- a/packages/angular_devkit/build_angular/src/builders/browser/specs/styles_spec.ts +++ b/packages/angular_devkit/build_angular/src/builders/browser/specs/styles_spec.ts @@ -492,30 +492,31 @@ describe('Browser Builder styles', () => { await browserBuild(architect, host, target, overrides); }); - it('causes equal failure for tilde and tilde-slash url()', async () => { + it('causes equal failure for tilde url()', async () => { host.writeMultipleFiles({ 'src/styles.css': ` body { - background-image: url('~/does-not-exist.jpg'); + background-image: url('~does-not-exist.jpg'); } `, }); - const overrides = { optimization: true }; - const run = await architect.scheduleTarget(target, overrides); + const run = await architect.scheduleTarget(target, { optimization: true }); await expectAsync(run.result).toBeResolvedTo(jasmine.objectContaining({ success: false })); + await run.stop(); + }); + it('causes equal failure for tilde-slash url()', async () => { host.writeMultipleFiles({ 'src/styles.css': ` body { - background-image: url('~does-not-exist.jpg'); + background-image: url('~/does-not-exist.jpg'); } `, }); - const run2 = await architect.scheduleTarget(target, overrides); - await expectAsync(run2.result).toBeResolvedTo(jasmine.objectContaining({ success: false })); - await run2.stop(); + const run = await architect.scheduleTarget(target, { optimization: true }); + await expectAsync(run.result).toBeResolvedTo(jasmine.objectContaining({ success: false })); await run.stop(); }); @@ -583,9 +584,7 @@ describe('Browser Builder styles', () => { const { files } = await browserBuild(architect, host, target, overrides); expect(await files['styles.css']).toMatch(/\.one(.|\n|\r)*\.two(.|\n|\r)*\.three/); }); - }); - extensionsWithImportSupport.forEach((ext) => { it(`adjusts relative resource URLs when using @import in ${ext} (global)`, async () => { host.copyFile('src/spectrum.png', './src/more-styles/images/global-img-relative.png'); host.writeMultipleFiles({ @@ -659,7 +658,7 @@ describe('Browser Builder styles', () => { result = await browserBuild(architect, host, target, { optimization: true }); expect(await result.files['styles.css']).toContain('rgba(0,0,0,.15)'); - }); + }, 80_000); it('works when using the same css file in `styles` and `stylesUrl`', async () => { host.writeMultipleFiles({ diff --git a/packages/angular_devkit/build_angular/src/builders/browser/tests/behavior/index_watch_spec.ts b/packages/angular_devkit/build_angular/src/builders/browser/tests/behavior/index_watch_spec.ts index 423b1ddf5311..a0b0b7fadb26 100644 --- a/packages/angular_devkit/build_angular/src/builders/browser/tests/behavior/index_watch_spec.ts +++ b/packages/angular_devkit/build_angular/src/builders/browser/tests/behavior/index_watch_spec.ts @@ -6,8 +6,7 @@ * found in the LICENSE file at https://angular.dev/license */ -import { concatMap, count, take, timeout } from 'rxjs'; -import { BUILD_TIMEOUT, buildWebpackBrowser } from '../../index'; +import { buildWebpackBrowser } from '../../index'; import { BASE_OPTIONS, BROWSER_BUILDER_INFO, describeBuilder } from '../setup'; describeBuilder(buildWebpackBrowser, BROWSER_BUILDER_INFO, (harness) => { @@ -18,36 +17,24 @@ describeBuilder(buildWebpackBrowser, BROWSER_BUILDER_INFO, (harness) => { watch: true, }); - const buildCount = await harness - .execute() - .pipe( - timeout(BUILD_TIMEOUT), - concatMap(async ({ result }, index) => { - expect(result?.success).toBe(true); + await harness.executeWithCases([ + async ({ result }) => { + expect(result?.success).toBe(true); - switch (index) { - case 0: { - harness.expectFile('dist/index.html').content.toContain('HelloWorldApp'); - harness.expectFile('dist/index.html').content.not.toContain('UpdatedPageTitle'); + harness.expectFile('dist/index.html').content.toContain('HelloWorldApp'); + harness.expectFile('dist/index.html').content.not.toContain('UpdatedPageTitle'); - // Trigger rebuild - await harness.modifyFile('src/index.html', (s) => - s.replace('HelloWorldApp', 'UpdatedPageTitle'), - ); - break; - } - case 1: { - harness.expectFile('dist/index.html').content.toContain('UpdatedPageTitle'); - break; - } - } - }), - take(2), - count(), - ) - .toPromise(); + // Trigger rebuild + await harness.modifyFile('src/index.html', (s) => + s.replace('HelloWorldApp', 'UpdatedPageTitle'), + ); + }, + ({ result }) => { + expect(result?.success).toBe(true); - expect(buildCount).toBe(2); + harness.expectFile('dist/index.html').content.toContain('UpdatedPageTitle'); + }, + ]); }); }); }); diff --git a/packages/angular_devkit/build_angular/src/builders/browser/tests/behavior/localize_watch_spec.ts b/packages/angular_devkit/build_angular/src/builders/browser/tests/behavior/localize_watch_spec.ts index 5f51ce3c87b4..2c39bf738b3a 100644 --- a/packages/angular_devkit/build_angular/src/builders/browser/tests/behavior/localize_watch_spec.ts +++ b/packages/angular_devkit/build_angular/src/builders/browser/tests/behavior/localize_watch_spec.ts @@ -6,8 +6,7 @@ * found in the LICENSE file at https://angular.dev/license */ -import { concatMap, count, take, timeout } from 'rxjs'; -import { BUILD_TIMEOUT, buildWebpackBrowser } from '../../index'; +import { buildWebpackBrowser } from '../../index'; import { BASE_OPTIONS, BROWSER_BUILDER_INFO, describeBuilder } from '../setup'; describeBuilder(buildWebpackBrowser, BROWSER_BUILDER_INFO, (harness) => { @@ -45,33 +44,19 @@ describeBuilder(buildWebpackBrowser, BROWSER_BUILDER_INFO, (harness) => { await harness.writeFile('src/locales/messages.fr.xlf', TRANSLATION_FILE_CONTENT); - const buildCount = await harness - .execute() - .pipe( - timeout(BUILD_TIMEOUT), - concatMap(async ({ result }, index) => { - expect(result?.success).toBe(true); + await harness.executeWithCases([ + async ({ result }) => { + expect(result?.success).toBe(true); + harness.expectFile('dist/fr/main.js').content.toContain('Bonjour'); - switch (index) { - case 0: { - harness.expectFile('dist/fr/main.js').content.toContain('Bonjour'); - - // Trigger rebuild - await harness.appendToFile('src/app/app.component.html', '\n\n'); - break; - } - case 1: { - harness.expectFile('dist/fr/main.js').content.toContain('Bonjour'); - break; - } - } - }), - take(2), - count(), - ) - .toPromise(); - - expect(buildCount).toBe(2); + // Trigger rebuild + await harness.appendToFile('src/app/app.component.html', '\n\n'); + }, + ({ result }) => { + expect(result?.success).toBe(true); + harness.expectFile('dist/fr/main.js').content.toContain('Bonjour'); + }, + ]); }); }); }); diff --git a/packages/angular_devkit/build_angular/src/builders/browser/tests/behavior/rebuild-errors_spec.ts b/packages/angular_devkit/build_angular/src/builders/browser/tests/behavior/rebuild-errors_spec.ts index ea4501600bab..0daece623c63 100644 --- a/packages/angular_devkit/build_angular/src/builders/browser/tests/behavior/rebuild-errors_spec.ts +++ b/packages/angular_devkit/build_angular/src/builders/browser/tests/behavior/rebuild-errors_spec.ts @@ -7,8 +7,7 @@ */ import { logging } from '@angular-devkit/core'; -import { concatMap, count, take, timeout } from 'rxjs'; -import { BUILD_TIMEOUT, buildWebpackBrowser } from '../../index'; +import { buildWebpackBrowser } from '../../index'; import { BASE_OPTIONS, BROWSER_BUILDER_INFO, describeBuilder } from '../setup'; describeBuilder(buildWebpackBrowser, BROWSER_BUILDER_INFO, (harness) => { @@ -68,85 +67,71 @@ describeBuilder(buildWebpackBrowser, BROWSER_BUILDER_INFO, (harness) => { `, ); - const buildCount = await harness - .execute({ outputLogsOnFailure: false }) - .pipe( - timeout(BUILD_TIMEOUT), - concatMap(async ({ result, logs }, index) => { - switch (index) { - case 0: - expect(result?.success).toBeTrue(); + await harness.executeWithCases( + [ + async ({ result }) => { + expect(result?.success).toBeTrue(); - // Update directive to use a different input type for 'foo' (number -> string) - // Should cause a template error - await harness.writeFile( - 'src/app/dir.ts', - ` + // Update directive to use a different input type for 'foo' (number -> string) + // Should cause a template error + await harness.writeFile( + 'src/app/dir.ts', + ` import { Directive, Input } from '@angular/core'; @Directive({ selector: 'dir', standalone: false }) export class Dir { @Input() foo: string; } `, - ); - - break; - case 1: - expect(result?.success).toBeFalse(); - expect(logs).toContain( - jasmine.objectContaining({ - message: jasmine.stringMatching(typeErrorText), - }), - ); - - // Make an unrelated change to verify error cache was updated - // Should persist error in the next rebuild - await harness.modifyFile('src/main.ts', (content) => content + '\n'); - - break; - case 2: - expect(result?.success).toBeFalse(); - expect(logs).toContain( - jasmine.objectContaining({ - message: jasmine.stringMatching(typeErrorText), - }), - ); - - // Revert the directive change that caused the error - // Should remove the error - await harness.writeFile('src/app/dir.ts', goodDirectiveContents); - - break; - case 3: - expect(result?.success).toBeTrue(); - expect(logs).not.toContain( - jasmine.objectContaining({ - message: jasmine.stringMatching(typeErrorText), - }), - ); - - // Make an unrelated change to verify error cache was updated - // Should continue showing no error - await harness.modifyFile('src/main.ts', (content) => content + '\n'); - - break; - case 4: - expect(result?.success).toBeTrue(); - expect(logs).not.toContain( - jasmine.objectContaining({ - message: jasmine.stringMatching(typeErrorText), - }), - ); - - break; - } - }), - take(5), - count(), - ) - .toPromise(); - - expect(buildCount).toBe(5); + ); + }, + async ({ result, logs }) => { + expect(result?.success).toBeFalse(); + expect(logs).toContain( + jasmine.objectContaining({ + message: jasmine.stringMatching(typeErrorText), + }), + ); + + // Make an unrelated change to verify error cache was updated + // Should persist error in the next rebuild + await harness.modifyFile('src/main.ts', (content) => content + '\n'); + }, + async ({ result, logs }) => { + expect(result?.success).toBeFalse(); + expect(logs).toContain( + jasmine.objectContaining({ + message: jasmine.stringMatching(typeErrorText), + }), + ); + + // Revert the directive change that caused the error + // Should remove the error + await harness.writeFile('src/app/dir.ts', goodDirectiveContents); + }, + async ({ result, logs }) => { + expect(result?.success).toBeTrue(); + expect(logs).not.toContain( + jasmine.objectContaining({ + message: jasmine.stringMatching(typeErrorText), + }), + ); + + // Make an unrelated change to verify error cache was updated + // Should continue showing no error + await harness.modifyFile('src/main.ts', (content) => content + '\n'); + }, + ({ result, logs }) => { + expect(result?.success).toBeTrue(); + expect(logs).not.toContain( + jasmine.objectContaining({ + message: jasmine.stringMatching(typeErrorText), + }), + ); + }, + ], + { outputLogsOnFailure: false }, + ); }); it('detects template errors with AOT codegen differences', async () => { @@ -218,85 +203,71 @@ describeBuilder(buildWebpackBrowser, BROWSER_BUILDER_INFO, (harness) => { `, ); - const buildCount = await harness - .execute({ outputLogsOnFailure: false }) - .pipe( - timeout(BUILD_TIMEOUT), - concatMap(async ({ result, logs }, index) => { - switch (index) { - case 0: - expect(result?.success).toBeTrue(); + await harness.executeWithCases( + [ + async ({ result }) => { + expect(result?.success).toBeTrue(); - // Update second directive to use string property `foo` as an Input - // Should cause a template error - await harness.writeFile( - 'src/app/dir2.ts', - ` + // Update second directive to use string property `foo` as an Input + // Should cause a template error + await harness.writeFile( + 'src/app/dir2.ts', + ` import { Directive, Input } from '@angular/core'; @Directive({ selector: 'dir', standalone: false }) export class Dir2 { @Input() foo: string; } `, - ); - - break; - case 1: - expect(result?.success).toBeFalse(); - expect(logs).toContain( - jasmine.objectContaining({ - message: jasmine.stringMatching(typeErrorText), - }), - ); - - // Make an unrelated change to verify error cache was updated - // Should persist error in the next rebuild - await harness.modifyFile('src/main.ts', (content) => content + '\n'); - - break; - case 2: - expect(result?.success).toBeFalse(); - expect(logs).toContain( - jasmine.objectContaining({ - message: jasmine.stringMatching(typeErrorText), - }), - ); - - // Revert the directive change that caused the error - // Should remove the error - await harness.writeFile('src/app/dir2.ts', goodDirectiveContents); - - break; - case 3: - expect(result?.success).toBeTrue(); - expect(logs).not.toContain( - jasmine.objectContaining({ - message: jasmine.stringMatching(typeErrorText), - }), - ); - - // Make an unrelated change to verify error cache was updated - // Should continue showing no error - await harness.modifyFile('src/main.ts', (content) => content + '\n'); - - break; - case 4: - expect(result?.success).toBeTrue(); - expect(logs).not.toContain( - jasmine.objectContaining({ - message: jasmine.stringMatching(typeErrorText), - }), - ); - - break; - } - }), - take(5), - count(), - ) - .toPromise(); - - expect(buildCount).toBe(5); + ); + }, + async ({ result, logs }) => { + expect(result?.success).toBeFalse(); + expect(logs).toContain( + jasmine.objectContaining({ + message: jasmine.stringMatching(typeErrorText), + }), + ); + + // Make an unrelated change to verify error cache was updated + // Should persist error in the next rebuild + await harness.modifyFile('src/main.ts', (content) => content + '\n'); + }, + async ({ result, logs }) => { + expect(result?.success).toBeFalse(); + expect(logs).toContain( + jasmine.objectContaining({ + message: jasmine.stringMatching(typeErrorText), + }), + ); + + // Revert the directive change that caused the error + // Should remove the error + await harness.writeFile('src/app/dir2.ts', goodDirectiveContents); + }, + async ({ result, logs }) => { + expect(result?.success).toBeTrue(); + expect(logs).not.toContain( + jasmine.objectContaining({ + message: jasmine.stringMatching(typeErrorText), + }), + ); + + // Make an unrelated change to verify error cache was updated + // Should continue showing no error + await harness.modifyFile('src/main.ts', (content) => content + '\n'); + }, + ({ result, logs }) => { + expect(result?.success).toBeTrue(); + expect(logs).not.toContain( + jasmine.objectContaining({ + message: jasmine.stringMatching(typeErrorText), + }), + ); + }, + ], + { outputLogsOnFailure: false }, + ); }); it('recovers from component stylesheet error', async () => { @@ -306,47 +277,35 @@ describeBuilder(buildWebpackBrowser, BROWSER_BUILDER_INFO, (harness) => { aot: false, }); - const buildCount = await harness - .execute({ outputLogsOnFailure: false }) - .pipe( - timeout(BUILD_TIMEOUT), - concatMap(async ({ result, logs }, index) => { - switch (index) { - case 0: - expect(result?.success).toBeTrue(); - await harness.writeFile('src/app/app.component.css', 'invalid-css-content'); - - break; - case 1: - expect(result?.success).toBeFalse(); - expect(logs).toContain( - jasmine.objectContaining({ - message: jasmine.stringMatching('invalid-css-content'), - }), - ); - - await harness.writeFile('src/app/app.component.css', 'p { color: green }'); - - break; - case 2: - expect(result?.success).toBeTrue(); - expect(logs).not.toContain( - jasmine.objectContaining({ - message: jasmine.stringMatching('invalid-css-content'), - }), - ); - - harness.expectFile('dist/main.js').content.toContain('p { color: green }'); - - break; - } - }), - take(3), - count(), - ) - .toPromise(); - - expect(buildCount).toBe(3); + await harness.executeWithCases( + [ + async ({ result }) => { + expect(result?.success).toBeTrue(); + await harness.writeFile('src/app/app.component.css', 'invalid-css-content'); + }, + async ({ result, logs }) => { + expect(result?.success).toBeFalse(); + expect(logs).toContain( + jasmine.objectContaining({ + message: jasmine.stringMatching('invalid-css-content'), + }), + ); + + await harness.writeFile('src/app/app.component.css', 'p { color: green }'); + }, + ({ result, logs }) => { + expect(result?.success).toBeTrue(); + expect(logs).not.toContain( + jasmine.objectContaining({ + message: jasmine.stringMatching('invalid-css-content'), + }), + ); + + harness.expectFile('dist/main.js').content.toContain('p { color: green }'); + }, + ], + { outputLogsOnFailure: false }, + ); }); }); }); diff --git a/packages/angular_devkit/build_angular/src/builders/browser/tests/options/inline-style-language_spec.ts b/packages/angular_devkit/build_angular/src/builders/browser/tests/options/inline-style-language_spec.ts index 177341814525..eede6f2f8099 100644 --- a/packages/angular_devkit/build_angular/src/builders/browser/tests/options/inline-style-language_spec.ts +++ b/packages/angular_devkit/build_angular/src/builders/browser/tests/options/inline-style-language_spec.ts @@ -6,7 +6,6 @@ * found in the LICENSE file at https://angular.dev/license */ -import { concatMap, count, take, timeout } from 'rxjs'; import { buildWebpackBrowser } from '../../index'; import { InlineStyleLanguage } from '../../schema'; import { BASE_OPTIONS, BROWSER_BUILDER_INFO, describeBuilder } from '../setup'; @@ -88,49 +87,38 @@ describeBuilder(buildWebpackBrowser, BROWSER_BUILDER_INFO, (harness) => { content.replace('__STYLE_MARKER__', '$primary: indianred;\\nh1 { color: $primary; }'), ); - const buildCount = await harness - .execute() - .pipe( - timeout(30000), - concatMap(async ({ result }, index) => { - expect(result?.success).toBe(true); - - switch (index) { - case 0: - harness.expectFile('dist/main.js').content.toContain('color: indianred'); - harness.expectFile('dist/main.js').content.not.toContain('color: aqua'); - - await harness.modifyFile('src/app/app.component.ts', (content) => - content.replace( - '$primary: indianred;\\nh1 { color: $primary; }', - '$primary: aqua;\\nh1 { color: $primary; }', - ), - ); - break; - case 1: - harness.expectFile('dist/main.js').content.not.toContain('color: indianred'); - harness.expectFile('dist/main.js').content.toContain('color: aqua'); - - await harness.modifyFile('src/app/app.component.ts', (content) => - content.replace( - '$primary: aqua;\\nh1 { color: $primary; }', - '$primary: blue;\\nh1 { color: $primary; }', - ), - ); - break; - case 2: - harness.expectFile('dist/main.js').content.not.toContain('color: indianred'); - harness.expectFile('dist/main.js').content.not.toContain('color: aqua'); - harness.expectFile('dist/main.js').content.toContain('color: blue'); - break; - } - }), - take(3), - count(), - ) - .toPromise(); - - expect(buildCount).toBe(3); + await harness.executeWithCases([ + async ({ result }) => { + expect(result?.success).toBe(true); + harness.expectFile('dist/main.js').content.toContain('color: indianred'); + harness.expectFile('dist/main.js').content.not.toContain('color: aqua'); + + await harness.modifyFile('src/app/app.component.ts', (content) => + content.replace( + '$primary: indianred;\\nh1 { color: $primary; }', + '$primary: aqua;\\nh1 { color: $primary; }', + ), + ); + }, + async ({ result }) => { + expect(result?.success).toBe(true); + harness.expectFile('dist/main.js').content.not.toContain('color: indianred'); + harness.expectFile('dist/main.js').content.toContain('color: aqua'); + + await harness.modifyFile('src/app/app.component.ts', (content) => + content.replace( + '$primary: aqua;\\nh1 { color: $primary; }', + '$primary: blue;\\nh1 { color: $primary; }', + ), + ); + }, + ({ result }) => { + expect(result?.success).toBe(true); + harness.expectFile('dist/main.js').content.not.toContain('color: indianred'); + harness.expectFile('dist/main.js').content.not.toContain('color: aqua'); + harness.expectFile('dist/main.js').content.toContain('color: blue'); + }, + ]); }); }); } diff --git a/packages/angular_devkit/build_angular/src/builders/browser/tests/options/verbose_spec.ts b/packages/angular_devkit/build_angular/src/builders/browser/tests/options/verbose_spec.ts index fccda49f8fea..c74af39557ed 100644 --- a/packages/angular_devkit/build_angular/src/builders/browser/tests/options/verbose_spec.ts +++ b/packages/angular_devkit/build_angular/src/builders/browser/tests/options/verbose_spec.ts @@ -7,8 +7,7 @@ */ import { logging } from '@angular-devkit/core'; -import { concatMap, count, take, timeout } from 'rxjs'; -import { BUILD_TIMEOUT, buildWebpackBrowser } from '../../index'; +import { buildWebpackBrowser } from '../../index'; import { BASE_OPTIONS, BROWSER_BUILDER_INFO, describeBuilder } from '../setup'; // The below plugin is only enabled when verbose option is set to true. @@ -73,34 +72,23 @@ describeBuilder(buildWebpackBrowser, BROWSER_BUILDER_INFO, (harness) => { watch: true, }); - await harness - .execute() - .pipe( - timeout(BUILD_TIMEOUT), - concatMap(async ({ result, logs }, index) => { - expect(result?.success).toBeTrue(); - - switch (index) { - case 0: - // Amend file - await harness.appendToFile('/src/main.ts', ' '); - break; - case 1: - expect(logs).toContain( - jasmine.objectContaining({ - message: jasmine.stringMatching( - /angular\.watch-files-logs-plugin\n\s+Modified files:\n.+main\.ts/, - ), - }), - ); - - break; - } - }), - take(2), - count(), - ) - .toPromise(); + await harness.executeWithCases([ + async ({ result }) => { + expect(result?.success).toBeTrue(); + // Amend file + await harness.appendToFile('/src/main.ts', ' '); + }, + ({ result, logs }) => { + expect(result?.success).toBeTrue(); + expect(logs).toContain( + jasmine.objectContaining({ + message: jasmine.stringMatching( + /angular\.watch-files-logs-plugin\n\s+Modified files:\n.+main\.ts/, + ), + }), + ); + }, + ]); }); it('should not include error stacktraces when false', async () => { diff --git a/packages/angular_devkit/build_angular/src/builders/browser/tests/options/watch_spec.ts b/packages/angular_devkit/build_angular/src/builders/browser/tests/options/watch_spec.ts index 5c39195e7009..d61290aa0b7c 100644 --- a/packages/angular_devkit/build_angular/src/builders/browser/tests/options/watch_spec.ts +++ b/packages/angular_devkit/build_angular/src/builders/browser/tests/options/watch_spec.ts @@ -6,7 +6,7 @@ * found in the LICENSE file at https://angular.dev/license */ -import { concatMap, count, take, timeout } from 'rxjs'; +import { timeout } from 'rxjs'; import { buildWebpackBrowser } from '../../index'; import { BASE_OPTIONS, BROWSER_BUILDER_INFO, describeBuilder } from '../setup'; @@ -77,33 +77,21 @@ describeBuilder(buildWebpackBrowser, BROWSER_BUILDER_INFO, (harness) => { watch: true, }); - const buildCount = await harness - .execute() - .pipe( - timeout(30000), - concatMap(async ({ result }, index) => { - expect(result?.success).toBe(true); - - switch (index) { - case 0: - harness.expectFile('dist/main.js').content.not.toContain('abcd1234'); - - await harness.modifyFile( - 'src/main.ts', - (content) => content + 'console.log("abcd1234");', - ); - break; - case 1: - harness.expectFile('dist/main.js').content.toContain('abcd1234'); - break; - } - }), - take(2), - count(), - ) - .toPromise(); + await harness.executeWithCases([ + async ({ result }) => { + expect(result?.success).toBe(true); + harness.expectFile('dist/main.js').content.not.toContain('abcd1234'); - expect(buildCount).toBe(2); + await harness.modifyFile( + 'src/main.ts', + (content) => content + 'console.log("abcd1234");', + ); + }, + ({ result }) => { + expect(result?.success).toBe(true); + harness.expectFile('dist/main.js').content.toContain('abcd1234'); + }, + ]); }); }); }); diff --git a/packages/angular_devkit/build_angular/src/builders/dev-server/tests/behavior/build_localize_replaced_watch_spec.ts b/packages/angular_devkit/build_angular/src/builders/dev-server/tests/behavior/build_localize_replaced_watch_spec.ts index cc68d0d7a189..ee9e88c039c3 100644 --- a/packages/angular_devkit/build_angular/src/builders/dev-server/tests/behavior/build_localize_replaced_watch_spec.ts +++ b/packages/angular_devkit/build_angular/src/builders/dev-server/tests/behavior/build_localize_replaced_watch_spec.ts @@ -7,11 +7,10 @@ */ /* eslint-disable max-len */ -import { concatMap, count, take, timeout } from 'rxjs'; import { URL } from 'node:url'; import { executeDevServer } from '../../index'; import { describeServeBuilder } from '../jasmine-helpers'; -import { BASE_OPTIONS, BUILD_TIMEOUT, DEV_SERVER_BUILDER_INFO } from '../setup'; +import { BASE_OPTIONS, DEV_SERVER_BUILDER_INFO } from '../setup'; describeServeBuilder( executeDevServer, @@ -53,31 +52,24 @@ describeServeBuilder( `, ); - const buildCount = await harness - .execute() - .pipe( - timeout(BUILD_TIMEOUT * 2), - concatMap(async ({ result }, index) => { - expect(result?.success).toBe(true); + await harness.executeWithCases([ + async ({ result }) => { + expect(result?.success).toBe(true); - const response = await fetch(new URL('main.js', `${result?.baseUrl}`)); - expect(await response?.text()).not.toContain('$localize`:'); + const response = await fetch(new URL('main.js', `${result?.baseUrl}`)); + expect(await response?.text()).not.toContain('$localize`:'); - switch (index) { - case 0: { - await harness.modifyFile('src/app/app.component.html', (content) => - content.replace('introduction', 'intro'), - ); - break; - } - } - }), - take(2), - count(), - ) - .toPromise(); + await harness.modifyFile('src/app/app.component.html', (content) => + content.replace('introduction', 'intro'), + ); + }, + async ({ result }) => { + expect(result?.success).toBe(true); - expect(buildCount).toBe(2); + const response = await fetch(new URL('main.js', `${result?.baseUrl}`)); + expect(await response?.text()).not.toContain('$localize`:'); + }, + ]); }); }, ); diff --git a/packages/angular_devkit/build_angular/src/builders/dev-server/tests/behavior/build_translation_watch_spec.ts b/packages/angular_devkit/build_angular/src/builders/dev-server/tests/behavior/build_translation_watch_spec.ts index 00c652449db2..0fa3104a4914 100644 --- a/packages/angular_devkit/build_angular/src/builders/dev-server/tests/behavior/build_translation_watch_spec.ts +++ b/packages/angular_devkit/build_angular/src/builders/dev-server/tests/behavior/build_translation_watch_spec.ts @@ -7,11 +7,10 @@ */ /* eslint-disable max-len */ -import { concatMap, count, take, timeout } from 'rxjs'; import { URL } from 'node:url'; import { executeDevServer } from '../../index'; import { describeServeBuilder } from '../jasmine-helpers'; -import { BASE_OPTIONS, BUILD_TIMEOUT, DEV_SERVER_BUILDER_INFO } from '../setup'; +import { BASE_OPTIONS, DEV_SERVER_BUILDER_INFO } from '../setup'; describeServeBuilder( executeDevServer, @@ -53,38 +52,26 @@ describeServeBuilder( await harness.writeFile('src/locales/messages.fr.xlf', TRANSLATION_FILE_CONTENT); - const buildCount = await harness - .execute() - .pipe( - timeout(BUILD_TIMEOUT), - concatMap(async ({ result }, index) => { - expect(result?.success).toBe(true); + await harness.executeWithCases([ + async ({ result }) => { + expect(result?.success).toBe(true); - const mainUrl = new URL('main.js', `${result?.baseUrl}`); + const mainUrl = new URL('main.js', `${result?.baseUrl}`); + const response = await fetch(mainUrl); + expect(await response?.text()).toContain('Bonjour'); - switch (index) { - case 0: { - const response = await fetch(mainUrl); - expect(await response?.text()).toContain('Bonjour'); - - await harness.modifyFile('src/locales/messages.fr.xlf', (content) => - content.replace('Bonjour', 'Salut'), - ); - break; - } - case 1: { - const response = await fetch(mainUrl); - expect(await response?.text()).toContain('Salut'); - break; - } - } - }), - take(2), - count(), - ) - .toPromise(); + await harness.modifyFile('src/locales/messages.fr.xlf', (content) => + content.replace('Bonjour', 'Salut'), + ); + }, + async ({ result }) => { + expect(result?.success).toBe(true); - expect(buildCount).toBe(2); + const mainUrl = new URL('main.js', `${result?.baseUrl}`); + const response = await fetch(mainUrl); + expect(await response?.text()).toContain('Salut'); + }, + ]); }); }); }, diff --git a/packages/angular_devkit/build_angular/src/builders/dev-server/tests/behavior/serve-live-reload-proxies_spec.ts b/packages/angular_devkit/build_angular/src/builders/dev-server/tests/behavior/serve-live-reload-proxies_spec.ts index 7617e31b45af..c2a1758f2b5e 100644 --- a/packages/angular_devkit/build_angular/src/builders/dev-server/tests/behavior/serve-live-reload-proxies_spec.ts +++ b/packages/angular_devkit/build_angular/src/builders/dev-server/tests/behavior/serve-live-reload-proxies_spec.ts @@ -11,11 +11,11 @@ import { tags } from '@angular-devkit/core'; import { createServer } from 'node:http'; import { createProxyServer } from 'http-proxy'; import { AddressInfo } from 'node:net'; +import { setTimeout as setTimeoutPromise } from 'node:timers/promises'; import puppeteer, { Browser, Page } from 'puppeteer'; -import { count, debounceTime, finalize, switchMap, take, timeout } from 'rxjs'; import { executeDevServer } from '../../index'; import { describeServeBuilder } from '../jasmine-helpers'; -import { BASE_OPTIONS, BUILD_TIMEOUT, DEV_SERVER_BUILDER_INFO } from '../setup'; +import { BASE_OPTIONS, DEV_SERVER_BUILDER_INFO } from '../setup'; // eslint-disable-next-line @typescript-eslint/no-explicit-any declare const document: any; @@ -190,38 +190,28 @@ describeServeBuilder( await harness.writeFile('src/app/app.component.html', '

{{ title }}

'); - const buildCount = await harness - .execute() - .pipe( - debounceTime(1000), - timeout(BUILD_TIMEOUT * 2), - switchMap(async ({ result }, index) => { - expect(result?.success).toBeTrue(); - if (typeof result?.baseUrl !== 'string') { - throw new Error('Expected "baseUrl" to be a string.'); - } - - switch (index) { - case 0: - await goToPageAndWaitForWS(page, result.baseUrl); - await harness.modifyFile('src/app/app.component.ts', (content) => - content.replace(`'app'`, `'app-live-reload'`), - ); - break; - case 1: - const innerText = await page.evaluate( - () => document.querySelector('p').innerText, - ); - expect(innerText).toBe('app-live-reload'); - break; - } - }), - take(2), - count(), - ) - .toPromise(); - - expect(buildCount).toBe(2); + await harness.executeWithCases([ + async ({ result }) => { + expect(result?.success).toBeTrue(); + if (typeof result?.baseUrl !== 'string') { + throw new Error('Expected "baseUrl" to be a string.'); + } + + await goToPageAndWaitForWS(page, result.baseUrl); + await harness.modifyFile('src/app/app.component.ts', (content) => + content.replace(`'app'`, `'app-live-reload'`), + ); + }, + async ({ result }) => { + expect(result?.success).toBeTrue(); + + // Wait for page to reload. + await setTimeoutPromise(500); + + const innerText = await page.evaluate(() => document.querySelector('p').innerText); + expect(innerText).toBe('app-live-reload'); + }, + ]); }); it('works without http -> http proxy', async () => { @@ -232,42 +222,38 @@ describeServeBuilder( await harness.writeFile('src/app/app.component.html', '

{{ title }}

'); let proxy: ProxyInstance | undefined; - const buildCount = await harness - .execute() - .pipe( - debounceTime(1000), - timeout(BUILD_TIMEOUT * 2), - switchMap(async ({ result }, index) => { - expect(result?.success).toBeTrue(); - if (typeof result?.baseUrl !== 'string') { - throw new Error('Expected "baseUrl" to be a string.'); - } - - switch (index) { - case 0: - proxy = await createProxy(result.baseUrl, false); - await goToPageAndWaitForWS(page, proxy.url); - await harness.modifyFile('src/app/app.component.ts', (content) => - content.replace(`'app'`, `'app-live-reload'`), - ); - break; - case 1: - const innerText = await page.evaluate( - () => document.querySelector('p').innerText, - ); - expect(innerText).toBe('app-live-reload'); - break; - } - }), - take(2), - count(), - finalize(() => { - proxy?.server.close(); - }), - ) - .toPromise(); - - expect(buildCount).toBe(2); + try { + await harness.executeWithCases( + [ + async ({ result }) => { + expect(result?.success).toBeTrue(); + if (typeof result?.baseUrl !== 'string') { + throw new Error('Expected "baseUrl" to be a string.'); + } + + proxy = await createProxy(result.baseUrl, false); + await goToPageAndWaitForWS(page, proxy.url); + await harness.modifyFile('src/app/app.component.ts', (content) => + content.replace(`'app'`, `'app-live-reload'`), + ); + }, + async ({ result }) => { + expect(result?.success).toBeTrue(); + + // Wait for page to reload. + await setTimeoutPromise(500); + + const innerText = await page.evaluate( + () => document.querySelector('p').innerText, + ); + expect(innerText).toBe('app-live-reload'); + }, + ], + { timeout: 50_000 }, + ); + } finally { + proxy?.server.close(); + } }); it('works without https -> http proxy', async () => { @@ -278,42 +264,39 @@ describeServeBuilder( await harness.writeFile('src/app/app.component.html', '

{{ title }}

'); let proxy: ProxyInstance | undefined; - const buildCount = await harness - .execute() - .pipe( - debounceTime(1000), - timeout(BUILD_TIMEOUT * 2), - switchMap(async ({ result }, index) => { - expect(result?.success).toBeTrue(); - if (typeof result?.baseUrl !== 'string') { - throw new Error('Expected "baseUrl" to be a string.'); - } - - switch (index) { - case 0: - proxy = await createProxy(result.baseUrl, true); - await goToPageAndWaitForWS(page, proxy.url); - await harness.modifyFile('src/app/app.component.ts', (content) => - content.replace(`'app'`, `'app-live-reload'`), - ); - break; - case 1: - const innerText = await page.evaluate( - () => document.querySelector('p').innerText, - ); - expect(innerText).toBe('app-live-reload'); - break; - } - }), - take(2), - count(), - finalize(() => { - proxy?.server.close(); - }), - ) - .toPromise(); - - expect(buildCount).toBe(2); + + try { + await harness.executeWithCases( + [ + async ({ result }) => { + expect(result?.success).toBeTrue(); + if (typeof result?.baseUrl !== 'string') { + throw new Error('Expected "baseUrl" to be a string.'); + } + + proxy = await createProxy(result.baseUrl, true); + await goToPageAndWaitForWS(page, proxy.url); + await harness.modifyFile('src/app/app.component.ts', (content) => + content.replace(`'app'`, `'app-live-reload'`), + ); + }, + async ({ result }) => { + expect(result?.success).toBeTrue(); + + // Wait for page to reload. + await setTimeoutPromise(500); + + const innerText = await page.evaluate( + () => document.querySelector('p').innerText, + ); + expect(innerText).toBe('app-live-reload'); + }, + ], + { timeout: 50_000 }, + ); + } finally { + proxy?.server.close(); + } }); }, ); diff --git a/packages/angular_devkit/build_angular/src/builders/dev-server/tests/behavior/serve_service-worker_spec.ts b/packages/angular_devkit/build_angular/src/builders/dev-server/tests/behavior/serve_service-worker_spec.ts index 2d90b2ead76d..556bbef930f5 100644 --- a/packages/angular_devkit/build_angular/src/builders/dev-server/tests/behavior/serve_service-worker_spec.ts +++ b/packages/angular_devkit/build_angular/src/builders/dev-server/tests/behavior/serve_service-worker_spec.ts @@ -6,11 +6,10 @@ * found in the LICENSE file at https://angular.dev/license */ -import { concatMap, count, take, timeout } from 'rxjs'; import { executeDevServer } from '../../index'; import { executeOnceAndFetch } from '../execute-fetch'; import { describeServeBuilder } from '../jasmine-helpers'; -import { BASE_OPTIONS, BUILD_TIMEOUT, DEV_SERVER_BUILDER_INFO } from '../setup'; +import { BASE_OPTIONS, DEV_SERVER_BUILDER_INFO } from '../setup'; const manifest = { index: '/index.html', @@ -179,48 +178,40 @@ describeServeBuilder( watch: true, }); - const buildCount = await harness - .execute() - .pipe( - timeout(BUILD_TIMEOUT), - concatMap(async ({ result }, index) => { - expect(result?.success).toBeTrue(); - const response = await fetch(new URL('ngsw.json', `${result?.baseUrl}`)); - const { hashTable } = (await response.json()) as { hashTable: object }; - const hashTableEntries = Object.keys(hashTable); - - switch (index) { - case 0: - expect(hashTableEntries).toEqual([ - '/assets/folder-asset.txt', - '/favicon.ico', - '/index.html', - '/media/spectrum.png', - ]); - - await harness.writeFile( - 'src/assets/folder-new-asset.txt', - harness.readFile('src/assets/folder-asset.txt'), - ); - break; - - case 1: - expect(hashTableEntries).toEqual([ - '/assets/folder-asset.txt', - '/assets/folder-new-asset.txt', - '/favicon.ico', - '/index.html', - '/media/spectrum.png', - ]); - break; - } - }), - take(2), - count(), - ) - .toPromise(); - - expect(buildCount).toBe(2); + await harness.executeWithCases([ + async ({ result }) => { + expect(result?.success).toBeTrue(); + const response = await fetch(new URL('ngsw.json', `${result?.baseUrl}`)); + const { hashTable } = (await response.json()) as { hashTable: object }; + const hashTableEntries = Object.keys(hashTable); + + expect(hashTableEntries).toEqual([ + '/assets/folder-asset.txt', + '/favicon.ico', + '/index.html', + '/media/spectrum.png', + ]); + + await harness.writeFile( + 'src/assets/folder-new-asset.txt', + harness.readFile('src/assets/folder-asset.txt'), + ); + }, + async ({ result }) => { + expect(result?.success).toBeTrue(); + const response = await fetch(new URL('ngsw.json', `${result?.baseUrl}`)); + const { hashTable } = (await response.json()) as { hashTable: object }; + const hashTableEntries = Object.keys(hashTable); + + expect(hashTableEntries).toEqual([ + '/assets/folder-asset.txt', + '/assets/folder-new-asset.txt', + '/favicon.ico', + '/index.html', + '/media/spectrum.png', + ]); + }, + ]); }); }); }, diff --git a/packages/angular_devkit/build_angular/src/builders/dev-server/tests/options/watch_spec.ts b/packages/angular_devkit/build_angular/src/builders/dev-server/tests/options/watch_spec.ts index e09ea21a58c5..a22856aaffdf 100644 --- a/packages/angular_devkit/build_angular/src/builders/dev-server/tests/options/watch_spec.ts +++ b/packages/angular_devkit/build_angular/src/builders/dev-server/tests/options/watch_spec.ts @@ -6,10 +6,10 @@ * found in the LICENSE file at https://angular.dev/license */ -import { TimeoutError, concatMap, count, take, timeout } from 'rxjs'; +import { TimeoutError } from 'rxjs'; import { executeDevServer } from '../../index'; import { describeServeBuilder } from '../jasmine-helpers'; -import { BASE_OPTIONS, BUILD_TIMEOUT, DEV_SERVER_BUILDER_INFO } from '../setup'; +import { BASE_OPTIONS, DEV_SERVER_BUILDER_INFO } from '../setup'; describeServeBuilder(executeDevServer, DEV_SERVER_BUILDER_INFO, (harness, setupTarget) => { describe('Option: "watch"', () => { @@ -24,32 +24,28 @@ describeServeBuilder(executeDevServer, DEV_SERVER_BUILDER_INFO, (harness, setupT }); await harness - .execute() - .pipe( - timeout(BUILD_TIMEOUT), - concatMap(async ({ result }, index) => { - expect(result?.success).toBe(true); + .executeWithCases( + [ + async ({ result }) => { + expect(result?.success).toBeTrue(); - switch (index) { - case 0: - await harness.modifyFile( - 'src/main.ts', - (content) => content + 'console.log("abcd1234");', - ); - break; - case 1: - fail('Expected files to not be watched.'); - break; - } - }), - take(2), + await harness.modifyFile( + 'src/main.ts', + (content) => content + 'console.log("abcd1234");', + ); + }, + () => { + fail('Expected files to not be watched.'); + }, + ], + { timeout: 25_000 }, ) - .toPromise() .catch((error) => { // Timeout is expected if watching is disabled if (error instanceof TimeoutError) { return; } + throw error; }); }); @@ -60,30 +56,19 @@ describeServeBuilder(executeDevServer, DEV_SERVER_BUILDER_INFO, (harness, setupT watch: undefined, }); - const buildCount = await harness - .execute() - .pipe( - timeout(BUILD_TIMEOUT), - concatMap(async ({ result }, index) => { - expect(result?.success).toBe(true); - - switch (index) { - case 0: - await harness.modifyFile( - 'src/main.ts', - (content) => content + 'console.log("abcd1234");', - ); - break; - case 1: - break; - } - }), - take(2), - count(), - ) - .toPromise(); + await harness.executeWithCases([ + async ({ result }) => { + expect(result?.success).toBe(true); - expect(buildCount).toBe(2); + await harness.modifyFile( + 'src/main.ts', + (content) => content + 'console.log("abcd1234");', + ); + }, + ({ result }) => { + expect(result?.success).toBe(true); + }, + ]); }); it('watches for file changes when true', async () => { @@ -92,30 +77,19 @@ describeServeBuilder(executeDevServer, DEV_SERVER_BUILDER_INFO, (harness, setupT watch: true, }); - const buildCount = await harness - .execute() - .pipe( - timeout(BUILD_TIMEOUT), - concatMap(async ({ result }, index) => { - expect(result?.success).toBe(true); - - switch (index) { - case 0: - await harness.modifyFile( - 'src/main.ts', - (content) => content + 'console.log("abcd1234");', - ); - break; - case 1: - break; - } - }), - take(2), - count(), - ) - .toPromise(); + await harness.executeWithCases([ + async ({ result }) => { + expect(result?.success).toBe(true); - expect(buildCount).toBe(2); + await harness.modifyFile( + 'src/main.ts', + (content) => content + 'console.log("abcd1234");', + ); + }, + ({ result }) => { + expect(result?.success).toBe(true); + }, + ]); }); }); }); diff --git a/packages/angular_devkit/build_angular/src/builders/dev-server/tests/setup.ts b/packages/angular_devkit/build_angular/src/builders/dev-server/tests/setup.ts index 1ca7202347ab..f92d3b713c9f 100644 --- a/packages/angular_devkit/build_angular/src/builders/dev-server/tests/setup.ts +++ b/packages/angular_devkit/build_angular/src/builders/dev-server/tests/setup.ts @@ -67,12 +67,6 @@ export const BASE_OPTIONS = Object.freeze({ watch: false, }); -/** - * Maximum time for single build/rebuild - * This accounts for CI variability. - */ -export const BUILD_TIMEOUT = 25_000; - /** * Cached browser builder option schema */ diff --git a/packages/angular_devkit/build_angular/src/builders/karma/tests/behavior/rebuilds_spec.ts b/packages/angular_devkit/build_angular/src/builders/karma/tests/behavior/rebuilds_spec.ts index e740b7adfcd6..ad9a0b432555 100644 --- a/packages/angular_devkit/build_angular/src/builders/karma/tests/behavior/rebuilds_spec.ts +++ b/packages/angular_devkit/build_angular/src/builders/karma/tests/behavior/rebuilds_spec.ts @@ -6,10 +6,8 @@ * found in the LICENSE file at https://angular.dev/license */ -import { concatMap, count, debounceTime, distinctUntilChanged, take, timeout } from 'rxjs'; import { execute } from '../../index'; import { BASE_OPTIONS, KARMA_BUILDER_INFO, describeKarmaBuilder } from '../setup'; -import { BuilderOutput } from '@angular-devkit/architect'; describeKarmaBuilder(execute, KARMA_BUILDER_INFO, (harness, setupTarget) => { describe('Behavior: "Rebuilds"', () => { @@ -25,48 +23,29 @@ describeKarmaBuilder(execute, KARMA_BUILDER_INFO, (harness, setupTarget) => { const goodFile = await harness.readFile('src/app/app.component.spec.ts'); - interface OutputCheck { - (result: BuilderOutput | undefined): Promise; - } - - const expectedSequence: OutputCheck[] = [ - async (result) => { - // Karma run should succeed. - // Add a compilation error. - expect(result?.success).withContext('Initial test run should succeed').toBeTrue(); - // Add an syntax error to a non-main file. - await harness.appendToFile('src/app/app.component.spec.ts', `error`); - }, - async (result) => { - expect(result?.success) - .withContext('Test should fail after build error was introduced') - .toBeFalse(); - await harness.writeFile('src/app/app.component.spec.ts', goodFile); - }, - async (result) => { - expect(result?.success) - .withContext('Test should succeed again after build error was fixed') - .toBeTrue(); - }, - ]; - - const buildCount = await harness - .execute({ outputLogsOnFailure: false }) - .pipe( - timeout(60000), - debounceTime(500), - // There may be a sequence of {success:true} events that should be - // de-duplicated. - distinctUntilChanged((prev, current) => prev.result?.success === current.result?.success), - concatMap(async ({ result }, index) => { - await expectedSequence[index](result); - }), - take(expectedSequence.length), - count(), - ) - .toPromise(); - - expect(buildCount).toBe(expectedSequence.length); + await harness.executeWithCases( + [ + async ({ result }) => { + // Karma run should succeed. + // Add a compilation error. + expect(result?.success).withContext('Initial test run should succeed').toBeTrue(); + // Add an syntax error to a non-main file. + await harness.appendToFile('src/app/app.component.spec.ts', `error`); + }, + async ({ result }) => { + expect(result?.success) + .withContext('Test should fail after build error was introduced') + .toBeFalse(); + await harness.writeFile('src/app/app.component.spec.ts', goodFile); + }, + async ({ result }) => { + expect(result?.success) + .withContext('Test should succeed again after build error was fixed') + .toBeTrue(); + }, + ], + { outputLogsOnFailure: false }, + ); }); }); });