Skip to content

Commit 4ec2a33

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 42811d8 commit 4ec2a33

File tree

5 files changed

+29
-3
lines changed

5 files changed

+29
-3
lines changed

goldens/public-api/angular/build/index.api.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ export type UnitTestBuilderOptions = {
228228
providersFile?: string;
229229
reporters?: string[];
230230
runner: Runner;
231+
setupFiles?: string[];
231232
tsConfig: string;
232233
watch?: boolean;
233234
};

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

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

199199
// Add setup file entries for TestBed initialization and project polyfills
200-
const setupFiles = ['init-testbed.js'];
200+
const setupFiles = ['init-testbed.js', ...normalizedOptions.setupFiles];
201201
if (buildTargetOptions?.polyfills?.length) {
202-
setupFiles.push('polyfills.js');
202+
// Placed first as polyfills may be required by the Testbed initialization
203+
// or other project provided setup files (e.g., zone.js, ECMAScript polyfills).
204+
setupFiles.unshift('polyfills.js');
203205
}
204206
const debugOptions = normalizedOptions.debug
205207
? {

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)