Skip to content

Commit bb21591

Browse files
committed
tasks
1 parent 5049c48 commit bb21591

File tree

7 files changed

+113
-158
lines changed

7 files changed

+113
-158
lines changed

cypress-backend.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
"integrationFolder": "cypress/test-backend",
44
"env": {
55
"codeCoverage": {
6-
"url": "http://localhost:3003/__coverage__"
6+
"timeout": 60000,
7+
"url": "http://localhost:3003/__coverage__",
8+
"ssr": "/api/__coverage__"
79
}
810
}
911
}

lib/plugin/chromeRemoteInterface.js

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// @ts-check
2+
/**
3+
* Based on
4+
* @see https://github.com/leftyio/v8-cypress-coverage-plugin/blob/master/src/plugin.js
5+
*/
6+
const path = require('path')
7+
const ChromeRemoteInterface = require('chrome-remote-interface')
8+
const {
9+
convertProfileCoverageToIstanbul
10+
} = require('../common/v8ToIstanbul')
11+
const { debug } = require('../common/common-utils');
12+
13+
14+
/**
15+
* @type {ChromeRemoteInterface.Client | null}
16+
*/
17+
let client = null
18+
19+
export function browserLaunchHandler(browser, launchOptions) {
20+
if (browser.name !== 'chrome') {
21+
return debug(
22+
` Warning: An unsupported browser is used, output will not be logged to console: ${browser.name}`
23+
)
24+
}
25+
// find how Cypress is going to control Chrome browser
26+
const rdpArgument = launchOptions.args.find((arg) =>
27+
arg.startsWith('--remote-debugging-port')
28+
)
29+
if (!rdpArgument) {
30+
return debug(
31+
`Could not find launch argument that starts with --remote-debugging-port`
32+
)
33+
}
34+
const rdpPort = Number.parseInt(rdpArgument.split('=')[1])
35+
const tryConnect = () => {
36+
return ChromeRemoteInterface({
37+
port: rdpPort
38+
})
39+
.then((newClient) => {
40+
client = newClient
41+
client.on('disconnect', () => {
42+
client = null
43+
})
44+
})
45+
.catch(() => {
46+
client = null
47+
setTimeout(tryConnect, 100)
48+
})
49+
}
50+
51+
tryConnect()
52+
}
53+
54+
export async function startPreciseCoverage() {
55+
if (!client) {
56+
return
57+
}
58+
59+
await client.Profiler.enable()
60+
await client.Runtime.enable()
61+
await client.Profiler.startPreciseCoverage({
62+
callCount: true,
63+
detailed: true
64+
})
65+
}
66+
67+
export function takePreciseCoverage() {
68+
if (!client) {
69+
return null
70+
}
71+
72+
return client.Profiler.takePreciseCoverage().then((cov) =>
73+
convertProfileCoverageToIstanbul(cov, {
74+
filename: path.join('reports', 'v8_out.json')
75+
})
76+
)
77+
}
78+
79+
export function stopPreciseCoverage() {
80+
if (!client) {
81+
return
82+
}
83+
return client.Profiler.stopPreciseCoverage()
84+
}

lib/plugin/task-utils.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
/// <reference types="cypress" />
66
const { readFileSync, writeFileSync, existsSync } = require('fs')
77
const { isAbsolute, resolve, join } = require('path')
8-
const debug = require('debug')('code-coverage')
98
const chalk = require('chalk')
109
const globby = require('globby')
1110
const yaml = require('js-yaml')
1211
const {
12+
debug,
1313
combineNycOptions,
1414
defaultNycOptions,
1515
fileCoveragePlaceholder
@@ -33,20 +33,26 @@ function readNycOptions(workingDirectory) {
3333
: {}
3434

3535
const nycrcYamlFilename = join(workingDirectory, '.nycrc.yaml')
36+
/**
37+
* @type {unknown}
38+
*/
3639
let nycrcYaml = {}
3740
if (existsSync(nycrcYamlFilename)) {
3841
try {
39-
nycrcYaml = yaml.safeLoad(readFileSync(nycrcYamlFilename, 'utf8'))
42+
nycrcYaml = yaml.load(readFileSync(nycrcYamlFilename, 'utf8'))
4043
} catch (error) {
4144
throw new Error(`Failed to load .nycrc.yaml: ${error.message}`)
4245
}
4346
}
4447

4548
const nycrcYmlFilename = join(workingDirectory, '.nycrc.yml')
49+
/**
50+
* @type {unknown}
51+
*/
4652
let nycrcYml = {}
4753
if (existsSync(nycrcYmlFilename)) {
4854
try {
49-
nycrcYml = yaml.safeLoad(readFileSync(nycrcYmlFilename, 'utf8'))
55+
nycrcYml = yaml.load(readFileSync(nycrcYmlFilename, 'utf8'))
5056
} catch (error) {
5157
throw new Error(`Failed to load .nycrc.yml: ${error.message}`)
5258
}

task.js renamed to lib/plugin/task.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,14 @@ const {
1010
tryFindingLocalFiles,
1111
readNycOptions,
1212
includeAllFiles
13-
} = require('./lib/plugin/task-utils')
14-
const { fixSourcePaths } = require('./lib/support/support-utils')
15-
const { removePlaceholders } = require('./lib/common/common-utils')
16-
17-
const debug = require('debug')('code-coverage')
13+
} = require('./task-utils')
14+
const { fixSourcePaths } = require('../support/support-utils')
15+
const { debug, removePlaceholders } = require('../common/common-utils')
16+
const {
17+
startPreciseCoverage,
18+
takePreciseCoverage,
19+
stopPreciseCoverage
20+
} = require('./chromeRemoteInterface')
1821

1922
// these are standard folder and file names used by NYC tools
2023
const processWorkingDirectory = process.cwd()
@@ -110,6 +113,9 @@ function maybePrintFinalCoverageFiles(folder) {
110113
}
111114

112115
const tasks = {
116+
startPreciseCoverage,
117+
takePreciseCoverage,
118+
stopPreciseCoverage,
113119
/**
114120
* Clears accumulated code coverage information.
115121
*

plugins.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@
44
// "pluginsFile": "cypress-code-coverage-v8/plugins",
55
// "supportFile": "cypress-code-coverage-v8/support"
66
// }
7-
//
7+
8+
const { browserLaunchHandler } = require('./lib/plugin/chromeRemoteInterface')
9+
const addTasks = require('./lib/plugin/task')
10+
811
module.exports = (on, config) => {
9-
require('./task')(on, config)
10-
require('./taskV8')(on, config)
12+
on('before:browser:launch', browserLaunchHandler)
13+
addTasks(on, config)
1114
// IMPORTANT to return the config object
1215
// with the any changed environment variables
1316
return config

task.d.ts

Lines changed: 0 additions & 6 deletions
This file was deleted.

taskV8.js

Lines changed: 0 additions & 140 deletions
This file was deleted.

0 commit comments

Comments
 (0)