Skip to content

Commit bdc0184

Browse files
committed
fix(@angular/build): support injecting global styles into vitest unit-tests
1 parent 3d65518 commit bdc0184

File tree

1 file changed

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

1 file changed

+48
-27
lines changed

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

Lines changed: 48 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -224,37 +224,58 @@ 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+
await context.injectTestProjects({
247+
test: {
248+
name: projectName,
249+
root: outputPath,
250+
globals: true,
251+
setupFiles,
252+
// Use `jsdom` if no browsers are explicitly configured.
253+
// `node` is effectively no "environment" and the default.
254+
environment: browser ? 'node' : 'jsdom',
255+
browser,
256+
},
257+
plugins: [
258+
{
259+
name: 'angular:html-index',
260+
transformIndexHtml() {
261+
// Add all global stylesheets
262+
return (
263+
Object.entries(result.files)
264+
// TODO: Expand this to all configured global stylesheets
265+
.filter(([file]) => file === 'styles.css')
266+
.map(([styleUrl]) => ({
267+
tag: 'link',
268+
attrs: {
269+
'href': styleUrl,
270+
'rel': 'stylesheet',
271+
},
272+
injectTo: 'head',
273+
}))
274+
);
275+
},
276+
},
277+
],
278+
});
258279
},
259280
},
260281
],

0 commit comments

Comments
 (0)