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
1 change: 0 additions & 1 deletion goldens/public-api/angular/build/index.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,6 @@ export type UnitTestBuilderOptions = {
browsers?: string[];
buildTarget?: string;
coverage?: boolean;
coverageAll?: boolean;
coverageExclude?: string[];
coverageInclude?: string[];
coverageReporters?: SchemaCoverageReporter[];
Expand Down
4 changes: 2 additions & 2 deletions modules/testing/builder/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
"@angular-devkit/architect": "workspace:*",
"@angular/ssr": "workspace:*",
"@angular-devkit/build-angular": "workspace:*",
"@vitest/coverage-v8": "3.2.4",
"@vitest/coverage-v8": "4.0.0-beta.17",
"jsdom": "27.0.0",
"rxjs": "7.8.2",
"vitest": "3.2.4"
"vitest": "4.0.0-beta.17"
}
}
4 changes: 2 additions & 2 deletions packages/angular/build/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
"ng-packagr": "21.0.0-next.4",
"postcss": "8.5.6",
"rxjs": "7.8.2",
"vitest": "3.2.4"
"vitest": "4.0.0-beta.17"
},
"peerDependencies": {
"@angular/core": "0.0.0-ANGULAR-FW-PEER-DEP",
Expand All @@ -74,7 +74,7 @@
"tailwindcss": "^2.0.0 || ^3.0.0 || ^4.0.0",
"tslib": "^2.3.0",
"typescript": ">=5.9 <6.0",
"vitest": "^3.1.1"
"vitest": "^4.0.0-beta.17"
},
"peerDependenciesMeta": {
"@angular/core": {
Expand Down
1 change: 0 additions & 1 deletion packages/angular/build/src/builders/unit-test/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ export async function normalizeOptions(
runnerName: runner,
coverage: options.coverage
? {
all: options.coverageAll,
exclude: options.coverageExclude,
include: options.coverageInclude,
reporters: normalizeReporterOption(options.coverageReporters),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,6 @@ export class KarmaExecutor implements TestExecutor {
);
}

if (unitTestOptions.coverage?.all) {
context.logger.warn(
'The "karma" test runner does not support the "coverageAll" option. The option will be ignored.',
);
}

if (unitTestOptions.coverage?.include) {
context.logger.warn(
'The "karma" test runner does not support the "coverageInclude" option. The option will be ignored.',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/

import { createRequire } from 'node:module';
import { assertIsError } from '../../../../utils/error';

export interface BrowserConfiguration {
browser?: import('vitest/node').BrowserConfigOptions;
Expand Down Expand Up @@ -39,45 +40,60 @@ function normalizeBrowserName(browserName: string): string {
return normalized.replace(/headless$/, '');
}

export function setupBrowserConfiguration(
export async function setupBrowserConfiguration(
browsers: string[] | undefined,
debug: boolean,
projectSourceRoot: string,
viewport: { width: number; height: number } | undefined,
): BrowserConfiguration {
): Promise<BrowserConfiguration> {
if (browsers === undefined) {
return {};
}

const projectResolver = createRequire(projectSourceRoot + '/').resolve;
let errors: string[] | undefined;

try {
projectResolver('@vitest/browser');
} catch {
errors ??= [];
errors.push(
'The "browsers" option requires the "@vitest/browser" package to be installed within the project.' +
' Please install this package and rerun the test command.',
);
}

const provider = findBrowserProvider(projectResolver);
if (!provider) {
const providerName = findBrowserProvider(projectResolver);
if (!providerName) {
errors ??= [];
errors.push(
'The "browsers" option requires either "playwright" or "webdriverio" to be installed within the project.' +
' Please install one of these packages and rerun the test command.',
);
}

// Vitest current requires the playwright browser provider to use the inspect-brk option used by "debug"
if (debug && provider !== 'playwright') {
errors ??= [];
errors.push(
'Debugging browser mode tests currently requires the use of "playwright".' +
' Please install this package and rerun the test command.',
);
let provider: import('vitest/node').BrowserProviderOption | undefined;
if (providerName) {
const providerPackage = `@vitest/browser-${providerName}`;
try {
const providerModule = await import(providerPackage);

// Validate that the imported module has the expected structure
const providerFactory = providerModule[providerName];
if (typeof providerFactory === 'function') {
provider = providerFactory();
} else {
errors ??= [];
errors.push(
`The "${providerPackage}" package does not have a valid browser provider export.`,
);
}
} catch (e) {
assertIsError(e);
errors ??= [];
// Check for a module not found error to provide a more specific message
if (e.code === 'ERR_MODULE_NOT_FOUND') {
errors.push(
`The "browsers" option with "${providerName}" requires the "${providerPackage}" package.` +
' Please install this package and rerun the test command.',
);
} else {
// Handle other potential errors during import
errors.push(
`An error occurred while loading the "${providerPackage}" browser provider:\n ${e.message}`,
);
}
}
}

if (errors) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ export class VitestExecutor implements TestExecutor {
const { startVitest } = vitestNodeModule;

// Setup vitest browser options if configured
const browserOptions = setupBrowserConfiguration(
const browserOptions = await setupBrowserConfiguration(
browsers,
debug,
this.options.projectSourceRoot,
Expand Down Expand Up @@ -237,7 +237,6 @@ async function generateCoverageOption(

return {
enabled: true,
all: coverage.all,
excludeAfterRemap: true,
include: coverage.include,
reportsDirectory: toPosixPath(path.join('coverage', projectName)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ const VitestTestRunner: TestRunner = {
checker.check('vitest');

if (options.browsers?.length) {
checker.check('@vitest/browser');
checker.checkAny(
['playwright', 'webdriverio'],
'The "browsers" option requires either "playwright" or "webdriverio" to be installed.',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export function createVitestPlugins(
// Create a subproject that can be configured with plugins for browser mode.
// Plugins defined directly in the vite overrides will not be present in the
// browser specific Vite instance.
await context.injectTestProjects({
const [project] = await context.injectTestProjects({
test: {
name: projectName,
root: workspaceRoot,
Expand Down Expand Up @@ -155,6 +155,13 @@ export function createVitestPlugins(
},
],
});

// Filter the resolved coverage exclude patterns to remove non-string values.
// This is a workaround for vitest adding `false` into the array when `startVitest` is
// used with `config: false`. This can be removed once corrected in Vitest.
project.config.coverage.exclude = project.config.coverage.exclude.filter(
(pattern) => typeof pattern === 'string',
);
},
},
];
Expand Down
5 changes: 0 additions & 5 deletions packages/angular/build/src/builders/unit-test/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,6 @@
"description": "Enables coverage reporting for tests.",
"default": false
},
"coverageAll": {
"type": "boolean",
"description": "Includes all files that match the `coverageInclude` pattern in the coverage report, not just those touched by tests.",
"default": true
},
"coverageInclude": {
"type": "array",
"description": "Specifies glob patterns of files to include in the coverage report.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ describeBuilder(execute, UNIT_TEST_BUILDER_INFO, (harness) => {
harness.useTarget('test', {
...BASE_OPTIONS,
coverage: true,
coverageInclude: ['**/*.ts'],
});

const { result } = await harness.executeOnce();
Expand All @@ -39,6 +40,7 @@ describeBuilder(execute, UNIT_TEST_BUILDER_INFO, (harness) => {
harness.useTarget('test', {
...BASE_OPTIONS,
coverage: true,
coverageInclude: ['**/*.ts'],
coverageExclude: ['**/error.ts'],
});

Expand Down
Loading
Loading