Skip to content

Commit b91fa31

Browse files
clydinalan-agius4
authored andcommitted
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. (cherry picked from commit 430dad5)
1 parent a2a1c19 commit b91fa31

File tree

10 files changed

+98
-52
lines changed

10 files changed

+98
-52
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.
Lines changed: 41 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
import { ng } from '../../utils/process';
22
import { writeFile, writeMultipleFiles } from '../../utils/fs';
33
import { updateJsonFile } from '../../utils/project';
4+
import { getGlobalVariable } from '../../utils/env';
5+
6+
export default async function () {
7+
const isWebpack = !getGlobalVariable('argv')['esbuild'];
48

5-
export default function () {
69
// Tests run in 'dev' environment by default.
7-
return (
8-
writeMultipleFiles({
9-
'src/environment.prod.ts': `
10+
await writeMultipleFiles({
11+
'src/environment.prod.ts': `
1012
export const environment = {
1113
production: true
1214
};`,
13-
'src/environment.ts': `
15+
'src/environment.ts': `
1416
export const environment = {
1517
production: false
1618
};
1719
`,
18-
'src/app/environment.spec.ts': `
20+
'src/app/environment.spec.ts': `
1921
import { environment } from '../environment';
2022
2123
describe('Test environment', () => {
@@ -24,29 +26,33 @@ export default function () {
2426
});
2527
});
2628
`,
27-
})
28-
.then(() => ng('test', '--watch=false'))
29-
.then(() =>
30-
updateJsonFile('angular.json', (configJson) => {
31-
const appArchitect = configJson.projects['test-project'].architect;
32-
appArchitect.test.configurations = {
33-
production: {
34-
fileReplacements: [
35-
{
36-
replace: 'src/environment.ts',
37-
with: 'src/environment.prod.ts',
38-
},
39-
],
40-
},
41-
};
42-
}),
43-
)
44-
45-
// Tests can run in different environment.
46-
.then(() =>
47-
writeFile(
48-
'src/app/environment.spec.ts',
49-
`
29+
});
30+
31+
await ng('test', '--watch=false');
32+
33+
await updateJsonFile('angular.json', (configJson) => {
34+
const appArchitect = configJson.projects['test-project'].architect;
35+
appArchitect[isWebpack ? 'test' : 'build'].configurations = {
36+
production: {
37+
fileReplacements: [
38+
{
39+
replace: 'src/environment.ts',
40+
with: 'src/environment.prod.ts',
41+
},
42+
],
43+
},
44+
};
45+
if (!isWebpack) {
46+
appArchitect.test.options ??= {};
47+
appArchitect.test.options.buildTarget = '::production';
48+
}
49+
});
50+
51+
// Tests can run in different environment.
52+
53+
await writeFile(
54+
'src/app/environment.spec.ts',
55+
`
5056
import { environment } from '../environment';
5157
5258
describe('Test environment', () => {
@@ -55,8 +61,11 @@ export default function () {
5561
});
5662
});
5763
`,
58-
),
59-
)
60-
.then(() => ng('test', '--configuration=production', '--watch=false'))
6164
);
65+
66+
if (isWebpack) {
67+
await ng('test', '--watch=false', '--configuration=production');
68+
} else {
69+
await ng('test', '--watch=false');
70+
}
6271
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { getGlobalVariable } from '../../utils/env';
12
import { writeMultipleFiles } from '../../utils/fs';
23
import { ng } from '../../utils/process';
34
import { updateJsonFile } from '../../utils/project';
@@ -60,9 +61,10 @@ export default async function () {
6061
// should fail because the global scripts were not added to scripts array
6162
await expectToFail(() => ng('test', '--watch=false'));
6263

64+
const isWebpack = !getGlobalVariable('argv')['esbuild'];
6365
await updateJsonFile('angular.json', (workspaceJson) => {
6466
const appArchitect = workspaceJson.projects['test-project'].architect;
65-
appArchitect.test.options.scripts = [
67+
appArchitect[isWebpack ? 'test' : 'build'].options.scripts = [
6668
{ input: 'src/string-script.js' },
6769
{ input: 'src/input-script.js' },
6870
];

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

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
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';
6+
import { getGlobalVariable } from '../../utils/env';
67

78
export default async function () {
9+
const isWebpack = !getGlobalVariable('argv')['esbuild'];
10+
811
await writeFile(
912
'src/app/app.spec.ts',
1013
`
@@ -15,8 +18,16 @@ export default async function () {
1518
);
1619

1720
// when sourcemaps are 'on' the stacktrace will point to the spec.ts file.
21+
await updateJsonFile('angular.json', (configJson) => {
22+
const appArchitect = configJson.projects['test-project'].architect;
23+
if (isWebpack) {
24+
appArchitect['test'].options.sourceMap = true;
25+
} else {
26+
appArchitect['build'].configurations.development.sourceMap = true;
27+
}
28+
});
1829
try {
19-
await ng('test', '--no-watch', '--source-map');
30+
await ng('test', '--no-watch');
2031
throw new Error('ng test should have failed.');
2132
} catch (error) {
2233
assertIsError(error);
@@ -25,8 +36,16 @@ export default async function () {
2536
}
2637

2738
// when sourcemaps are 'off' the stacktrace won't point to the spec.ts file.
39+
await updateJsonFile('angular.json', (configJson) => {
40+
const appArchitect = configJson.projects['test-project'].architect;
41+
if (isWebpack) {
42+
appArchitect['test'].options.sourceMap = false;
43+
} else {
44+
appArchitect['build'].configurations.development.sourceMap = false;
45+
}
46+
});
2847
try {
29-
await ng('test', '--no-watch', '--no-source-map');
48+
await ng('test', '--no-watch');
3049
throw new Error('ng test should have failed.');
3150
} catch (error) {
3251
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)