@@ -29,12 +29,16 @@ var __importStar = (this && this.__importStar) || function (mod) {
29
29
__setModuleDefault ( result , mod ) ;
30
30
return result ;
31
31
} ;
32
+ var __importDefault = ( this && this . __importDefault ) || function ( mod ) {
33
+ return ( mod && mod . __esModule ) ? mod : { "default" : mod } ;
34
+ } ;
32
35
Object . defineProperty ( exports , "__esModule" , { value : true } ) ;
33
36
exports . execute = execute ;
34
37
exports . writeTestFiles = writeTestFiles ;
35
38
const build_1 = require ( "@angular/build" ) ;
36
39
const private_1 = require ( "@angular/build/private" ) ;
37
40
const crypto_1 = require ( "crypto" ) ;
41
+ const fast_glob_1 = __importDefault ( require ( "fast-glob" ) ) ;
38
42
const fs = __importStar ( require ( "fs/promises" ) ) ;
39
43
const path = __importStar ( require ( "path" ) ) ;
40
44
const rxjs_1 = require ( "rxjs" ) ;
@@ -75,8 +79,7 @@ async function getProjectSourceRoot(context) {
75
79
const sourceRoot = ( projectMetadata . sourceRoot ?? projectMetadata . root ?? '' ) ;
76
80
return path . join ( context . workspaceRoot , sourceRoot ) ;
77
81
}
78
- async function collectEntrypoints ( options , context ) {
79
- const projectSourceRoot = await getProjectSourceRoot ( context ) ;
82
+ async function collectEntrypoints ( options , context , projectSourceRoot ) {
80
83
// Glob for files to test.
81
84
const testFiles = await ( 0 , find_tests_1 . findTests ) ( options . include ?? [ ] , options . exclude ?? [ ] , context . workspaceRoot , projectSourceRoot ) ;
82
85
const entryPoints = new Set ( [
@@ -95,12 +98,16 @@ async function initializeApplication(options, context, karmaOptions, transforms
95
98
context . logger . warn ( `This build is using the application builder but transforms.webpackConfiguration was provided. The transform will be ignored.` ) ;
96
99
}
97
100
const testDir = path . join ( context . workspaceRoot , 'dist/test-out' , ( 0 , crypto_1 . randomUUID ) ( ) ) ;
101
+ const projectSourceRoot = await getProjectSourceRoot ( context ) ;
98
102
const [ karma , [ entryPoints , polyfills ] ] = await Promise . all ( [
99
103
Promise . resolve ( ) . then ( ( ) => __importStar ( require ( 'karma' ) ) ) ,
100
- collectEntrypoints ( options , context ) ,
104
+ collectEntrypoints ( options , context , projectSourceRoot ) ,
101
105
fs . rm ( testDir , { recursive : true , force : true } ) ,
102
106
] ) ;
103
107
const outputPath = testDir ;
108
+ const instrumentForCoverage = options . codeCoverage
109
+ ? createInstrumentationFilter ( projectSourceRoot , getInstrumentationExcludedPaths ( context . workspaceRoot , options . codeCoverageExclude ?? [ ] ) )
110
+ : undefined ;
104
111
// Build tests with `application` builder, using test files as entry points.
105
112
const buildOutput = await first ( ( 0 , private_1 . buildApplicationInternal ) ( {
106
113
entryPoints,
@@ -115,6 +122,7 @@ async function initializeApplication(options, context, karmaOptions, transforms
115
122
styles : true ,
116
123
vendor : true ,
117
124
} ,
125
+ instrumentForCoverage,
118
126
styles : options . styles ,
119
127
polyfills,
120
128
webWorkerTsConfig : options . webWorkerTsConfig ,
@@ -207,3 +215,18 @@ async function first(generator) {
207
215
}
208
216
throw new Error ( 'Expected generator to emit at least once.' ) ;
209
217
}
218
+ function createInstrumentationFilter ( includedBasePath , excludedPaths ) {
219
+ return ( request ) => {
220
+ return ( ! excludedPaths . has ( request ) &&
221
+ ! / \. ( e 2 e | s p e c ) \. t s x ? $ | [ \\ / ] n o d e _ m o d u l e s [ \\ / ] / . test ( request ) &&
222
+ request . startsWith ( includedBasePath ) ) ;
223
+ } ;
224
+ }
225
+ function getInstrumentationExcludedPaths ( root , excludedPaths ) {
226
+ const excluded = new Set ( ) ;
227
+ for ( const excludeGlob of excludedPaths ) {
228
+ const excludePath = excludeGlob [ 0 ] === '/' ? excludeGlob . slice ( 1 ) : excludeGlob ;
229
+ fast_glob_1 . default . sync ( excludePath , { cwd : root } ) . forEach ( ( p ) => excluded . add ( path . join ( root , p ) ) ) ;
230
+ }
231
+ return excluded ;
232
+ }
0 commit comments