Skip to content

Commit 48a278e

Browse files
kyliauclydin
authored andcommitted
fix(@angular-devkit/build-angular): identify plugin provided as string
Karma plugins could be provided either a string or object, but our detection logic for code coverage plugin only covers the string case. Fix #19571
1 parent d368bfd commit 48a278e

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

packages/angular_devkit/build_angular/src/karma/code-coverage_spec.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,4 +169,19 @@ describe('Karma Builder code coverage', () => {
169169
await run.stop();
170170

171171
}, 120000);
172+
173+
it('is able to process coverage plugin provided as string', async () => {
174+
host.replaceInFile('karma.conf.js', /plugins: \[.+?\]/s, `plugins: [
175+
require('karma-jasmine'),
176+
require('karma-chrome-launcher'),
177+
require('karma-jasmine-html-reporter'),
178+
require('@angular-devkit/build-angular/plugins/karma'),
179+
'karma-coverage', // instead of require('karma-coverage')
180+
]`);
181+
const run = await architect.scheduleTarget(karmaTargetSpec, { codeCoverage: true });
182+
183+
const {success} = await run.result;
184+
expect(success).toBe(true);
185+
await run.stop();
186+
}, 120000);
172187
});

packages/angular_devkit/build_angular/src/webpack/plugins/karma.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ const init: any = (config: any, emitter: any, customFileHandlers: any) => {
9393
config.plugins = config.plugins || [];
9494
config.reporters = config.reporters || [];
9595
const {plugins, reporters} = config;
96-
const hasCoveragePlugin = plugins.some((p: {}) => 'reporter:coverage' in p);
97-
const hasIstanbulPlugin = plugins.some((p: {}) => 'reporter:coverage-istanbul' in p);
96+
const hasCoveragePlugin = plugins.some(isPlugin('karma-coverage', 'reporter:coverage'));
97+
const hasIstanbulPlugin = plugins.some(isPlugin('karma-coverage-istanbul-reporter', 'reporter:coverage-istanbul'));
9898
const hasCoverageReporter = reporters.includes('coverage');
9999
const hasIstanbulReporter = reporters.includes('coverage-istanbul');
100100
if (hasCoveragePlugin && !hasCoverageReporter) {
@@ -334,6 +334,22 @@ function fallbackMiddleware() {
334334
};
335335
}
336336

337+
/**
338+
* Returns a function that returns true if the plugin identifier matches the
339+
* `moduleId` or `pluginName`. A plugin identifier can be either a string or
340+
* an object according to https://karma-runner.github.io/5.2/config/plugins.html
341+
* @param moduleId name of the node module (e.g. karma-coverage)
342+
* @param pluginName name of the karma plugin (e.g. reporter:coverage)
343+
*/
344+
function isPlugin(moduleId: string, pluginName: string) {
345+
return (plugin: string|{}): boolean => {
346+
if (typeof plugin === 'string') {
347+
return plugin === moduleId;
348+
}
349+
return pluginName in plugin;
350+
}
351+
}
352+
337353
module.exports = {
338354
'framework:@angular-devkit/build-angular': ['factory', init],
339355
'reporter:@angular-devkit/build-angular--sourcemap-reporter': ['type', sourceMapReporter],

0 commit comments

Comments
 (0)