Skip to content

Commit 13b636d

Browse files
committed
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 725ea16 commit 13b636d

File tree

12 files changed

+55
-138
lines changed

12 files changed

+55
-138
lines changed

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

Lines changed: 0 additions & 44 deletions
This file was deleted.

packages/schematics/angular/application/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,6 @@ function addAppToWorkspaceFile(
213213
main: `${sourceRoot}/test.ts`,
214214
polyfills: `${sourceRoot}/polyfills.ts`,
215215
tsConfig: `${projectRoot}tsconfig.spec.json`,
216-
karmaConfig: `${projectRoot}karma.conf.js`,
217216
inlineStyleLanguage,
218217
assets: [`${sourceRoot}/favicon.ico`, `${sourceRoot}/assets`],
219218
styles: [`${sourceRoot}/styles.${options.style}`],
@@ -231,7 +230,7 @@ function addAppToWorkspaceFile(
231230
});
232231
}
233232
function minimalPathFilter(path: string): boolean {
234-
const toRemoveList = /(test.ts|tsconfig.spec.json|karma.conf.js).template$/;
233+
const toRemoveList = /(test.ts|tsconfig.spec.json).template$/;
235234

236235
return !toRemoveList.test(path);
237236
}

packages/schematics/angular/application/index_spec.ts

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ describe('Application Schematic', () => {
5252
const files = tree.files;
5353
expect(files).toEqual(
5454
jasmine.arrayContaining([
55-
'/projects/foo/karma.conf.js',
5655
'/projects/foo/tsconfig.app.json',
5756
'/projects/foo/tsconfig.spec.json',
5857
'/projects/foo/src/environments/environment.ts',
@@ -170,14 +169,6 @@ describe('Application Schematic', () => {
170169
expect(_extends).toBe('../../tsconfig.json');
171170
});
172171

173-
it('should set the right coverage folder in the karma.json file', async () => {
174-
const tree = await schematicRunner
175-
.runSchematicAsync('application', defaultOptions, workspaceTree)
176-
.toPromise();
177-
const karmaConf = getFileContent(tree, '/projects/foo/karma.conf.js');
178-
expect(karmaConf).toContain(`dir: require('path').join(__dirname, '../../coverage/foo')`);
179-
});
180-
181172
it('should set the skipTests flag for other schematics when using --skipTests=true', async () => {
182173
const options: ApplicationOptions = { ...defaultOptions, skipTests: true };
183174
const tree = await schematicRunner
@@ -255,7 +246,6 @@ describe('Application Schematic', () => {
255246
const files = tree.files;
256247
[
257248
'/projects/foo/tsconfig.spec.json',
258-
'/projects/foo/karma.conf.js',
259249
'/projects/foo/src/test.ts',
260250
'/projects/foo/src/app/app.component.css',
261251
'/projects/foo/src/app/app.component.html',
@@ -286,7 +276,6 @@ describe('Application Schematic', () => {
286276
const files = tree.files;
287277
[
288278
'/projects/foo/tsconfig.spec.json',
289-
'/projects/foo/karma.conf.js',
290279
'/projects/foo/src/test.ts',
291280
'/projects/foo/src/app/app.component.html',
292281
'/projects/foo/src/app/app.component.spec.ts',
@@ -317,7 +306,6 @@ describe('Application Schematic', () => {
317306
const files = tree.files;
318307
[
319308
'/projects/foo/tsconfig.spec.json',
320-
'/projects/foo/karma.conf.js',
321309
'/projects/foo/src/test.ts',
322310
'/projects/foo/src/app/app.component.css',
323311
'/projects/foo/src/app/app.component.spec.ts',
@@ -405,7 +393,6 @@ describe('Application Schematic', () => {
405393
const files = tree.files;
406394
expect(files).toEqual(
407395
jasmine.arrayContaining([
408-
'/karma.conf.js',
409396
'/tsconfig.app.json',
410397
'/tsconfig.spec.json',
411398
'/src/environments/environment.ts',
@@ -443,7 +430,7 @@ describe('Application Schematic', () => {
443430
const testOpt = prj.architect.test.options;
444431
expect(testOpt.main).toEqual('src/test.ts');
445432
expect(testOpt.tsConfig).toEqual('tsconfig.spec.json');
446-
expect(testOpt.karmaConfig).toEqual('karma.conf.js');
433+
expect(testOpt.karmaConfig).toBeUndefined();
447434
expect(testOpt.styles).toEqual(['src/styles.css']);
448435
});
449436

@@ -596,10 +583,5 @@ describe('Application Schematic', () => {
596583

597584
const cfg = JSON.parse(tree.readContent('/angular.json'));
598585
expect(cfg.projects['@myscope/myapp']).toBeDefined();
599-
600-
const karmaConf = getFileContent(tree, '/projects/myscope/myapp/karma.conf.js');
601-
expect(karmaConf).toContain(
602-
`dir: require('path').join(__dirname, '../../../coverage/myscope/myapp')`,
603-
);
604586
});
605587
});

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

Lines changed: 0 additions & 44 deletions
This file was deleted.

packages/schematics/angular/library/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,6 @@ function addLibToWorkspaceFile(
110110
options: {
111111
main: `${projectRoot}/src/test.ts`,
112112
tsConfig: `${projectRoot}/tsconfig.spec.json`,
113-
karmaConfig: `${projectRoot}/karma.conf.js`,
114113
},
115114
},
116115
},

packages/schematics/angular/library/index_spec.ts

Lines changed: 0 additions & 14 deletions
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',
@@ -311,11 +310,6 @@ describe('Library Schematic', () => {
311310

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

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

343-
it(`should set coverage folder to "coverage/foo"`, async () => {
344-
const tree = await schematicRunner
345-
.runSchematicAsync('library', defaultOptions, workspaceTree)
346-
.toPromise();
347-
const karmaConf = getFileContent(tree, '/projects/foo/karma.conf.js');
348-
expect(karmaConf).toContain(`dir: require('path').join(__dirname, '../../coverage/foo')`);
349-
});
350-
351337
it(`should create correct paths when 'newProjectRoot' is blank`, async () => {
352338
const workspaceTree = await schematicRunner
353339
.runSchematicAsync('workspace', { ...workspaceOptions, newProjectRoot: '' })
Lines changed: 37 additions & 5 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 2 deletions
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/project.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ export async function prepareProjectForE2e(name: string) {
8484
await runWebdriverUpdate();
8585
}
8686

87-
await useCIChrome('e2e');
88-
await useCIChrome('');
87+
await useCIChrome(name, 'e2e');
88+
await useCIChrome(name, '');
8989
await useCIDefaults(name);
9090

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

162-
export function useCIDefaults(projectName = 'test-project') {
162+
export function useCIDefaults(projectName = 'test-project'): Promise<void> {
163163
return updateJsonFile('angular.json', (workspaceJson) => {
164164
// Disable progress reporting on CI to reduce spam.
165165
const project = workspaceJson.projects[projectName];
@@ -181,11 +181,12 @@ export function useCIDefaults(projectName = 'test-project') {
181181
});
182182
}
183183

184-
export async function useCIChrome(projectDir: string = ''): Promise<void> {
184+
export async function useCIChrome(projectName: string, projectDir = ''): Promise<void> {
185185
const protractorConf = path.join(projectDir, 'protractor.conf.js');
186186
const karmaConf = path.join(projectDir, 'karma.conf.js');
187187

188188
const chromePath = require('puppeteer').executablePath();
189+
process.env.CHROME_BIN = chromePath;
189190

190191
// Use Puppeteer in protractor if a config is found on the project.
191192
if (fs.existsSync(protractorConf)) {
@@ -218,6 +219,12 @@ export async function useCIChrome(projectDir: string = ''): Promise<void> {
218219
if (fs.existsSync(karmaConf)) {
219220
await prependToFile(karmaConf, `process.env.CHROME_BIN = String.raw\`${chromePath}\`;`);
220221
await replaceInFile(karmaConf, `browsers: ['Chrome']`, `browsers: ['ChromeHeadless']`);
222+
} else {
223+
return updateJsonFile('angular.json', (workspaceJson) => {
224+
const project = workspaceJson.projects[projectName];
225+
const appTargets = project.targets || project.architect;
226+
appTargets.test.options.browsers = 'ChromeHeadless';
227+
});
221228
}
222229
}
223230

0 commit comments

Comments
 (0)