Skip to content

Commit

Permalink
feat(@schematics/angular): remove karma.conf.js from newly generate…
Browse files Browse the repository at this point in the history
…d 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.
  • Loading branch information
alan-agius4 committed Sep 28, 2022
1 parent 2b60292 commit b6897db
Show file tree
Hide file tree
Showing 14 changed files with 60 additions and 147 deletions.

This file was deleted.

5 changes: 1 addition & 4 deletions packages/schematics/angular/application/index.ts
Expand Up @@ -206,7 +206,6 @@ function addAppToWorkspaceFile(
options: {
polyfills: ['zone.js', 'zone.js/testing'],
tsConfig: `${projectRoot}tsconfig.spec.json`,
karmaConfig: `${projectRoot}karma.conf.js`,
inlineStyleLanguage,
assets: [`${sourceRoot}/favicon.ico`, `${sourceRoot}/assets`],
styles: [`${sourceRoot}/styles.${options.style}`],
Expand All @@ -224,9 +223,7 @@ function addAppToWorkspaceFile(
});
}
function minimalPathFilter(path: string): boolean {
const toRemoveList = /(tsconfig.spec.json|karma.conf.js).template$/;

return !toRemoveList.test(path);
return !path.endsWith('tsconfig.spec.json.template');
}

export default function (options: ApplicationOptions): Rule {
Expand Down
21 changes: 3 additions & 18 deletions packages/schematics/angular/application/index_spec.ts
Expand Up @@ -9,7 +9,6 @@
import { SchematicTestRunner, UnitTestTree } from '@angular-devkit/schematics/testing';
import { parse as parseJson } from 'jsonc-parser';
import { latestVersions } from '../utility/latest-versions';
import { getFileContent } from '../utility/test';
import { Schema as WorkspaceOptions } from '../workspace/schema';
import { Schema as ApplicationOptions, Style, ViewEncapsulation } from './schema';

Expand Down Expand Up @@ -52,7 +51,6 @@ describe('Application Schematic', () => {
const files = tree.files;
expect(files).toEqual(
jasmine.arrayContaining([
'/projects/foo/karma.conf.js',
'/projects/foo/tsconfig.app.json',
'/projects/foo/tsconfig.spec.json',
'/projects/foo/src/favicon.ico',
Expand Down Expand Up @@ -165,14 +163,6 @@ describe('Application Schematic', () => {
expect(_extends).toBe('../../tsconfig.json');
});

it('should set the right coverage folder in the karma.json file', async () => {
const tree = await schematicRunner
.runSchematicAsync('application', defaultOptions, workspaceTree)
.toPromise();
const karmaConf = getFileContent(tree, '/projects/foo/karma.conf.js');
expect(karmaConf).toContain(`dir: require('path').join(__dirname, '../../coverage/foo')`);
});

it('should set the skipTests flag for other schematics when using --skipTests=true', async () => {
const options: ApplicationOptions = { ...defaultOptions, skipTests: true };
const tree = await schematicRunner
Expand Down Expand Up @@ -250,7 +240,6 @@ describe('Application Schematic', () => {
const files = tree.files;
[
'/projects/foo/tsconfig.spec.json',
'/projects/foo/karma.conf.js',
'/projects/foo/src/app/app.component.css',
'/projects/foo/src/app/app.component.html',
'/projects/foo/src/app/app.component.spec.ts',
Expand Down Expand Up @@ -278,6 +267,7 @@ describe('Application Schematic', () => {
[
'/projects/foo/tsconfig.spec.json',
'/projects/foo/karma.conf.js',
'/projects/foo/src/test.ts',
'/projects/foo/src/app/app.component.html',
'/projects/foo/src/app/app.component.spec.ts',
].forEach((x) => expect(files).not.toContain(x));
Expand Down Expand Up @@ -305,6 +295,7 @@ describe('Application Schematic', () => {
[
'/projects/foo/tsconfig.spec.json',
'/projects/foo/karma.conf.js',
'/projects/foo/src/test.ts',
'/projects/foo/src/app/app.component.css',
'/projects/foo/src/app/app.component.spec.ts',
].forEach((x) => expect(files).not.toContain(x));
Expand Down Expand Up @@ -388,7 +379,6 @@ describe('Application Schematic', () => {
const files = tree.files;
expect(files).toEqual(
jasmine.arrayContaining([
'/karma.conf.js',
'/tsconfig.app.json',
'/tsconfig.spec.json',
'/src/favicon.ico',
Expand Down Expand Up @@ -421,7 +411,7 @@ describe('Application Schematic', () => {

const testOpt = prj.architect.test.options;
expect(testOpt.tsConfig).toEqual('tsconfig.spec.json');
expect(testOpt.karmaConfig).toEqual('karma.conf.js');
expect(testOpt.karmaConfig).toBeUndefined();
expect(testOpt.styles).toEqual(['src/styles.css']);
});

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

const cfg = JSON.parse(tree.readContent('/angular.json'));
expect(cfg.projects['@myscope/myapp']).toBeDefined();

const karmaConf = getFileContent(tree, '/projects/myscope/myapp/karma.conf.js');
expect(karmaConf).toContain(
`dir: require('path').join(__dirname, '../../../coverage/myscope/myapp')`,
);
});
});
44 changes: 0 additions & 44 deletions packages/schematics/angular/library/files/karma.conf.js.template

This file was deleted.

1 change: 0 additions & 1 deletion packages/schematics/angular/library/index.ts
Expand Up @@ -110,7 +110,6 @@ function addLibToWorkspaceFile(
options: {
tsConfig: `${projectRoot}/tsconfig.spec.json`,
polyfills: ['zone.js', 'zone.js/testing'],
karmaConfig: `${projectRoot}/karma.conf.js`,
},
},
},
Expand Down
14 changes: 0 additions & 14 deletions packages/schematics/angular/library/index_spec.ts
Expand Up @@ -51,7 +51,6 @@ describe('Library Schematic', () => {
const files = tree.files;
expect(files).toEqual(
jasmine.arrayContaining([
'/projects/foo/karma.conf.js',
'/projects/foo/ng-package.json',
'/projects/foo/package.json',
'/projects/foo/README.md',
Expand Down Expand Up @@ -310,11 +309,6 @@ describe('Library Schematic', () => {

const rootTsCfg = getJsonFileContent(tree, '/tsconfig.json');
expect(rootTsCfg.compilerOptions.paths['@myscope/mylib']).toEqual(['dist/myscope/mylib']);

const karmaConf = getFileContent(tree, '/projects/myscope/mylib/karma.conf.js');
expect(karmaConf).toContain(
`dir: require('path').join(__dirname, '../../../coverage/myscope/mylib')`,
);
});

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

it(`should set coverage folder to "coverage/foo"`, async () => {
const tree = await schematicRunner
.runSchematicAsync('library', defaultOptions, workspaceTree)
.toPromise();
const karmaConf = getFileContent(tree, '/projects/foo/karma.conf.js');
expect(karmaConf).toContain(`dir: require('path').join(__dirname, '../../coverage/foo')`);
});

it(`should create correct paths when 'newProjectRoot' is blank`, async () => {
const workspaceTree = await schematicRunner
.runSchematicAsync('workspace', { ...workspaceOptions, newProjectRoot: '' })
Expand Down
42 changes: 37 additions & 5 deletions tests/legacy-cli/e2e/tests/basic/test.ts
@@ -1,9 +1,41 @@
import { ng } from '../../utils/process';
import { moveFile } from '../../utils/fs';
import { writeMultipleFiles } from '../../utils/fs';

export default function () {
export default async function () {
// make sure both --watch=false work
return ng('test', '--watch=false')
.then(() => moveFile('./karma.conf.js', './karma.conf.bis.js'))
.then(() => ng('test', '--watch=false', '--karma-config=karma.conf.bis.js'));
await ng('test', '--watch=false');

// Works with custom config
await writeMultipleFiles({
'./karma.conf.bis.js': `
// Karma configuration file, see link for more information
// https://karma-runner.github.io/1.0/config/configuration-file.html
module.exports = function (config) {
config.set({
basePath: '',
frameworks: ['jasmine', '@angular-devkit/build-angular'],
plugins: [
require('karma-jasmine'),
require('karma-chrome-launcher'),
require('karma-jasmine-html-reporter'),
require('karma-coverage'),
require('@angular-devkit/build-angular/plugins/karma')
],
client: {
clearContext: false // leave Jasmine Spec Runner output visible in browser
},
reporters: ['progress', 'kjhtml'],
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: true,
browsers: ['ChromeHeadless'],
singleRun: false,
restartOnFileChange: true
});
};
`,
});

await ng('test', '--watch=false', '--karma-config=karma.conf.bis.js');
}
Expand Up @@ -5,6 +5,6 @@ import { useCIChrome } from '../../../utils/project';
export default function () {
return ng('generate', 'application', 'app2')
.then(() => expectFileToMatch('angular.json', /\"app2\":/))
.then(() => useCIChrome('projects/app2'))
.then(() => useCIChrome('app2', 'projects/app2'))
.then(() => ng('test', 'app2', '--watch=false'));
}
Expand Up @@ -21,7 +21,7 @@ export default function () {
)
.then(() => ng('generate', 'application', 'app-two', '--skip-install'))
.then(() => useCIDefaults('app-two'))
.then(() => useCIChrome('./projects/app-two'))
.then(() => useCIChrome('app-two', './projects/app-two'))
.then(() =>
updateJsonFile('angular.json', (configJson) => {
configJson.projects['test-project'].schematics = {
Expand Down
2 changes: 1 addition & 1 deletion tests/legacy-cli/e2e/tests/misc/cli-exit-interop.ts
Expand Up @@ -26,7 +26,7 @@ export default async function () {
// Simulates issue https://github.com/angular/angular-cli/issues/23289
await replaceInFile('angular.json', /styles\.css/g, 'styles.scss');

await useCIChrome();
await useCIChrome('thirteen-project');
await useCIDefaults('thirteen-project');
await noSilentNg('test', '--watch=false');
} finally {
Expand Down
4 changes: 2 additions & 2 deletions tests/legacy-cli/e2e/tests/update/update.ts
Expand Up @@ -72,8 +72,8 @@ export default async function () {
await ng('update', '@angular/cli', ...extraUpdateArgs);

// Setup testing to use CI Chrome.
await useCIChrome('./');
await useCIChrome('./e2e/');
await useCIChrome('twelve-project', './');
await useCIChrome('twelve-project', './e2e/');
await useCIDefaults('twelve-project');

// Run CLI commands.
Expand Down
2 changes: 1 addition & 1 deletion tests/legacy-cli/e2e/utils/process.ts
Expand Up @@ -167,7 +167,7 @@ export function extractNpmEnv() {

function extractCIEnv(): NodeJS.ProcessEnv {
return Object.keys(process.env)
.filter((v) => v.startsWith('SAUCE_') || v === 'CI' || v === 'CIRCLECI')
.filter((v) => v.startsWith('SAUCE_') || v === 'CI' || v === 'CIRCLECI' || v === 'CHROME_BIN')
.reduce<NodeJS.ProcessEnv>((vars, n) => {
vars[n] = process.env[n];
return vars;
Expand Down
21 changes: 10 additions & 11 deletions tests/legacy-cli/e2e/utils/project.ts
Expand Up @@ -83,8 +83,8 @@ export async function prepareProjectForE2e(name: string) {
await runWebdriverUpdate();
}

await useCIChrome('e2e');
await useCIChrome('');
await useCIChrome(name, 'e2e');
await useCIChrome(name, '');
await useCIDefaults(name);

// Force sourcemaps to be from the root of the filesystem.
Expand Down Expand Up @@ -160,7 +160,7 @@ export function useSha() {
}
}

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

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

const chromePath = require('puppeteer').executablePath();

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

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

export function getNgCLIVersion(): SemVer {
Expand Down
3 changes: 3 additions & 0 deletions tests/legacy-cli/e2e_runner.ts
Expand Up @@ -170,6 +170,9 @@ console.log(['Tests:', ...testsToRun].join('\n '));

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

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

0 comments on commit b6897db

Please sign in to comment.