From b05aaed1deaa00112e2e0f3724aeb9e6b6ac79f5 Mon Sep 17 00:00:00 2001 From: Jan Krems Date: Tue, 5 Nov 2024 08:35:18 -0800 Subject: [PATCH] fix(@angular-devkit/build-angular): remove double-watch in karma The Karma file watching was racing with the file writes done by the application builder. Since we already tell Karma when to reun via `.refeshFiles()`, disabling Karma's own file watcher should make things more reliable. This allows removing a weird special-case in the test case and removes the noisy "File chaned" logs generated by Karma. Fixes https://github.com/angular/angular-cli/issues/28755 --- .../src/builders/karma/application_builder.ts | 17 +++++++++++------ .../karma/tests/behavior/rebuilds_spec.ts | 9 +-------- 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/packages/angular_devkit/build_angular/src/builders/karma/application_builder.ts b/packages/angular_devkit/build_angular/src/builders/karma/application_builder.ts index f82a3b0da1b8..3ac5a360960a 100644 --- a/packages/angular_devkit/build_angular/src/builders/karma/application_builder.ts +++ b/packages/angular_devkit/build_angular/src/builders/karma/application_builder.ts @@ -265,28 +265,33 @@ async function initializeApplication( karmaOptions.files ??= []; karmaOptions.files.push( // Serve polyfills first. - { pattern: `${outputPath}/polyfills.js`, type: 'module' }, + { pattern: `${outputPath}/polyfills.js`, type: 'module', watched: false }, // Serve global setup script. - { pattern: `${outputPath}/${mainName}.js`, type: 'module' }, + { pattern: `${outputPath}/${mainName}.js`, type: 'module', watched: false }, // Serve all source maps. - { pattern: `${outputPath}/*.map`, included: false }, + { pattern: `${outputPath}/*.map`, included: false, watched: false }, ); if (hasChunkOrWorkerFiles(buildOutput.files)) { karmaOptions.files.push( // Allow loading of chunk-* files but don't include them all on load. - { pattern: `${outputPath}/{chunk,worker}-*.js`, type: 'module', included: false }, + { + pattern: `${outputPath}/{chunk,worker}-*.js`, + type: 'module', + included: false, + watched: false, + }, ); } karmaOptions.files.push( // Serve remaining JS on page load, these are the test entrypoints. - { pattern: `${outputPath}/*.js`, type: 'module' }, + { pattern: `${outputPath}/*.js`, type: 'module', watched: false }, ); if (options.styles?.length) { // Serve CSS outputs on page load, these are the global styles. - karmaOptions.files.push({ pattern: `${outputPath}/*.css`, type: 'css' }); + karmaOptions.files.push({ pattern: `${outputPath}/*.css`, type: 'css', watched: false }); } const parsedKarmaConfig: Config & ConfigOptions = await karma.config.parseConfig( 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 a89f2dbd7c7a..2f4ae93cc134 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 @@ -11,7 +11,7 @@ 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, isApplicationBuilder) => { +describeKarmaBuilder(execute, KARMA_BUILDER_INFO, (harness, setupTarget) => { describe('Behavior: "Rebuilds"', () => { beforeEach(async () => { await setupTarget(harness); @@ -45,13 +45,6 @@ describeKarmaBuilder(execute, KARMA_BUILDER_INFO, (harness, setupTarget, isAppli expect(result?.success).toBeTrue(); }, ]; - if (isApplicationBuilder) { - expectedSequence.unshift(async (result) => { - // This is the initial Karma run, it should succeed. - // For simplicity, we trigger a run the first time we build in watch mode. - expect(result?.success).toBeTrue(); - }); - } const buildCount = await harness .execute({ outputLogsOnFailure: false })