Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion goldens/public-api/angular_devkit/build_angular/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ export interface KarmaBuilderOptions {
include?: string[];
inlineStyleLanguage?: InlineStyleLanguage_2;
karmaConfig: string;
main: string;
main?: string;
poll?: number;
polyfills?: Polyfills_2;
preserveSymlinks?: boolean;
Expand Down
31 changes: 31 additions & 0 deletions packages/angular_devkit/build_angular/src/builders/karma/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,17 @@ export function execute(
throw new Error('The builder requires a target.');
}

if (!options.main) {
webpackConfig.entry ??= {};
if (typeof webpackConfig.entry === 'object' && !Array.isArray(webpackConfig.entry)) {
if (Array.isArray(webpackConfig.entry['main'])) {
webpackConfig.entry['main'].push(getBuiltInMainFile());
} else {
webpackConfig.entry['main'] = [getBuiltInMainFile()];
}
}
}

const projectMetadata = await context.getProjectMetadata(projectName);
const sourceRoot = (projectMetadata.sourceRoot ?? projectMetadata.root ?? '') as string;

Expand Down Expand Up @@ -169,3 +180,23 @@ export function execute(

export { KarmaBuilderOptions };
export default createBuilder<Record<string, string> & KarmaBuilderOptions>(execute);

function getBuiltInMainFile(): string {
const content = Buffer.from(
`
import { getTestBed } from '@angular/core/testing';
import {
BrowserDynamicTestingModule,
platformBrowserDynamicTesting,
} from '@angular/platform-browser-dynamic/testing';

// Initialize the Angular testing environment.
getTestBed().initTestEnvironment(BrowserDynamicTestingModule, platformBrowserDynamicTesting(), {
errorOnUnknownElements: true,
errorOnUnknownProperties: true
});
`,
).toString('base64');

return `ng-virtual-main.js!=!data:text/javascript;base64,${content}`;
}
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@
}
},
"additionalProperties": false,
"required": ["main", "tsConfig", "karmaConfig"],
"required": ["tsConfig", "karmaConfig"],
"definitions": {
"assetPattern": {
"oneOf": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ export const KARMA_BUILDER_INFO = Object.freeze({
* Also disables progress reporting to minimize logging output.
*/
export const BASE_OPTIONS = Object.freeze<Schema>({
main: 'src/test.ts',
polyfills: 'src/polyfills.ts',
polyfills: ['zone.js', 'zone.js/testing', '@angular/localize/init'],
tsConfig: 'src/tsconfig.spec.json',
karmaConfig: 'karma.conf.js',
browsers: 'ChromeHeadlessCI',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ export async function getCommonConfig(wco: WebpackConfigOptions): Promise<Config
if (Array.isArray(entryPoints['main'])) {
entryPoints['main'].unshift(zoneTestingEntryPoint);
} else {
entryPoints['main'] = [zoneTestingEntryPoint, entryPoints['main'] as string];
entryPoints['main'] = [zoneTestingEntryPoint];
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@
"test": {
"builder": "@angular-devkit/build-angular:karma",
"options": {
"main": "src/test.ts",
"polyfills": "src/polyfills.ts",
"polyfills": ["zone.js", "zone.js/testing", "@angular/localize/init"],

"tsConfig": "src/tsconfig.spec.json",
"karmaConfig": "karma.conf.js",
"browsers": "ChromeHeadlessCI",
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
]
},
"files": [
"test.ts",
"polyfills.ts"
],
"include": [
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
"types": []
},
"exclude": [
"src/test.ts",
"**/*.spec.ts"
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
"jasmine"
]
},
"files": [
"src/test.ts"
],
"include": [
"**/*.spec.ts",
"**/*.d.ts"
Expand Down
12 changes: 0 additions & 12 deletions packages/schematics/angular/application/files/src/test.ts.template

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
"jasmine"
]
},
"files": [
"src/test.ts"
],
"include": [
"src/**/*.spec.ts",
"src/**/*.d.ts"
Expand Down
3 changes: 1 addition & 2 deletions packages/schematics/angular/application/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,6 @@ function addAppToWorkspaceFile(
: {
builder: Builders.Karma,
options: {
main: `${sourceRoot}/test.ts`,
polyfills: ['zone.js', 'zone.js/testing'],
tsConfig: `${projectRoot}tsconfig.spec.json`,
karmaConfig: `${projectRoot}karma.conf.js`,
Expand All @@ -231,7 +230,7 @@ function addAppToWorkspaceFile(
});
}
function minimalPathFilter(path: string): boolean {
const toRemoveList = /(test.ts|tsconfig.spec.json|karma.conf.js).template$/;
const toRemoveList = /(tsconfig.spec.json|karma.conf.js).template$/;

return !toRemoveList.test(path);
}
Expand Down
11 changes: 2 additions & 9 deletions packages/schematics/angular/application/index_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ describe('Application Schematic', () => {
'/projects/foo/src/index.html',
'/projects/foo/src/main.ts',
'/projects/foo/src/styles.css',
'/projects/foo/src/test.ts',
'/projects/foo/src/app/app.module.ts',
'/projects/foo/src/app/app.component.css',
'/projects/foo/src/app/app.component.html',
Expand Down Expand Up @@ -164,8 +163,7 @@ describe('Application Schematic', () => {
const tree = await schematicRunner
.runSchematicAsync('application', defaultOptions, workspaceTree)
.toPromise();
const { files, extends: _extends } = readJsonFile(tree, '/projects/foo/tsconfig.spec.json');
expect(files).toEqual(['src/test.ts']);
const { extends: _extends } = readJsonFile(tree, '/projects/foo/tsconfig.spec.json');
expect(_extends).toBe('../../tsconfig.json');
});

Expand Down Expand Up @@ -255,7 +253,6 @@ 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.html',
'/projects/foo/src/app/app.component.spec.ts',
Expand Down Expand Up @@ -285,7 +282,6 @@ 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 @@ -315,7 +311,6 @@ 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 @@ -410,7 +405,6 @@ describe('Application Schematic', () => {
'/src/index.html',
'/src/main.ts',
'/src/styles.css',
'/src/test.ts',
'/src/app/app.module.ts',
'/src/app/app.component.css',
'/src/app/app.component.html',
Expand All @@ -436,7 +430,6 @@ describe('Application Schematic', () => {
expect(buildOpt.tsConfig).toEqual('tsconfig.app.json');

const testOpt = prj.architect.test.options;
expect(testOpt.main).toEqual('src/test.ts');
expect(testOpt.tsConfig).toEqual('tsconfig.spec.json');
expect(testOpt.karmaConfig).toEqual('karma.conf.js');
expect(testOpt.styles).toEqual(['src/styles.css']);
Expand Down Expand Up @@ -510,13 +503,13 @@ describe('Application Schematic', () => {
expect(appTsConfig.extends).toEqual('./tsconfig.json');
const specTsConfig = readJsonFile(tree, '/tsconfig.spec.json');
expect(specTsConfig.extends).toEqual('./tsconfig.json');
expect(specTsConfig.files).toEqual(['src/test.ts']);
});

it(`should create correct paths when 'newProjectRoot' is blank`, async () => {
const workspaceTree = await schematicRunner
.runSchematicAsync('workspace', { ...workspaceOptions, newProjectRoot: '' })
.toPromise();

const options = { ...defaultOptions, projectRoot: undefined };
const tree = await schematicRunner
.runSchematicAsync('application', options, workspaceTree)
Expand Down
12 changes: 0 additions & 12 deletions packages/schematics/angular/library/files/src/test.ts.template

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
"types": []
},
"exclude": [
"src/test.ts",
"**/*.spec.ts"
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
"jasmine"
]
},
"files": [
"src/test.ts"
],
"include": [
"**/*.spec.ts",
"**/*.d.ts"
Expand Down
1 change: 0 additions & 1 deletion packages/schematics/angular/library/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ function addLibToWorkspaceFile(
test: {
builder: Builders.Karma,
options: {
main: `${projectRoot}/src/test.ts`,
tsConfig: `${projectRoot}/tsconfig.spec.json`,
polyfills: ['zone.js', 'zone.js/testing'],
karmaConfig: `${projectRoot}/karma.conf.js`,
Expand Down
1 change: 0 additions & 1 deletion packages/schematics/angular/library/index_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ describe('Library Schematic', () => {
'/projects/foo/README.md',
'/projects/foo/tsconfig.lib.json',
'/projects/foo/tsconfig.lib.prod.json',
'/projects/foo/src/test.ts',
'/projects/foo/src/my-index.ts',
'/projects/foo/src/lib/foo.module.ts',
'/projects/foo/src/lib/foo.component.spec.ts',
Expand Down