Skip to content

Commit c71a783

Browse files
authored
fix(angular): ensure crystal targets for testing when bundler=rspack (#30631)
## Current Behavior Angular Rspack relies on Inferred Targets however, when scaffolding the application, the unit test runners are being set up with executors. ## Expected Behavior Ensure that when `bundler=rspack` unit test runners are being set up with inference plugins
1 parent d4ebf82 commit c71a783

File tree

12 files changed

+66
-11
lines changed

12 files changed

+66
-11
lines changed

packages/angular/src/generators/add-linting/add-linting.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export async function addLintingGenerator(
4040
setParserOptionsProject: options.setParserOptionsProject,
4141
skipFormat: true,
4242
rootProject: rootProject,
43-
addPlugin: false,
43+
addPlugin: options.addPlugin ?? false,
4444
addExplicitTargets: true,
4545
skipPackageJson: options.skipPackageJson,
4646
});

packages/angular/src/generators/add-linting/schema.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ export interface AddLintingGeneratorSchema {
66
skipFormat?: boolean;
77
skipPackageJson?: boolean;
88
unitTestRunner?: string;
9+
addPlugin?: boolean;
910
}

packages/angular/src/generators/application/application.spec.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1246,6 +1246,7 @@ describe('app', () => {
12461246
]
12471247
`);
12481248
});
1249+
12491250
it('should generate a correct setup when --bundler=rspack including a correct config file and no build target', async () => {
12501251
await generateApp(appTree, 'app1', {
12511252
bundler: 'rspack',
@@ -1257,6 +1258,42 @@ describe('app', () => {
12571258
expect(appTree.read('app1/rspack.config.ts', 'utf-8')).toMatchSnapshot();
12581259
});
12591260

1261+
it('should generate use crystal jest when --bundler=rspack', async () => {
1262+
await generateApp(appTree, 'app1', {
1263+
bundler: 'rspack',
1264+
unitTestRunner: UnitTestRunner.Jest,
1265+
});
1266+
1267+
const project = readProjectConfiguration(appTree, 'app1');
1268+
expect(project.targets.test).not.toBeDefined();
1269+
1270+
const nxJson = readNxJson(appTree);
1271+
const jestPlugin = nxJson.plugins.find(
1272+
(p) =>
1273+
(typeof p === 'string' && p === '@nx/jest/plugin') ||
1274+
(typeof p !== 'string' && p.plugin === '@nx/jest/plugin')
1275+
);
1276+
expect(jestPlugin).toBeDefined();
1277+
});
1278+
1279+
it('should generate use crystal vitest when --bundler=rspack', async () => {
1280+
await generateApp(appTree, 'app1', {
1281+
bundler: 'rspack',
1282+
unitTestRunner: UnitTestRunner.Vitest,
1283+
});
1284+
1285+
const project = readProjectConfiguration(appTree, 'app1');
1286+
expect(project.targets.test).not.toBeDefined();
1287+
1288+
const nxJson = readNxJson(appTree);
1289+
const vitePlugin = nxJson.plugins.find(
1290+
(p) =>
1291+
(typeof p === 'string' && p === '@nx/vite/plugin') ||
1292+
(typeof p !== 'string' && p.plugin === '@nx/vite/plugin')
1293+
);
1294+
expect(vitePlugin).toBeDefined();
1295+
});
1296+
12601297
it('should generate target options "browser" and "buildTarget"', async () => {
12611298
await generateApp(appTree, 'my-app', { standalone: true });
12621299

packages/angular/src/generators/application/application.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ import {
3131
updateEditorTsConfig,
3232
} from './lib';
3333
import type { Schema } from './schema';
34-
import { tsNodeVersion } from '../../utils/versions';
3534

3635
export async function applicationGenerator(
3736
tree: Tree,
@@ -43,7 +42,7 @@ export async function applicationGenerator(
4342
schema.bundler = 'webpack';
4443
}
4544

46-
const options = await normalizeOptions(tree, schema);
45+
const options = await normalizeOptions(tree, schema, isRspack);
4746
const rootOffset = offsetFromRoot(options.appProjectRoot);
4847

4948
await jsInitGenerator(tree, {
@@ -55,6 +54,7 @@ export async function applicationGenerator(
5554
await angularInitGenerator(tree, {
5655
...options,
5756
skipFormat: true,
57+
addPlugin: options.addPlugin,
5858
});
5959

6060
if (!options.skipPackageJson) {

packages/angular/src/generators/application/lib/add-e2e.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ export async function addE2e(tree: Tree, options: NormalizedSchema) {
1515
// since e2e are separate projects, default to adding plugins
1616
const nxJson = readNxJson(tree);
1717
const addPlugin =
18-
process.env.NX_ADD_PLUGINS !== 'false' &&
19-
nxJson.useInferencePlugins !== false;
18+
nxJson['useInferencePlugins'] !== false &&
19+
process.env.NX_ADD_PLUGINS !== 'false';
2020

2121
const e2eWebServerInfo = getAngularE2EWebServerInfo(
2222
tree,

packages/angular/src/generators/application/lib/add-linting.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,6 @@ export async function addLinting(host: Tree, options: NormalizedSchema) {
1616
skipPackageJson: options.skipPackageJson,
1717
unitTestRunner: options.unitTestRunner,
1818
skipFormat: true,
19+
addPlugin: options.addPlugin,
1920
});
2021
}

packages/angular/src/generators/application/lib/add-unit-test-runner.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export async function addUnitTestRunner(host: Tree, options: NormalizedSchema) {
1212
projectRoot: options.appProjectRoot,
1313
skipPackageJson: options.skipPackageJson,
1414
strict: options.strict,
15+
addPlugin: options.addPlugin,
1516
});
1617
break;
1718
case UnitTestRunner.Vitest:
@@ -20,6 +21,7 @@ export async function addUnitTestRunner(host: Tree, options: NormalizedSchema) {
2021
projectRoot: options.appProjectRoot,
2122
skipPackageJson: options.skipPackageJson,
2223
strict: options.strict,
24+
addPlugin: options.addPlugin,
2325
});
2426
break;
2527
}

packages/angular/src/generators/application/lib/normalize-options.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { joinPathFragments, type Tree } from '@nx/devkit';
1+
import { joinPathFragments, readNxJson, type Tree } from '@nx/devkit';
22
import {
33
determineProjectNameAndRootOptions,
44
ensureRootProjectName,
@@ -8,9 +8,16 @@ import { E2eTestRunner, UnitTestRunner } from '../../../utils/test-runners';
88
import type { Schema } from '../schema';
99
import type { NormalizedSchema } from './normalized-schema';
1010

11+
function arePluginsExplicitlyDisabled(host: Tree) {
12+
const { useInferencePlugins } = readNxJson(host);
13+
const addPluginEnvVar = process.env.NX_ADD_PLUGINS;
14+
return useInferencePlugins === false || addPluginEnvVar === 'false';
15+
}
16+
1117
export async function normalizeOptions(
1218
host: Tree,
13-
options: Partial<Schema>
19+
options: Partial<Schema>,
20+
isRspack?: boolean
1421
): Promise<NormalizedSchema> {
1522
await ensureRootProjectName(options as Schema, 'application');
1623
const { projectName: appProjectName, projectRoot: appProjectRoot } =
@@ -31,8 +38,12 @@ export async function normalizeOptions(
3138

3239
const bundler = options.bundler ?? 'esbuild';
3340

41+
const addPlugin =
42+
options.addPlugin ?? (!arePluginsExplicitlyDisabled(host) && isRspack);
43+
3444
// Set defaults and then overwrite with user options
3545
return {
46+
addPlugin,
3647
style: 'css',
3748
routing: true,
3849
inlineStyle: false,

packages/angular/src/generators/application/schema.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,5 @@ export interface Schema {
3131
ssr?: boolean;
3232
serverRouting?: boolean;
3333
nxCloudToken?: string;
34+
addPlugin?: boolean;
3435
}

packages/angular/src/generators/convert-to-rspack/convert-to-rspack.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,8 +410,8 @@ export async function convertToRspack(
410410
);
411411
}
412412
}
413+
serveTargetNames.push(targetName);
413414
}
414-
serveTargetNames.push(targetName);
415415
}
416416

417417
const customWebpackConfigInfo = customWebpackConfigPath

packages/angular/src/generators/utils/add-jest.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export type AddJestOptions = {
1111
projectRoot: string;
1212
skipPackageJson: boolean;
1313
strict: boolean;
14+
addPlugin?: boolean;
1415
};
1516

1617
export async function addJest(
@@ -40,8 +41,8 @@ export async function addJest(
4041
skipSerializers: false,
4142
skipPackageJson: options.skipPackageJson,
4243
skipFormat: true,
43-
addPlugin: false,
44-
addExplicitTargets: true,
44+
addPlugin: options.addPlugin ?? false,
45+
addExplicitTargets: !options.addPlugin,
4546
});
4647

4748
const setupFile = joinPathFragments(

packages/angular/src/generators/utils/add-vitest.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export type AddVitestOptions = {
66
projectRoot: string;
77
skipPackageJson: boolean;
88
strict: boolean;
9+
addPlugin?: boolean;
910
};
1011

1112
export async function addVitest(
@@ -22,6 +23,6 @@ export async function addVitest(
2223
uiFramework: 'angular',
2324
testEnvironment: 'jsdom',
2425
coverageProvider: 'v8',
25-
addPlugin: false,
26+
addPlugin: options.addPlugin ?? false,
2627
});
2728
}

0 commit comments

Comments
 (0)