@@ -24,11 +24,13 @@ import { OutputHashing } from '../application/schema';
24
24
import { writeTestFiles } from '../karma/application_builder' ;
25
25
import { findTests , getTestEntrypoints } from '../karma/find-tests' ;
26
26
import { useKarmaBuilder } from './karma-bridge' ;
27
- import { normalizeOptions } from './options' ;
27
+ import { NormalizedUnitTestOptions , normalizeOptions } from './options' ;
28
28
import type { Schema as UnitTestOptions } from './schema' ;
29
29
30
30
export type { UnitTestOptions } ;
31
31
32
+ type VitestCoverageOption = Exclude < import ( 'vitest/node' ) . InlineConfig [ 'coverage' ] , undefined > ;
33
+
32
34
/**
33
35
* @experimental Direct usage of this function is considered experimental.
34
36
*/
@@ -230,15 +232,11 @@ export async function* execute(
230
232
include : [ ] ,
231
233
reporters : normalizedOptions . reporters ?? [ 'default' ] ,
232
234
watch : normalizedOptions . watch ,
233
- coverage : {
234
- enabled : ! ! normalizedOptions . codeCoverage ,
235
- excludeAfterRemap : true ,
236
- exclude : normalizedOptions . codeCoverage ?. exclude ?? [ ] ,
237
- // Special handling for `reporter` due to an undefined value causing upstream failures
238
- ...( normalizedOptions . codeCoverage ?. reporters
239
- ? { reporter : normalizedOptions . codeCoverage . reporters }
240
- : { } ) ,
241
- } ,
235
+ coverage : generateCoverageOption (
236
+ normalizedOptions . codeCoverage ,
237
+ workspaceRoot ,
238
+ outputPath ,
239
+ ) ,
242
240
...debugOptions ,
243
241
} ,
244
242
{
@@ -249,7 +247,7 @@ export async function* execute(
249
247
// Create a subproject that can be configured with plugins for browser mode.
250
248
// Plugins defined directly in the vite overrides will not be present in the
251
249
// browser specific Vite instance.
252
- await context . injectTestProjects ( {
250
+ const [ project ] = await context . injectTestProjects ( {
253
251
test : {
254
252
name : projectName ,
255
253
root : outputPath ,
@@ -282,6 +280,15 @@ export async function* execute(
282
280
} ,
283
281
] ,
284
282
} ) ;
283
+
284
+ // Adjust coverage excludes to not include the otherwise automatically inserted included unit tests.
285
+ // Vite does this as a convenience but is problematic for the bundling strategy employed by the
286
+ // builder's test setup. To workaround this, the excludes are adjusted here to only automaticallyAdd commentMore actions
287
+ // exclude the TypeScript source test files.
288
+ project . config . coverage . exclude = [
289
+ ...( normalizedOptions . codeCoverage ?. exclude ?? [ ] ) ,
290
+ '**/*.{test,spec}.?(c|m)ts' ,
291
+ ] ;
285
292
} ,
286
293
} ,
287
294
] ,
@@ -377,3 +384,24 @@ function generateOutputPath(): string {
377
384
378
385
return path . join ( 'dist' , 'test-out' , `${ datePrefix } -${ uuidSuffix } ` ) ;
379
386
}
387
+ function generateCoverageOption (
388
+ codeCoverage : NormalizedUnitTestOptions [ 'codeCoverage' ] ,
389
+ workspaceRoot : string ,
390
+ outputPath : string ,
391
+ ) : VitestCoverageOption {
392
+ if ( ! codeCoverage ) {
393
+ return {
394
+ enabled : false ,
395
+ } ;
396
+ }
397
+
398
+ return {
399
+ enabled : true ,
400
+ excludeAfterRemap : true ,
401
+ include : [ `${ path . relative ( workspaceRoot , outputPath ) } /**` ] ,
402
+ // Special handling for `reporter` due to an undefined value causing upstream failures
403
+ ...( codeCoverage . reporters
404
+ ? ( { reporter : codeCoverage . reporters } satisfies VitestCoverageOption )
405
+ : { } ) ,
406
+ } ;
407
+ }
0 commit comments