Skip to content

Commit b031a42

Browse files
committed
fix(@schematics/angular): align Karma project generation with unified unit-test builder
Updates the application and ng-new schematics to correctly configure Karma-based projects. - The test target in the generated angular.json now uses the `@angular/build:unit-test` builder. - The builder options are simplified to only include runner: 'karma', as other settings are now handled by the unified builder. - Corresponding schematic tests have been updated to reflect these changes.
1 parent cf47d15 commit b031a42

File tree

10 files changed

+48
-22
lines changed

10 files changed

+48
-22
lines changed

packages/schematics/angular/application/index.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -396,13 +396,9 @@ function addAppToWorkspaceFile(options: ApplicationOptions, appDir: string): Rul
396396
options: {},
397397
}
398398
: {
399-
builder: Builders.BuildKarma,
399+
builder: Builders.BuildUnitTest,
400400
options: {
401-
polyfills: options.zoneless ? undefined : ['zone.js', 'zone.js/testing'],
402-
tsConfig: `${projectRoot}tsconfig.spec.json`,
403-
inlineStyleLanguage,
404-
assets: [{ 'glob': '**/*', 'input': `${projectRoot}public` }],
405-
styles: [`${sourceRoot}/styles.${options.style}`],
401+
runner: 'karma',
406402
},
407403
},
408404
},

packages/schematics/angular/application/index_spec.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -448,10 +448,11 @@ describe('Application Schematic', () => {
448448
const config = JSON.parse(tree.readContent('/angular.json'));
449449
const prj = config.projects.foo;
450450
const testOpt = prj.architect.test;
451-
expect(testOpt.builder).toEqual('@angular/build:karma');
452-
expect(testOpt.options.tsConfig).toEqual('tsconfig.spec.json');
453-
expect(testOpt.options.assets).toEqual([{ glob: '**/*', input: 'public' }]);
454-
expect(testOpt.options.styles).toEqual(['src/styles.css']);
451+
expect(testOpt.builder).toEqual('@angular/build:unit-test');
452+
expect(testOpt.options.runner).toEqual('karma');
453+
expect(testOpt.options.tsConfig).toBeUndefined();
454+
expect(testOpt.options.assets).toBeUndefined();
455+
expect(testOpt.options.styles).toBeUndefined();
455456
});
456457

457458
it('should set the relative tsconfig paths', async () => {

packages/schematics/angular/ng-new/index_spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,8 @@ describe('Ng New Schematic', () => {
169169
},
170170
},
171171
} = JSON.parse(tree.readContent('/bar/angular.json'));
172-
expect(test.builder).toBe('@angular/build:karma');
172+
expect(test.builder).toBe('@angular/build:unit-test');
173+
expect(test.options).toEqual({ runner: 'karma' });
173174

174175
const { devDependencies } = JSON.parse(tree.readContent('/bar/package.json'));
175176
expect(devDependencies['karma']).toBeDefined();

tests/legacy-cli/e2e/initialize/500-create-project.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ export default async function () {
6464

6565
const test = json['projects']['test-project']['architect']['test'];
6666
test.builder = '@angular-devkit/build-angular:karma';
67+
test.options ??= {};
68+
test.options.tsConfig = 'tsconfig.spec.json';
69+
delete test.options.runner;
6770
});
6871
await updateJsonFile('tsconfig.json', (tsconfig) => {
6972
delete tsconfig.compilerOptions.esModuleInterop;

tests/legacy-cli/e2e/tests/basic/test.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { ng } from '../../utils/process';
22
import { writeMultipleFiles } from '../../utils/fs';
3+
import { getGlobalVariable } from '../../utils/env';
34

45
export default async function () {
56
// make sure both --watch=false work
@@ -48,5 +49,11 @@ export default async function () {
4849
`,
4950
});
5051

51-
await ng('test', '--watch=false', '--karma-config=karma.conf.bis.js');
52+
const isWebpack = !getGlobalVariable('argv')['esbuild'];
53+
54+
if (isWebpack) {
55+
await ng('test', '--watch=false', '--karma-config=karma.conf.bis.js');
56+
} else {
57+
await ng('test', '--watch=false', '--runner-config=karma.conf.bis.js');
58+
}
5259
}

tests/legacy-cli/e2e/tests/test/test-code-coverage-exclude.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
1+
import { getGlobalVariable } from '../../utils/env';
12
import { expectFileToExist, rimraf } from '../../utils/fs';
23
import { silentNg } from '../../utils/process';
34
import { expectToFail } from '../../utils/utils';
45

56
export default async function () {
7+
const isWebpack = !getGlobalVariable('argv')['esbuild'];
8+
const coverageOptionName = isWebpack ? '--code-coverage' : '--coverage';
9+
610
// This test is already in build-angular, but that doesn't run on Windows.
7-
await silentNg('test', '--no-watch', '--code-coverage');
11+
await silentNg('test', '--no-watch', coverageOptionName);
812
await expectFileToExist('coverage/test-project/app.ts.html');
913
// Delete coverage directory
1014
await rimraf('coverage');
1115

1216
await silentNg(
1317
'test',
1418
'--no-watch',
15-
'--code-coverage',
16-
`--code-coverage-exclude='src/**/app.ts'`,
19+
coverageOptionName,
20+
`${coverageOptionName}-exclude='src/**/app.ts'`,
1721
);
1822

1923
// Doesn't include excluded.

tests/legacy-cli/e2e/tests/test/test-environment.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export default function () {
2929
.then(() =>
3030
updateJsonFile('angular.json', (configJson) => {
3131
const appArchitect = configJson.projects['test-project'].architect;
32-
appArchitect.test.configurations = {
32+
appArchitect.build.configurations = {
3333
production: {
3434
fileReplacements: [
3535
{
@@ -39,6 +39,8 @@ export default function () {
3939
],
4040
},
4141
};
42+
appArchitect.test.options ??= {};
43+
appArchitect.test.options.buildTarget = '::production';
4244
}),
4345
)
4446

@@ -57,6 +59,6 @@ export default function () {
5759
`,
5860
),
5961
)
60-
.then(() => ng('test', '--configuration=production', '--watch=false'))
62+
.then(() => ng('test', '--watch=false'))
6163
);
6264
}

tests/legacy-cli/e2e/tests/test/test-scripts.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export default async function () {
6262

6363
await updateJsonFile('angular.json', (workspaceJson) => {
6464
const appArchitect = workspaceJson.projects['test-project'].architect;
65-
appArchitect.test.options.scripts = [
65+
appArchitect.build.options.scripts = [
6666
{ input: 'src/string-script.js' },
6767
{ input: 'src/input-script.js' },
6868
];

tests/legacy-cli/e2e/tests/test/test-sourcemap.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import assert from 'node:assert';
2-
import { getGlobalVariable } from '../../utils/env';
32
import { writeFile } from '../../utils/fs';
43
import { ng } from '../../utils/process';
54
import { assertIsError } from '../../utils/utils';
5+
import { updateJsonFile } from '../../utils/project';
66

77
export default async function () {
88
await writeFile(
@@ -15,8 +15,12 @@ export default async function () {
1515
);
1616

1717
// when sourcemaps are 'on' the stacktrace will point to the spec.ts file.
18+
await updateJsonFile('angular.json', (configJson) => {
19+
const appArchitect = configJson.projects['test-project'].architect;
20+
appArchitect.build.configurations.development.sourceMap = true;
21+
});
1822
try {
19-
await ng('test', '--no-watch', '--source-map');
23+
await ng('test', '--no-watch');
2024
throw new Error('ng test should have failed.');
2125
} catch (error) {
2226
assertIsError(error);
@@ -25,8 +29,12 @@ export default async function () {
2529
}
2630

2731
// when sourcemaps are 'off' the stacktrace won't point to the spec.ts file.
32+
await updateJsonFile('angular.json', (configJson) => {
33+
const appArchitect = configJson.projects['test-project'].architect;
34+
appArchitect.build.configurations.development.sourceMap = false;
35+
});
2836
try {
29-
await ng('test', '--no-watch', '--no-source-map');
37+
await ng('test', '--no-watch');
3038
throw new Error('ng test should have failed.');
3139
} catch (error) {
3240
assertIsError(error);

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,11 @@ export async function useCIChrome(projectName: string, projectDir = ''): Promise
191191
return updateJsonFile('angular.json', (workspaceJson) => {
192192
const project = workspaceJson.projects[projectName];
193193
const appTargets = project.targets || project.architect;
194-
appTargets.test.options.browsers = 'ChromeHeadlessNoSandbox';
194+
if (appTargets.test.builder === '@angular/build:unit-test') {
195+
appTargets.test.options.browsers = ['ChromeHeadlessNoSandbox'];
196+
} else {
197+
appTargets.test.options.browsers = 'ChromeHeadlessNoSandbox';
198+
}
195199
});
196200
}
197201

0 commit comments

Comments
 (0)