Skip to content

Commit

Permalink
fix(@angular-devkit/build-angular): block Karma from starting until b…
Browse files Browse the repository at this point in the history
…uild is complete

This change is inspired by karma-runner/karma-chrome-launcher#154 (comment) which blocks Karma from launching the browsers until the compilation is complete.

This is needed especially for large applications when using code-coverage where otherwise the users would have to fine-tune several Karma timeouts such as  `captureTimeout` for various environments.

Closes #22495

(cherry picked from commit 7ce5000)
  • Loading branch information
alan-agius4 authored and filipesilva committed Feb 9, 2022
1 parent 214aede commit 8c8377f
Showing 1 changed file with 27 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -169,29 +169,42 @@ const init: any = (config: any, emitter: any) => {
compiler.hooks.watchRun.tapAsync('karma', (_: any, callback: () => void) => handler(callback));
compiler.hooks.run.tapAsync('karma', (_: any, callback: () => void) => handler(callback));

webpackMiddleware = webpackDevMiddleware(compiler, webpackMiddlewareConfig);
emitter.on('exit', (done: any) => {
webpackMiddleware.close();
done();
});

function unblock() {
isBlocked = false;
blocked.forEach((cb) => cb());
blocked = [];
}

let lastCompilationHash: string | undefined;
compiler.hooks.done.tap('karma', (stats) => {
if (stats.hasErrors()) {
lastCompilationHash = undefined;
} else if (stats.hash != lastCompilationHash) {
// Refresh karma only when there are no webpack errors, and if the compilation changed.
lastCompilationHash = stats.hash;
emitter.refreshFiles();
}
unblock();
});
let isFirstRun = true;

return new Promise<void>((resolve) => {
compiler.hooks.done.tap('karma', (stats) => {
if (isFirstRun) {
// This is needed to block Karma from launching browsers before Webpack writes the assets in memory.
// See the below:
// https://github.com/karma-runner/karma-chrome-launcher/issues/154#issuecomment-986661937
// https://github.com/angular/angular-cli/issues/22495
isFirstRun = false;
resolve();
}

webpackMiddleware = webpackDevMiddleware(compiler, webpackMiddlewareConfig);
if (stats.hasErrors()) {
lastCompilationHash = undefined;
} else if (stats.hash != lastCompilationHash) {
// Refresh karma only when there are no webpack errors, and if the compilation changed.
lastCompilationHash = stats.hash;
emitter.refreshFiles();
}

emitter.on('exit', (done: any) => {
webpackMiddleware.close();
done();
unblock();
});
});
};

Expand Down

0 comments on commit 8c8377f

Please sign in to comment.