Skip to content

Commit d39eb31

Browse files
committed
fix(@angular/build): support injecting global styles into vitest unit-tests
When using the experimental `unit-test` builder with the `vitest` runner, the build option's global stylesheet file (output: `styles.css`) will now be included in the generated `index.html` file used when browser mode is enabled. The global stylesheets used are currently limited to the default `styles.css` output file. If custom named global stylesheets are configured, they will not currently be injected into the test index HTML file used with browser testing.
1 parent 3d65518 commit d39eb31

File tree

1 file changed

+51
-27
lines changed
  • packages/angular/build/src/builders/unit-test

1 file changed

+51
-27
lines changed

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

Lines changed: 51 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -224,37 +224,61 @@ export async function* execute(
224224
{
225225
// Disable configuration file resolution/loading
226226
config: false,
227+
root: workspaceRoot,
228+
project: ['base', projectName],
229+
name: 'base',
230+
include: [],
231+
reporters: normalizedOptions.reporters ?? ['default'],
232+
watch: normalizedOptions.watch,
233+
coverage: {
234+
enabled: !!normalizedOptions.codeCoverage,
235+
reporter: normalizedOptions.codeCoverage?.reporters,
236+
excludeAfterRemap: true,
237+
exclude: normalizedOptions.codeCoverage?.exclude,
238+
},
239+
...debugOptions,
227240
},
228241
{
229-
test: {
230-
root: outputPath,
231-
globals: true,
232-
setupFiles,
233-
// Use `jsdom` if no browsers are explicitly configured.
234-
// `node` is effectively no "environment" and the default.
235-
environment: browser ? 'node' : 'jsdom',
236-
watch: normalizedOptions.watch,
237-
browser,
238-
reporters: normalizedOptions.reporters ?? ['default'],
239-
coverage: {
240-
enabled: !!normalizedOptions.codeCoverage,
241-
reporter: normalizedOptions.codeCoverage?.reporters,
242-
excludeAfterRemap: true,
243-
},
244-
...debugOptions,
245-
},
246242
plugins: [
247243
{
248-
name: 'angular-coverage-exclude',
249-
configureVitest(context) {
250-
// Adjust coverage excludes to not include the otherwise automatically inserted included unit tests.
251-
// Vite does this as a convenience but is problematic for the bundling strategy employed by the
252-
// builder's test setup. To workaround this, the excludes are adjusted here to only automatically
253-
// exclude the TypeScript source test files.
254-
context.project.config.coverage.exclude = [
255-
...(normalizedOptions.codeCoverage?.exclude ?? []),
256-
'**/*.{test,spec}.?(c|m)ts',
257-
];
244+
name: 'angular:project-init',
245+
async configureVitest(context) {
246+
// Create a subproject that can be configured with plugins for browser mode.
247+
// Plugins defined directly in the vite overrides will not be present in the
248+
// browser specific Vite instance.
249+
await context.injectTestProjects({
250+
test: {
251+
name: projectName,
252+
root: outputPath,
253+
globals: true,
254+
setupFiles,
255+
// Use `jsdom` if no browsers are explicitly configured.
256+
// `node` is effectively no "environment" and the default.
257+
environment: browser ? 'node' : 'jsdom',
258+
browser,
259+
},
260+
plugins: [
261+
{
262+
name: 'angular:html-index',
263+
transformIndexHtml() {
264+
// Add all global stylesheets
265+
return (
266+
Object.entries(result.files)
267+
// TODO: Expand this to all configured global stylesheets
268+
.filter(([file]) => file === 'styles.css')
269+
.map(([styleUrl]) => ({
270+
tag: 'link',
271+
attrs: {
272+
'href': styleUrl,
273+
'rel': 'stylesheet',
274+
},
275+
injectTo: 'head',
276+
}))
277+
);
278+
},
279+
},
280+
],
281+
});
258282
},
259283
},
260284
],

0 commit comments

Comments
 (0)