@@ -66,13 +66,8 @@ const registerHooks = () => {
66
66
windowCoverageObjects = [ ]
67
67
68
68
const saveCoverageObject = ( win ) => {
69
- // if the application code has been instrumented, then the app iframe "win.__coverage__" will be available,
70
- // in addition, accessing win.__coverage__ can throw when testing cross-origin code,
71
- // because we don't control the cross-origin code, we can safely return
72
- let applicationSourceCoverage
73
- try {
74
- applicationSourceCoverage = win ?. __coverage__
75
- } catch { }
69
+ // if application code has been instrumented, the app iframe "window" has an object
70
+ const applicationSourceCoverage = win . __coverage__
76
71
if ( ! applicationSourceCoverage ) {
77
72
return
78
73
}
@@ -148,39 +143,49 @@ const registerHooks = () => {
148
143
// we can only request server-side code coverage
149
144
// if we are running end-to-end tests,
150
145
// otherwise where do we send the request?
151
- const url = Cypress . _ . get (
146
+ const captureUrls = Cypress . _ . get (
152
147
Cypress . env ( 'codeCoverage' ) ,
153
148
'url' ,
154
149
'/__coverage__'
155
150
)
156
- cy . request ( {
157
- url,
158
- log : false ,
159
- failOnStatusCode : false
160
- } )
161
- . then ( ( r ) => {
162
- return Cypress . _ . get ( r , 'body.coverage' , null )
151
+ function captureCoverage ( url , suffix = '' ) {
152
+ cy . request ( {
153
+ url,
154
+ log : false ,
155
+ failOnStatusCode : false
163
156
} )
164
- . then ( ( coverage ) => {
165
- if ( ! coverage ) {
166
- // we did not get code coverage - this is the
167
- // original failed request
168
- const expectBackendCoverageOnly = Cypress . _ . get (
169
- Cypress . env ( 'codeCoverage' ) ,
170
- 'expectBackendCoverageOnly' ,
171
- false
172
- )
173
- if ( expectBackendCoverageOnly ) {
174
- throw new Error (
175
- `Expected to collect backend code coverage from ${ url } `
157
+ . then ( ( r ) => {
158
+ return Cypress . _ . get ( r , 'body.coverage' , null )
159
+ } )
160
+ . then ( ( coverage ) => {
161
+ if ( ! coverage ) {
162
+ // we did not get code coverage - this is the
163
+ // original failed request
164
+ const expectBackendCoverageOnly = Cypress . _ . get (
165
+ Cypress . env ( 'codeCoverage' ) ,
166
+ 'expectBackendCoverageOnly' ,
167
+ false
176
168
)
177
- } else {
178
- // we did not really expect to collect the backend code coverage
179
- return
169
+ if ( expectBackendCoverageOnly ) {
170
+ throw new Error (
171
+ `Expected to collect backend code coverage from ${ url } `
172
+ )
173
+ } else {
174
+ // we did not really expect to collect the backend code coverage
175
+ return
176
+ }
180
177
}
181
- }
182
- sendCoverage ( coverage , 'backend' )
183
- } )
178
+ sendCoverage ( coverage , `backend${ suffix } ` )
179
+ } )
180
+ }
181
+
182
+ if ( Array . isArray ( captureUrls ) ) {
183
+ for ( const [ index , url ] of captureUrls . entries ( ) ) {
184
+ captureCoverage ( url , `_${ index } ` )
185
+ }
186
+ } else {
187
+ captureCoverage ( captureUrls )
188
+ }
184
189
}
185
190
} )
186
191
0 commit comments