Skip to content

Commit 27e8f9e

Browse files
committed
fix(@angular/build): support extra test setup files with unit-test vitest runner
When using the experimental unit-test builder with Vitest, a new `setupFiles` option is now available. This option is similar to the Vitest option in that it allows for setup and configuration prior to each test. The `setupFiles` are executed after any application polyfills as well as after the TestBed initialization. If custom TestBed initialization is needed (this is not typical), The TestBed environment can first be reset in a setupFile and then initialized as needed. Note that resetting the TestBed environment in this way will cause the `providersFile` to no longer add any providers to the tests and any custom providers would need to be manually added during initialization.
1 parent 4161ea8 commit 27e8f9e

File tree

4 files changed

+28
-3
lines changed

4 files changed

+28
-3
lines changed

packages/angular/build/src/builders/unit-test/builder.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,9 +193,11 @@ export async function* execute(
193193
}
194194

195195
// Add setup file entries for TestBed initialization and project polyfills
196-
const setupFiles = ['init-testbed.js'];
196+
const setupFiles = ['init-testbed.js', ...normalizedOptions.setupFiles];
197197
if (buildTargetOptions?.polyfills?.length) {
198-
setupFiles.push('polyfills.js');
198+
// Placed first as polyfills may be required by the Testbed initialization
199+
// or other project provided setup files (e.g., zone.js, ECMAScript polyfills).
200+
setupFiles.unshift('polyfills.js');
199201
}
200202
const debugOptions = normalizedOptions.debug
201203
? {

packages/angular/build/src/builders/unit-test/karma-bridge.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ export async function useKarmaBuilder(
2121
);
2222
}
2323

24+
if (unitTestOptions.setupFiles.length) {
25+
context.logger.warn(
26+
'The "karma" test runner does not support the "setupFiles" option. The option will be ignored.',
27+
);
28+
}
29+
2430
const buildTargetOptions = (await context.validateOptions(
2531
await context.getTargetOptions(unitTestOptions.buildTarget),
2632
await context.getBuilderNameForTarget(unitTestOptions.buildTarget),

packages/angular/build/src/builders/unit-test/options.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ export async function normalizeOptions(
6262
watch: options.watch ?? isTTY(),
6363
debug: options.debug ?? false,
6464
providersFile: options.providersFile && path.join(workspaceRoot, options.providersFile),
65+
setupFiles: options.setupFiles
66+
? options.setupFiles.map((setupFile) => path.join(workspaceRoot, setupFile))
67+
: [],
6568
};
6669
}
6770

packages/angular/build/src/builders/unit-test/schema.json

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,14 @@
7676
"type": "array",
7777
"minItems": 1,
7878
"maxItems": 2,
79-
"items": [{ "$ref": "#/definitions/coverage-reporters" }, { "type": "object" }]
79+
"items": [
80+
{
81+
"$ref": "#/definitions/coverage-reporters"
82+
},
83+
{
84+
"type": "object"
85+
}
86+
]
8087
}
8188
]
8289
}
@@ -92,6 +99,13 @@
9299
"type": "string",
93100
"description": "TypeScript file that exports an array of Angular providers to use during test execution. The array must be a default export.",
94101
"minLength": 1
102+
},
103+
"setupFiles": {
104+
"type": "array",
105+
"items": {
106+
"type": "string"
107+
},
108+
"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."
95109
}
96110
},
97111
"additionalProperties": false,

0 commit comments

Comments
 (0)