Skip to content

Commit 9d3a2d6

Browse files
committed
fix merge
1 parent 5333196 commit 9d3a2d6

File tree

4 files changed

+47
-40
lines changed

4 files changed

+47
-40
lines changed

lib/common/common-utils.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,11 @@ const fileCoveragePlaceholder = (fullPath) => {
4646
}
4747
}
4848

49+
const keys = ['statementMap', 'fnMap', 'branchMap', 's', 'f', 'b']
4950
const isPlaceholder = (entry) => {
50-
// when the file has been instrumented, its entry has "hash" property
51-
return !('hash' in entry)
51+
return keys.every((key) => {
52+
return !(key in entry) || Object.keys(entry[key]).length === 0
53+
})
5254
}
5355

5456
/**

lib/plugin/chromeRemoteInterface.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,9 @@ function takePreciseCoverage() {
6767
return null
6868
}
6969

70-
return client.Profiler.takePreciseCoverage().then((cov) =>
71-
convertProfileCoverageToIstanbul(cov)
70+
return client.Profiler.takePreciseCoverage().then((cov) => {
71+
return convertProfileCoverageToIstanbul(cov)
72+
}
7273
)
7374
}
7475

lib/plugin/task.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,10 +153,9 @@ const tasks = {
153153
debug('parsed sent coverage')
154154

155155
fixSourcePaths(coverage)
156-
157156
const previousCoverage = existsSync(nycFilename)
158157
? JSON.parse(readFileSync(nycFilename, 'utf8'))
159-
: {}
158+
: '{}'
160159

161160
// previous code coverage object might have placeholder entries
162161
// for files that we have not seen yet,
@@ -167,10 +166,11 @@ const tasks = {
167166

168167
const coverageMap = istanbul.createCoverageMap(previousCoverage)
169168
coverageMap.merge(coverage)
170-
saveCoverage(coverageMap)
169+
const result = coverageMap.toJSON();
170+
saveCoverage(result)
171171
debug('wrote coverage file %s', nycFilename)
172172

173-
return JSON.stringify(coverageMap)
173+
return JSON.stringify(result)
174174
},
175175

176176
/**

support.js

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,19 @@ const logMessage = (s) => {
2424
* Sends collected code coverage object to the backend code
2525
* via "cy.task".
2626
*/
27-
const sendCoverage = (coverage, url = '/') => {
28-
logMessage(`Saving code coverage for **${url}**`)
27+
const sendCoverage = (coverage, comment = '/') => {
28+
logMessage(`Saving code coverage for **${comment}**`)
2929

3030
const totalCoverage = filterFilesFromCoverage(coverage)
3131

3232
// stringify coverage object for speed
33-
cy.task('combineCoverage', JSON.stringify(totalCoverage), {
34-
log: false
35-
}).then(updatedCoverage => {
36-
console.warn({
37-
url,
38-
coverage,
39-
totalCoverage,
40-
updatedCoverage: JSON.parse(String(updatedCoverage)),
33+
return cy
34+
.task('combineCoverage', JSON.stringify(totalCoverage), {
35+
log: false
4136
})
42-
})
37+
// .then((result) => {
38+
// return JSON.parse(String(result))
39+
// })
4340
}
4441

4542
const registerHooks = () => {
@@ -49,14 +46,13 @@ const registerHooks = () => {
4946
let hostObjects = []
5047

5148
before(() => {
52-
// each object will have the coverage and url pathname
53-
// to let the user know the coverage has been collected
49+
// each object will have the url pathname
50+
// to let the user know the coverage will be collected
5451
hostObjects = []
5552
// we need to reset the coverage when running
5653
// in the interactive mode, otherwise the counters will
5754
// keep increasing every time we rerun the tests
5855
const logInstance = logMessage('Initialize')
59-
6056
cy.task(
6157
'resetCoverage',
6258
{
@@ -110,7 +106,7 @@ const registerHooks = () => {
110106
// collect and merge frontend coverage
111107
cy.task('takePreciseCoverage', null, {
112108
timeout: dayjs.duration(3, 'minutes').asMilliseconds(),
113-
log: true
109+
log: false
114110
}).then((clientCoverage) => {
115111
cy.location({ log: false }).then((loc) => {
116112
if (clientCoverage) {
@@ -127,7 +123,7 @@ const registerHooks = () => {
127123
})
128124
})
129125

130-
after(function collectBackendCoverage() {
126+
after(async function collectBackendCoverage() {
131127
// I wish I could fail the tests if there is no code coverage information
132128
// but throwing an error here does not fail the test run due to
133129
// https://github.com/cypress-io/cypress/issues/2296
@@ -162,36 +158,44 @@ const registerHooks = () => {
162158
})
163159
].filter(Boolean)
164160

165-
finalUrls.forEach((url) => {
166-
return cy
167-
.request({
168-
url,
161+
await Cypress.Promise.mapSeries(finalUrls, (url) => {
162+
return new Cypress.Promise((resolve, reject) => {
163+
cy.request({
164+
url: String(url),
169165
log: true,
170166
failOnStatusCode: false
171167
})
172-
.then((r) => {
173-
return Cypress._.get(r, 'body.coverage', null)
174-
})
175-
.then((coverage) => {
176-
if (!coverage) {
177-
// we did not get code coverage - this is the
178-
// original failed request
168+
.then((r) => {
169+
return Cypress._.get(r, 'body.coverage', null)
170+
})
171+
.then((coverage) => {
172+
if (coverage) {
173+
sendCoverage(coverage, `server - ${url}`).then(() => {
174+
resolve()
175+
})
176+
return
177+
}
178+
179+
// we did not get code coverage
179180
const expectBackendCoverageOnly = Cypress._.get(
180181
Cypress.env('codeCoverage'),
181182
'expectBackendCoverageOnly',
182183
false
183184
)
184185
if (expectBackendCoverageOnly) {
185-
throw new Error(
186-
`Expected to collect backend code coverage from ${url}`
186+
reject(
187+
new Error(
188+
`Expected to collect backend code coverage from ${url}`
189+
)
187190
)
191+
return
188192
} else {
193+
resolve()
189194
// we did not really expect to collect the backend code coverage
190195
return
191196
}
192-
}
193-
sendCoverage(coverage, `server - ${url}`)
194-
})
197+
})
198+
})
195199
})
196200
}
197201
})

0 commit comments

Comments
 (0)