Skip to content

Commit b6897db

Browse files
committedSep 28, 2022
feat(@schematics/angular): remove karma.conf.js from newly generated projects
`@angular-devkit/build-angular` now has a built in Karma config. Users can still create their own Karma configuration if they want to override the default configuration.
1 parent 2b60292 commit b6897db

File tree

14 files changed

+60
-147
lines changed

14 files changed

+60
-147
lines changed
 

‎packages/schematics/angular/application/files/karma.conf.js.template

-44
This file was deleted.

‎packages/schematics/angular/application/index.ts

+1-4
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,6 @@ function addAppToWorkspaceFile(
206206
options: {
207207
polyfills: ['zone.js', 'zone.js/testing'],
208208
tsConfig: `${projectRoot}tsconfig.spec.json`,
209-
karmaConfig: `${projectRoot}karma.conf.js`,
210209
inlineStyleLanguage,
211210
assets: [`${sourceRoot}/favicon.ico`, `${sourceRoot}/assets`],
212211
styles: [`${sourceRoot}/styles.${options.style}`],
@@ -224,9 +223,7 @@ function addAppToWorkspaceFile(
224223
});
225224
}
226225
function minimalPathFilter(path: string): boolean {
227-
const toRemoveList = /(tsconfig.spec.json|karma.conf.js).template$/;
228-
229-
return !toRemoveList.test(path);
226+
return !path.endsWith('tsconfig.spec.json.template');
230227
}
231228

232229
export default function (options: ApplicationOptions): Rule {

‎packages/schematics/angular/application/index_spec.ts

+3-18
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import { SchematicTestRunner, UnitTestTree } from '@angular-devkit/schematics/testing';
1010
import { parse as parseJson } from 'jsonc-parser';
1111
import { latestVersions } from '../utility/latest-versions';
12-
import { getFileContent } from '../utility/test';
1312
import { Schema as WorkspaceOptions } from '../workspace/schema';
1413
import { Schema as ApplicationOptions, Style, ViewEncapsulation } from './schema';
1514

@@ -52,7 +51,6 @@ describe('Application Schematic', () => {
5251
const files = tree.files;
5352
expect(files).toEqual(
5453
jasmine.arrayContaining([
55-
'/projects/foo/karma.conf.js',
5654
'/projects/foo/tsconfig.app.json',
5755
'/projects/foo/tsconfig.spec.json',
5856
'/projects/foo/src/favicon.ico',
@@ -165,14 +163,6 @@ describe('Application Schematic', () => {
165163
expect(_extends).toBe('../../tsconfig.json');
166164
});
167165

168-
it('should set the right coverage folder in the karma.json file', async () => {
169-
const tree = await schematicRunner
170-
.runSchematicAsync('application', defaultOptions, workspaceTree)
171-
.toPromise();
172-
const karmaConf = getFileContent(tree, '/projects/foo/karma.conf.js');
173-
expect(karmaConf).toContain(`dir: require('path').join(__dirname, '../../coverage/foo')`);
174-
});
175-
176166
it('should set the skipTests flag for other schematics when using --skipTests=true', async () => {
177167
const options: ApplicationOptions = { ...defaultOptions, skipTests: true };
178168
const tree = await schematicRunner
@@ -250,7 +240,6 @@ describe('Application Schematic', () => {
250240
const files = tree.files;
251241
[
252242
'/projects/foo/tsconfig.spec.json',
253-
'/projects/foo/karma.conf.js',
254243
'/projects/foo/src/app/app.component.css',
255244
'/projects/foo/src/app/app.component.html',
256245
'/projects/foo/src/app/app.component.spec.ts',
@@ -278,6 +267,7 @@ describe('Application Schematic', () => {
278267
[
279268
'/projects/foo/tsconfig.spec.json',
280269
'/projects/foo/karma.conf.js',
270+
'/projects/foo/src/test.ts',
281271
'/projects/foo/src/app/app.component.html',
282272
'/projects/foo/src/app/app.component.spec.ts',
283273
].forEach((x) => expect(files).not.toContain(x));
@@ -305,6 +295,7 @@ describe('Application Schematic', () => {
305295
[
306296
'/projects/foo/tsconfig.spec.json',
307297
'/projects/foo/karma.conf.js',
298+
'/projects/foo/src/test.ts',
308299
'/projects/foo/src/app/app.component.css',
309300
'/projects/foo/src/app/app.component.spec.ts',
310301
].forEach((x) => expect(files).not.toContain(x));
@@ -388,7 +379,6 @@ describe('Application Schematic', () => {
388379
const files = tree.files;
389380
expect(files).toEqual(
390381
jasmine.arrayContaining([
391-
'/karma.conf.js',
392382
'/tsconfig.app.json',
393383
'/tsconfig.spec.json',
394384
'/src/favicon.ico',
@@ -421,7 +411,7 @@ describe('Application Schematic', () => {
421411

422412
const testOpt = prj.architect.test.options;
423413
expect(testOpt.tsConfig).toEqual('tsconfig.spec.json');
424-
expect(testOpt.karmaConfig).toEqual('karma.conf.js');
414+
expect(testOpt.karmaConfig).toBeUndefined();
425415
expect(testOpt.styles).toEqual(['src/styles.css']);
426416
});
427417

@@ -574,10 +564,5 @@ describe('Application Schematic', () => {
574564

575565
const cfg = JSON.parse(tree.readContent('/angular.json'));
576566
expect(cfg.projects['@myscope/myapp']).toBeDefined();
577-
578-
const karmaConf = getFileContent(tree, '/projects/myscope/myapp/karma.conf.js');
579-
expect(karmaConf).toContain(
580-
`dir: require('path').join(__dirname, '../../../coverage/myscope/myapp')`,
581-
);
582567
});
583568
});

‎packages/schematics/angular/library/files/karma.conf.js.template

-44
This file was deleted.

‎packages/schematics/angular/library/index.ts

-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,6 @@ function addLibToWorkspaceFile(
110110
options: {
111111
tsConfig: `${projectRoot}/tsconfig.spec.json`,
112112
polyfills: ['zone.js', 'zone.js/testing'],
113-
karmaConfig: `${projectRoot}/karma.conf.js`,
114113
},
115114
},
116115
},

‎packages/schematics/angular/library/index_spec.ts

-14
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ describe('Library Schematic', () => {
5151
const files = tree.files;
5252
expect(files).toEqual(
5353
jasmine.arrayContaining([
54-
'/projects/foo/karma.conf.js',
5554
'/projects/foo/ng-package.json',
5655
'/projects/foo/package.json',
5756
'/projects/foo/README.md',
@@ -310,11 +309,6 @@ describe('Library Schematic', () => {
310309

311310
const rootTsCfg = getJsonFileContent(tree, '/tsconfig.json');
312311
expect(rootTsCfg.compilerOptions.paths['@myscope/mylib']).toEqual(['dist/myscope/mylib']);
313-
314-
const karmaConf = getFileContent(tree, '/projects/myscope/mylib/karma.conf.js');
315-
expect(karmaConf).toContain(
316-
`dir: require('path').join(__dirname, '../../../coverage/myscope/mylib')`,
317-
);
318312
});
319313

320314
it(`should dasherize scoped libraries`, async () => {
@@ -339,14 +333,6 @@ describe('Library Schematic', () => {
339333
expect(cfg.projects['@myScope/myLib']).toBeDefined();
340334
});
341335

342-
it(`should set coverage folder to "coverage/foo"`, async () => {
343-
const tree = await schematicRunner
344-
.runSchematicAsync('library', defaultOptions, workspaceTree)
345-
.toPromise();
346-
const karmaConf = getFileContent(tree, '/projects/foo/karma.conf.js');
347-
expect(karmaConf).toContain(`dir: require('path').join(__dirname, '../../coverage/foo')`);
348-
});
349-
350336
it(`should create correct paths when 'newProjectRoot' is blank`, async () => {
351337
const workspaceTree = await schematicRunner
352338
.runSchematicAsync('workspace', { ...workspaceOptions, newProjectRoot: '' })
+37-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,41 @@
11
import { ng } from '../../utils/process';
2-
import { moveFile } from '../../utils/fs';
2+
import { writeMultipleFiles } from '../../utils/fs';
33

4-
export default function () {
4+
export default async function () {
55
// make sure both --watch=false work
6-
return ng('test', '--watch=false')
7-
.then(() => moveFile('./karma.conf.js', './karma.conf.bis.js'))
8-
.then(() => ng('test', '--watch=false', '--karma-config=karma.conf.bis.js'));
6+
await ng('test', '--watch=false');
7+
8+
// Works with custom config
9+
await writeMultipleFiles({
10+
'./karma.conf.bis.js': `
11+
// Karma configuration file, see link for more information
12+
// https://karma-runner.github.io/1.0/config/configuration-file.html
13+
module.exports = function (config) {
14+
config.set({
15+
basePath: '',
16+
frameworks: ['jasmine', '@angular-devkit/build-angular'],
17+
plugins: [
18+
require('karma-jasmine'),
19+
require('karma-chrome-launcher'),
20+
require('karma-jasmine-html-reporter'),
21+
require('karma-coverage'),
22+
require('@angular-devkit/build-angular/plugins/karma')
23+
],
24+
client: {
25+
clearContext: false // leave Jasmine Spec Runner output visible in browser
26+
},
27+
reporters: ['progress', 'kjhtml'],
28+
port: 9876,
29+
colors: true,
30+
logLevel: config.LOG_INFO,
31+
autoWatch: true,
32+
browsers: ['ChromeHeadless'],
33+
singleRun: false,
34+
restartOnFileChange: true
35+
});
36+
};
37+
`,
38+
});
39+
40+
await ng('test', '--watch=false', '--karma-config=karma.conf.bis.js');
941
}

‎tests/legacy-cli/e2e/tests/generate/application/application-basic.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ import { useCIChrome } from '../../../utils/project';
55
export default function () {
66
return ng('generate', 'application', 'app2')
77
.then(() => expectFileToMatch('angular.json', /\"app2\":/))
8-
.then(() => useCIChrome('projects/app2'))
8+
.then(() => useCIChrome('app2', 'projects/app2'))
99
.then(() => ng('test', 'app2', '--watch=false'));
1010
}

‎tests/legacy-cli/e2e/tests/generate/directive/directive-prefix.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export default function () {
2121
)
2222
.then(() => ng('generate', 'application', 'app-two', '--skip-install'))
2323
.then(() => useCIDefaults('app-two'))
24-
.then(() => useCIChrome('./projects/app-two'))
24+
.then(() => useCIChrome('app-two', './projects/app-two'))
2525
.then(() =>
2626
updateJsonFile('angular.json', (configJson) => {
2727
configJson.projects['test-project'].schematics = {

‎tests/legacy-cli/e2e/tests/misc/cli-exit-interop.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export default async function () {
2626
// Simulates issue https://github.com/angular/angular-cli/issues/23289
2727
await replaceInFile('angular.json', /styles\.css/g, 'styles.scss');
2828

29-
await useCIChrome();
29+
await useCIChrome('thirteen-project');
3030
await useCIDefaults('thirteen-project');
3131
await noSilentNg('test', '--watch=false');
3232
} finally {

‎tests/legacy-cli/e2e/tests/update/update.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ export default async function () {
7272
await ng('update', '@angular/cli', ...extraUpdateArgs);
7373

7474
// Setup testing to use CI Chrome.
75-
await useCIChrome('./');
76-
await useCIChrome('./e2e/');
75+
await useCIChrome('twelve-project', './');
76+
await useCIChrome('twelve-project', './e2e/');
7777
await useCIDefaults('twelve-project');
7878

7979
// Run CLI commands.

‎tests/legacy-cli/e2e/utils/process.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ export function extractNpmEnv() {
167167

168168
function extractCIEnv(): NodeJS.ProcessEnv {
169169
return Object.keys(process.env)
170-
.filter((v) => v.startsWith('SAUCE_') || v === 'CI' || v === 'CIRCLECI')
170+
.filter((v) => v.startsWith('SAUCE_') || v === 'CI' || v === 'CIRCLECI' || v === 'CHROME_BIN')
171171
.reduce<NodeJS.ProcessEnv>((vars, n) => {
172172
vars[n] = process.env[n];
173173
return vars;

‎tests/legacy-cli/e2e/utils/project.ts

+10-11
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ export async function prepareProjectForE2e(name: string) {
8383
await runWebdriverUpdate();
8484
}
8585

86-
await useCIChrome('e2e');
87-
await useCIChrome('');
86+
await useCIChrome(name, 'e2e');
87+
await useCIChrome(name, '');
8888
await useCIDefaults(name);
8989

9090
// Force sourcemaps to be from the root of the filesystem.
@@ -160,7 +160,7 @@ export function useSha() {
160160
}
161161
}
162162

163-
export function useCIDefaults(projectName = 'test-project') {
163+
export function useCIDefaults(projectName = 'test-project'): Promise<void> {
164164
return updateJsonFile('angular.json', (workspaceJson) => {
165165
// Disable progress reporting on CI to reduce spam.
166166
const project = workspaceJson.projects[projectName];
@@ -182,10 +182,8 @@ export function useCIDefaults(projectName = 'test-project') {
182182
});
183183
}
184184

185-
export async function useCIChrome(projectDir: string = ''): Promise<void> {
185+
export async function useCIChrome(projectName: string, projectDir = ''): Promise<void> {
186186
const protractorConf = path.join(projectDir, 'protractor.conf.js');
187-
const karmaConf = path.join(projectDir, 'karma.conf.js');
188-
189187
const chromePath = require('puppeteer').executablePath();
190188

191189
// Use Puppeteer in protractor if a config is found on the project.
@@ -215,11 +213,12 @@ export async function useCIChrome(projectDir: string = ''): Promise<void> {
215213
);
216214
}
217215

218-
// Use Puppeteer in karma if a config is found on the project.
219-
if (fs.existsSync(karmaConf)) {
220-
await prependToFile(karmaConf, `process.env.CHROME_BIN = String.raw\`${chromePath}\`;`);
221-
await replaceInFile(karmaConf, `browsers: ['Chrome']`, `browsers: ['ChromeHeadless']`);
222-
}
216+
// Use ChromeHeadless.
217+
return updateJsonFile('angular.json', (workspaceJson) => {
218+
const project = workspaceJson.projects[projectName];
219+
const appTargets = project.targets || project.architect;
220+
appTargets.test.options.browsers = 'ChromeHeadless';
221+
});
223222
}
224223

225224
export function getNgCLIVersion(): SemVer {

‎tests/legacy-cli/e2e_runner.ts

+3
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,9 @@ console.log(['Tests:', ...testsToRun].join('\n '));
170170

171171
setGlobalVariable('argv', argv);
172172
setGlobalVariable('package-manager', argv.yarn ? 'yarn' : 'npm');
173+
// This is needed by karma-chrome-launcher
174+
// https://github.com/karma-runner/karma-chrome-launcher#headless-chromium-with-puppeteer
175+
process.env['CHROME_BIN'] = require('puppeteer').executablePath();
173176

174177
Promise.all([findFreePort(), findFreePort(), findPackageTars()])
175178
.then(async ([httpPort, httpsPort, packageTars]) => {

0 commit comments

Comments
 (0)
Please sign in to comment.