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/build/index.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ export type UnitTestBuilderOptions = {
reporters?: SchemaReporter[];
runner: Runner;
setupFiles?: string[];
tsConfig: string;
tsConfig?: string;
watch?: boolean;
};

Expand Down
5 changes: 4 additions & 1 deletion packages/angular/build/src/builders/unit-test/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,10 +296,13 @@ export async function* execute(
...buildTargetOptions,
...runnerBuildOptions,
watch: normalizedOptions.watch,
tsConfig: normalizedOptions.tsConfig,
progress: normalizedOptions.buildProgress ?? buildTargetOptions.progress,
} satisfies ApplicationBuilderInternalOptions;

if (normalizedOptions.tsConfig) {
applicationBuildOptions.tsConfig = normalizedOptions.tsConfig;
}

const dumpDirectory = normalizedOptions.dumpVirtualFiles
? path.join(normalizedOptions.cacheOptions.path, 'unit-test', 'output-files')
: undefined;
Expand Down
27 changes: 26 additions & 1 deletion packages/angular/build/src/builders/unit-test/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/

import { type BuilderContext, targetFromTargetString } from '@angular-devkit/architect';
import { constants, promises as fs } from 'node:fs';
import path from 'node:path';
import { normalizeCacheOptions } from '../../utils/normalize-cache';
import { getProjectRootPaths } from '../../utils/project-metadata';
Expand All @@ -15,6 +16,16 @@ import type { Schema as UnitTestBuilderOptions } from './schema';

export type NormalizedUnitTestBuilderOptions = Awaited<ReturnType<typeof normalizeOptions>>;

async function exists(path: string): Promise<boolean> {
try {
await fs.access(path, constants.F_OK);

return true;
} catch {
return false;
}
}

function normalizeReporterOption(
reporters: unknown[] | undefined,
): [string, Record<string, unknown>][] | undefined {
Expand Down Expand Up @@ -43,7 +54,21 @@ export async function normalizeOptions(
const buildTargetSpecifier = options.buildTarget ?? `::development`;
const buildTarget = targetFromTargetString(buildTargetSpecifier, projectName, 'build');

const { tsConfig, runner, browsers, progress, filter } = options;
const { runner, browsers, progress, filter } = options;

let tsConfig = options.tsConfig;
if (tsConfig) {
const fullTsConfigPath = path.join(workspaceRoot, tsConfig);
if (!(await exists(fullTsConfigPath))) {
throw new Error(`The specified tsConfig file '${tsConfig}' does not exist.`);
}
} else {
const tsconfigSpecPath = path.join(projectRoot, 'tsconfig.spec.json');
if (await exists(tsconfigSpecPath)) {
// The application builder expects a path relative to the workspace root.
tsConfig = path.relative(workspaceRoot, tsconfigSpecPath);
}
}

return {
// Project/workspace information
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export class KarmaExecutor implements TestExecutor {
)) as unknown as ApplicationBuilderInternalOptions;

const karmaOptions: KarmaBuilderOptions = {
tsConfig: unitTestOptions.tsConfig,
tsConfig: unitTestOptions.tsConfig ?? buildTargetOptions.tsConfig,
polyfills: buildTargetOptions.polyfills,
assets: buildTargetOptions.assets,
scripts: buildTargetOptions.scripts,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,7 @@ export async function getVitestBuildOptions(
options: NormalizedUnitTestBuilderOptions,
baseBuildOptions: Partial<ApplicationBuilderInternalOptions>,
): Promise<RunnerOptions> {
const {
workspaceRoot,
projectSourceRoot,
include,
exclude = [],
watch,
tsConfig,
providersFile,
} = options;
const { workspaceRoot, projectSourceRoot, include, exclude = [], watch, providersFile } = options;

// Find test files
const testFiles = await findTests(include, exclude, workspaceRoot, projectSourceRoot);
Expand Down Expand Up @@ -108,7 +100,6 @@ export async function getVitestBuildOptions(
sourceMap: { scripts: true, vendor: false, styles: false },
outputHashing: adjustOutputHashing(baseBuildOptions.outputHashing),
optimization: false,
tsConfig,
entryPoints,
externalDependencies: ['vitest', '@vitest/browser/context'],
};
Expand Down
4 changes: 2 additions & 2 deletions packages/angular/build/src/builders/unit-test/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
},
"tsConfig": {
"type": "string",
"description": "The path to the TypeScript configuration file, relative to the workspace root."
"description": "The path to the TypeScript configuration file, relative to the workspace root. Defaults to `tsconfig.spec.json` in the project root if it exists. If not specified and the default does not exist, the `tsConfig` from the specified `buildTarget` will be used."
},
"runner": {
"type": "string",
Expand Down Expand Up @@ -188,5 +188,5 @@
}
},
"additionalProperties": false,
"required": ["buildTarget", "tsConfig", "runner"]
"required": ["buildTarget", "runner"]
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,43 +12,48 @@ import {
describeBuilder,
UNIT_TEST_BUILDER_INFO,
setupApplicationTarget,
expectLog,
} from '../setup';

describeBuilder(execute, UNIT_TEST_BUILDER_INFO, (harness) => {
xdescribe('Option: "tsConfig"', () => {
describe('Option: "tsConfig"', () => {
beforeEach(async () => {
setupApplicationTarget(harness);
});

it('should fail when tsConfig is not provided', async () => {
it('should use "tsconfig.spec.json" by default when it exists', async () => {
const { tsConfig, ...rest } = BASE_OPTIONS;
harness.useTarget('test', rest as any);
harness.useTarget('test', rest);

await expectAsync(harness.executeOnce()).toBeRejectedWithError(/"tsConfig" is required/);
// Create tsconfig.spec.json
await harness.writeFile(
'tsconfig.spec.json',
`{ "extends": "./tsconfig.json", "compilerOptions": { "types": ["jasmine"] }, "include": ["src/**/*.ts"] }`,
);

const { result } = await harness.executeOnce();
expect(result?.success).toBeTrue();
// TODO: Add expectation that the file was used.
});

it('should fail when tsConfig is empty', async () => {
harness.useTarget('test', {
...BASE_OPTIONS,
tsConfig: '',
});
it('should use build target tsConfig when "tsconfig.spec.json" does not exist', async () => {
const { tsConfig, ...rest } = BASE_OPTIONS;
harness.useTarget('test', rest);

await expectAsync(harness.executeOnce()).toBeRejectedWithError(
/must NOT have fewer than 1 characters/,
);
// The build target tsconfig is not setup to build the tests and should fail
const { result } = await harness.executeOnce();
expect(result?.success).toBeFalse();
});

it('should fail when tsConfig does not exist', async () => {
it('should fail when user specified tsConfig does not exist', async () => {
harness.useTarget('test', {
...BASE_OPTIONS,
tsConfig: 'src/tsconfig.spec.json',
tsConfig: 'random/tsconfig.spec.json',
});

const { result, error } = await harness.executeOnce({ outputLogsOnFailure: false });
expect(result).toBeUndefined();
expect(error?.message).toMatch(
`The specified tsConfig file "src/tsconfig.spec.json" does not exist.`,
);
const { result, logs } = await harness.executeOnce({ outputLogsOnFailure: false });
expect(result?.success).toBeFalse();
expectLog(logs, `The specified tsConfig file 'random/tsconfig.spec.json' does not exist.`);
});
});
});