Skip to content

Commit 5333196

Browse files
committed
wip
1 parent bb21591 commit 5333196

File tree

12 files changed

+238
-17830
lines changed

12 files changed

+238
-17830
lines changed

lib/common/sourceMap.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const lineLengths = (f) =>
66
.split(/\n|\u2028|\u2029/)
77
.map((l) => l.length)
88

9-
export function getSources(f, url, sourceMapCache = {}) {
9+
function getSources(f, url, sourceMapCache = {}) {
1010
if (sourceMapCache[url]) {
1111
return sourceMapCache[url]
1212
}
@@ -37,3 +37,7 @@ export function getSources(f, url, sourceMapCache = {}) {
3737
return sources
3838
}
3939
}
40+
41+
module.exports = {
42+
getSources,
43+
}

lib/common/v8ToIstanbul.js

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const { fileURLToPath } = require('url')
44
const libCoverage = require('istanbul-lib-coverage')
55
const v8toIstanbul = require('v8-to-istanbul')
66
const { getSources } = require('./sourceMap')
7+
const { debug } = require('./common-utils');
78

89
async function convertToIstanbul(obj, sourceMapCache = {}) {
910
const file = fileURLToPath(obj.url)
@@ -27,13 +28,8 @@ function exists(filename) {
2728
* @see https://github.com/bcoe/c8/issues/376
2829
* @see https://github.com/tapjs/processinfo/blob/33c72e547139630cde35a4126bb4575ad7157065/lib/register-coverage.cjs
2930
*/
30-
async function convertProfileCoverageToIstanbul(cov, { filename = '' } = {}) {
31-
const previousCoverage = filename
32-
? (await exists(filename))
33-
? JSON.parse(await fs.readFile(filename, 'utf8'))
34-
: undefined
35-
: undefined
36-
const map = libCoverage.createCoverageMap(previousCoverage)
31+
async function convertProfileCoverageToIstanbul(cov) {
32+
const map = libCoverage.createCoverageMap()
3733
const sourceMapCache = (cov['source-map-cache'] = {})
3834

3935
const coverages = await Promise.all(
@@ -56,9 +52,9 @@ async function convertProfileCoverageToIstanbul(cov, { filename = '' } = {}) {
5652
}, null)
5753

5854
const result = map.toJSON()
59-
if (filename) {
60-
await fs.writeFile(filename, JSON.stringify(result, null, 2), 'utf8')
61-
}
55+
// if (filename) {
56+
// await fs.writeFile(filename, JSON.stringify(result, null, 2), 'utf8')
57+
// }
6258
return result
6359
}
6460

lib/plugin/chromeRemoteInterface.js

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,19 @@
33
* Based on
44
* @see https://github.com/leftyio/v8-cypress-coverage-plugin/blob/master/src/plugin.js
55
*/
6-
const path = require('path')
76
const ChromeRemoteInterface = require('chrome-remote-interface')
8-
const {
9-
convertProfileCoverageToIstanbul
10-
} = require('../common/v8ToIstanbul')
11-
const { debug } = require('../common/common-utils');
12-
7+
const { convertProfileCoverageToIstanbul } = require('../common/v8ToIstanbul')
8+
const { debug } = require('../common/common-utils')
139

1410
/**
1511
* @type {ChromeRemoteInterface.Client | null}
1612
*/
1713
let client = null
1814

19-
export function browserLaunchHandler(browser, launchOptions) {
15+
function browserLaunchHandler(browser, launchOptions) {
2016
if (browser.name !== 'chrome') {
2117
return debug(
22-
` Warning: An unsupported browser is used, output will not be logged to console: ${browser.name}`
18+
`Warning: An unsupported browser is used, output will not be logged to console: ${browser.name}`
2319
)
2420
}
2521
// find how Cypress is going to control Chrome browser
@@ -51,34 +47,42 @@ export function browserLaunchHandler(browser, launchOptions) {
5147
tryConnect()
5248
}
5349

54-
export async function startPreciseCoverage() {
50+
async function startPreciseCoverage() {
5551
if (!client) {
56-
return
52+
debug('no chrome client')
53+
return null
5754
}
5855

5956
await client.Profiler.enable()
6057
await client.Runtime.enable()
61-
await client.Profiler.startPreciseCoverage({
58+
return client.Profiler.startPreciseCoverage({
6259
callCount: true,
6360
detailed: true
6461
})
6562
}
6663

67-
export function takePreciseCoverage() {
64+
function takePreciseCoverage() {
6865
if (!client) {
66+
debug('no chrome client')
6967
return null
7068
}
7169

7270
return client.Profiler.takePreciseCoverage().then((cov) =>
73-
convertProfileCoverageToIstanbul(cov, {
74-
filename: path.join('reports', 'v8_out.json')
75-
})
71+
convertProfileCoverageToIstanbul(cov)
7672
)
7773
}
7874

79-
export function stopPreciseCoverage() {
75+
function stopPreciseCoverage() {
8076
if (!client) {
81-
return
77+
debug('no chrome client')
78+
return null
8279
}
8380
return client.Profiler.stopPreciseCoverage()
8481
}
82+
83+
module.exports = {
84+
browserLaunchHandler,
85+
startPreciseCoverage,
86+
takePreciseCoverage,
87+
stopPreciseCoverage
88+
}

lib/plugin/task.js

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ const tasks = {
146146
* with previously collected coverage.
147147
*
148148
* @param {string} sentCoverage Stringified coverage object sent by the test runner
149-
* @returns {null} Nothing is returned from this task
149+
* @returns {any} Updated coverage
150150
*/
151151
combineCoverage(sentCoverage) {
152152
const coverage = JSON.parse(sentCoverage)
@@ -170,7 +170,7 @@ const tasks = {
170170
saveCoverage(coverageMap)
171171
debug('wrote coverage file %s', nycFilename)
172172

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

176176
/**
@@ -234,16 +234,6 @@ const tasks = {
234234
* Registers code coverage collection and reporting tasks.
235235
* Sets an environment variable to tell the browser code that it can
236236
* send the coverage.
237-
* @example
238-
```
239-
// your plugins file
240-
module.exports = (on, config) => {
241-
require('cypress-code-coverage-v8/task')(on, config)
242-
// IMPORTANT to return the config object
243-
// with the any changed environment variables
244-
return config
245-
}
246-
```
247237
*/
248238
function registerCodeCoverageTasks(on, config) {
249239
on('task', tasks)

middleware/koa.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const { debug } = require('../lib/common/common-utils');
2121
* "baseUrl": "http://localhost:3000",
2222
* "env": {
2323
* "codeCoverage": {
24-
* "api": "http://localhost:4000/__coverage__"
24+
* "api": "http://localhost:4000/api/__coverage__"
2525
* }
2626
* }
2727
* }
@@ -34,7 +34,8 @@ module.exports = (app) => {
3434
debug('skipping koa middleware, code coverage is not enabled')
3535
return
3636
}
37-
37+
debug('adding koa middleware, code coverage is enabled')
38+
3839
const { takePreciseCoverage } = require('../lib/register/v8Interface')
3940
// expose "GET __coverage__" endpoint that just returns
4041
// global coverage information (if the application has been instrumented)
@@ -43,6 +44,8 @@ module.exports = (app) => {
4344
return next()
4445
}
4546

47+
debug('taking precise coverage')
48+
4649
ctx.body = {
4750
coverage: (await takePreciseCoverage()) || null
4851
}

middleware/nextjs.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ if (isCoverageEnabled()) {
1010
* for Next.js API route. To use, create new `pages/api/coverage.js` file
1111
* and re-export this default middleware function.
1212
*
13-
* @example in your pages/api/coverage.js
13+
* @example in your pages/api/__coverage__.js
1414
* ```ts
15-
* module.exports = require('cypress-code-coverage-v8/middleware/nextjs')
15+
* import coverageHandler from 'cypress-code-coverage-v8/middleware/nextjs';
16+
* export default coverageHandler;
1617
* ```
1718
*
1819
* Then add to your cypress.json an environment variable pointing at the API
@@ -21,7 +22,7 @@ if (isCoverageEnabled()) {
2122
* "baseUrl": "http://localhost:3000",
2223
* "env": {
2324
* "codeCoverage": {
24-
* "ssr": "/api/coverage"
25+
* "ssr": "/api/__coverage__"
2526
* }
2627
* }
2728
* }

0 commit comments

Comments
 (0)