Skip to content
Open
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
7 changes: 6 additions & 1 deletion packages/angular/build/src/builders/unit-test/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export async function normalizeOptions(
const buildTargetSpecifier = options.buildTarget ?? `::development`;
const buildTarget = targetFromTargetString(buildTargetSpecifier, projectName, 'build');

// Load target options to determine preserveSymlinks setting
// Load target options to determine preserveSymlinks and conditions settings
let buildTargetOptions: Record<string, unknown> | undefined;
try {
buildTargetOptions = await context.getTargetOptions(buildTarget);
Expand All @@ -57,6 +57,10 @@ export async function normalizeOptions(
? buildTargetOptions.preserveSymlinks
: process.execArgv.includes('--preserve-symlinks');

const conditions = Array.isArray(buildTargetOptions?.conditions)
? buildTargetOptions.conditions.filter((c): c is string => typeof c === 'string')
: undefined;
Comment on lines +60 to +62

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

To ensure robustness and prevent potential runtime errors (e.g., if the configuration is loaded programmatically or schema validation is bypassed), we should defensively filter the conditions array to ensure it only contains string values instead of relying on a type assertion.

Suggested change
const conditions = Array.isArray(buildTargetOptions?.conditions)
? (buildTargetOptions.conditions as string[])
: undefined;
const conditions = Array.isArray(buildTargetOptions?.conditions)
? buildTargetOptions.conditions.filter((c): c is string => typeof c === 'string')
: undefined;

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Applied in e73ffc5. The build target options here aren't schema-validated, so the type check matches how preserveSymlinks is read above.


// Setup base paths based on workspace root and project information
const workspaceRoot = canonicalizePath(context.workspaceRoot);

Expand Down Expand Up @@ -168,6 +172,7 @@ export async function normalizeOptions(
dumpVirtualFiles: options.dumpVirtualFiles,
listTests: options.listTests,
preserveSymlinks,
conditions,
polyfills,
runnerConfig:
typeof runnerConfig === 'string'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,7 @@ export class VitestExecutor implements TestExecutor {
watch,
isolate: this.options.isolate,
preserveSymlinks: this.options.preserveSymlinks,
conditions: this.options.conditions,
runnerArgOptions: this.runnerArgOptions?.project,
}),
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ interface VitestConfigPluginOptions {
watch: boolean;
isolate: boolean | undefined;
preserveSymlinks?: boolean;
conditions?: string[];

/** Vitest project options from the `runnerArgs` builder option. */
runnerArgOptions?: Record<string, unknown>;
Expand Down Expand Up @@ -285,7 +286,13 @@ export async function createVitestConfigPlugin(
},
resolve: {
mainFields: ['es2020', 'module', 'main'],
conditions: ['es2015', 'es2020', 'module', ...(browser ? ['browser'] : [])],
// Custom build conditions replace the default `module` condition, as they do in the build.
conditions: [
'es2015',
'es2020',
...(options.conditions ?? ['module']),
...(browser ? ['browser'] : []),
],
preserveSymlinks: options.preserveSymlinks,
},
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/

import { execute } from '../../index';
import {
BASE_OPTIONS,
describeBuilder,
UNIT_TEST_BUILDER_INFO,
setupApplicationTarget,
} from '../setup';

describeBuilder(execute, UNIT_TEST_BUILDER_INFO, (harness) => {
describe('Behavior: "Vitest build conditions"', () => {
beforeEach(async () => {
await harness.writeFiles({
'node_modules/cond-pkg/package.json': JSON.stringify({
name: 'cond-pkg',
version: '1.0.0',
type: 'module',
exports: {
'.': {
'my-condition': { types: './index.d.ts', default: './custom.mjs' },
types: './index.d.ts',
default: './default.mjs',
},
},
}),
'node_modules/cond-pkg/index.d.ts': `export declare const VALUE: string;`,
'node_modules/cond-pkg/custom.mjs': `export const VALUE = 'custom-value';`,
'node_modules/cond-pkg/default.mjs': `export const VALUE = 'default-value';`,
});
});

function writeSpec(expected: string) {
return harness.writeFile(
'src/app/app.component.spec.ts',
`
import { describe, expect, test } from 'vitest';
import { VALUE } from 'cond-pkg';

describe('cond-pkg', () => {
test('resolves the expected export', () => {
expect(VALUE).toBe('${expected}');
});
});
`,
);
}

it('should resolve packages with the build target conditions', async () => {
setupApplicationTarget(harness, { conditions: ['my-condition'] });
await writeSpec('custom-value');

harness.useTarget('test', {
...BASE_OPTIONS,
});

const { result } = await harness.executeOnce();
expect(result?.success).toBeTrue();
});

it('should resolve packages with the default conditions when the build target has none', async () => {
setupApplicationTarget(harness);
await writeSpec('default-value');

harness.useTarget('test', {
...BASE_OPTIONS,
});

const { result } = await harness.executeOnce();
expect(result?.success).toBeTrue();
});
});
});