Skip to content

fix(@angular/build): support extra test setup files with unit-test vitest runner #30664

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 9, 2025
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
1 change: 1 addition & 0 deletions goldens/public-api/angular/build/index.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ export type UnitTestBuilderOptions = {
providersFile?: string;
reporters?: string[];
runner: Runner;
setupFiles?: string[];
tsConfig: string;
watch?: boolean;
};
Expand Down
6 changes: 4 additions & 2 deletions packages/angular/build/src/builders/unit-test/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,11 @@ export async function* execute(
}

// Add setup file entries for TestBed initialization and project polyfills
const setupFiles = ['init-testbed.js'];
const setupFiles = ['init-testbed.js', ...normalizedOptions.setupFiles];
if (buildTargetOptions?.polyfills?.length) {
setupFiles.push('polyfills.js');
// Placed first as polyfills may be required by the Testbed initialization
// or other project provided setup files (e.g., zone.js, ECMAScript polyfills).
setupFiles.unshift('polyfills.js');
}
const debugOptions = normalizedOptions.debug
? {
Expand Down
6 changes: 6 additions & 0 deletions packages/angular/build/src/builders/unit-test/karma-bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ export async function useKarmaBuilder(
);
}

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

const buildTargetOptions = (await context.validateOptions(
await context.getTargetOptions(unitTestOptions.buildTarget),
await context.getBuilderNameForTarget(unitTestOptions.buildTarget),
Expand Down
3 changes: 3 additions & 0 deletions packages/angular/build/src/builders/unit-test/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ export async function normalizeOptions(
watch: options.watch ?? isTTY(),
debug: options.debug ?? false,
providersFile: options.providersFile && path.join(workspaceRoot, options.providersFile),
setupFiles: options.setupFiles
? options.setupFiles.map((setupFile) => path.join(workspaceRoot, setupFile))
: [],
};
}

Expand Down
16 changes: 15 additions & 1 deletion packages/angular/build/src/builders/unit-test/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,14 @@
"type": "array",
"minItems": 1,
"maxItems": 2,
"items": [{ "$ref": "#/definitions/coverage-reporters" }, { "type": "object" }]
"items": [
{
"$ref": "#/definitions/coverage-reporters"
},
{
"type": "object"
}
]
}
]
}
Expand All @@ -92,6 +99,13 @@
"type": "string",
"description": "TypeScript file that exports an array of Angular providers to use during test execution. The array must be a default export.",
"minLength": 1
},
"setupFiles": {
"type": "array",
"items": {
"type": "string"
},
"description": "A list of global setup and configuration files that are included before the test files. The application's polyfills are always included before these files. The Angular Testbed is also initialized prior to the execution of these files."
}
},
"additionalProperties": false,
Expand Down