@circleci/v8-coverage-collector 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Circle Internet Services, Inc.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,76 @@
1
+ # v8-coverage-collector
2
+
3
+ Wrapper around v8's Profiler APIs used to gather coverage data for CircleCI's Smarter Testing Javascript plugins.
4
+
5
+ The coverage collector can be used to collect precise coverage during the test run lifecycles.
6
+
7
+ ## Usage
8
+
9
+ Add the package as a dependency
10
+
11
+ ```shell
12
+ pnpm i @circleci/v8-coverage-collector
13
+ ```
14
+
15
+ Use the provided APIs to collect and reset coverage during the test lifecycle events.
16
+
17
+ ```ts
18
+ import {V8CoverageCollector} from '@circleci/v8-coverage-collector';
19
+
20
+ export default class TestHookExample {
21
+ private inspector: V8CoverageCollector;
22
+
23
+ constructor() {
24
+ this.inspector = new V8CoverageCollector();
25
+ }
26
+
27
+ async onBeforeAll(): Promise<void> {
28
+ await this.inspector.connect()
29
+ }
30
+
31
+ async onBeforeTest(): Promise<void> {
32
+ await this.inspector.resetCoverage();
33
+ }
34
+
35
+ async onAfterTest(test: ExampleTest): Promise<void> {
36
+ await this.inspector
37
+ .collectCoverage(process.cwd(), test.file, test.name)
38
+ .then((result) => {
39
+ test.meta.testKey = result.testKey;
40
+ test.meta.coveredFiles = result.coveredFiles;
41
+ });
42
+ }
43
+
44
+ async onAfterAll(): Promise<void> {
45
+ await this.inspector.disconnect()
46
+ }
47
+ }
48
+ ```
49
+
50
+ ## Development
51
+
52
+ Install and use current node version.
53
+
54
+ ```shell
55
+ NODE_VER=$(cat ./.nvmrc)
56
+ nvm install $NODE_VER
57
+ nvm use $NODE_VER
58
+ ```
59
+
60
+ Install dependencies with pnpm.
61
+
62
+ ```shell
63
+ pnpm install
64
+ ```
65
+
66
+ Build the plugin.
67
+
68
+ ```shell
69
+ pnpm build
70
+ ```
71
+
72
+ Run tests.
73
+
74
+ ```shell
75
+ pnpm test
76
+ ```
@@ -0,0 +1,106 @@
1
+ /**
2
+ * This module contains the CircleCI v8 coverage collector, used to gather
3
+ * coverage data for CircleCI's Smarter Testing JavaScript plugins.
4
+ *
5
+ * The coverage collector can be used to collect precise coverage during the
6
+ * test run lifecycles.
7
+ *
8
+ * @example
9
+ * ```ts
10
+ * import { V8CoverageCollector } from '@circleci/v8-coverage-collector';
11
+ *
12
+ * export default class TestHookExample {
13
+ * private inspector: V8CoverageCollector;
14
+ *
15
+ * constructor() {
16
+ * this.inspector = new V8CoverageCollector();
17
+ * }
18
+ *
19
+ * async onBeforeAll(): Promise<void> {
20
+ * await this.inspector.connect()
21
+ * }
22
+ *
23
+ * async onBeforeTest(): Promise<void> {
24
+ * await this.inspector.resetCoverage();
25
+ * }
26
+ *
27
+ * async onAfterTest(test: ExampleTest): Promise<void> {
28
+ * await this.inspector
29
+ * .collectCoverage(process.cwd(), test.file, test.class, test.name)
30
+ * .then((result) => {
31
+ * test.meta.testKey = result.testKey;
32
+ * test.meta.coveredFiles = result.coveredFiles;
33
+ * });
34
+ * }
35
+ *
36
+ * async onAfterAll(): Promise<void> {
37
+ * await this.inspector.disconnect()
38
+ * }
39
+ * }
40
+ * ```
41
+ *
42
+ * @module
43
+ */
44
+ /**
45
+ * TestCoverage is the response from {@link collectCoverage}
46
+ * for a given test file and name.
47
+ */
48
+ export interface TestCoverage {
49
+ /**
50
+ * testKey is unique identifier for a given test.
51
+ *
52
+ * @example `{test file}::{test name}|{phase}`
53
+ *
54
+ * */
55
+ testKey: string;
56
+ /**
57
+ * coveredFiles is a list of found covered files, relative
58
+ * to the given working directory.
59
+ */
60
+ coveredFiles: string[];
61
+ }
62
+ /**
63
+ * V8CoverageCollector provides APIs for collecting coverage using the
64
+ * v8 Profile APIs.
65
+ */
66
+ export declare class V8CoverageCollector {
67
+ private readonly session;
68
+ private readonly post;
69
+ constructor();
70
+ /**
71
+ * connect connects and enables the v8 Profiler APIs.
72
+ *
73
+ * @returns {Promise<void>}
74
+ */
75
+ connect(): Promise<void>;
76
+ /**
77
+ * disconnect disconnects and disables the v8 Profiler APIs.
78
+ *
79
+ * @returns {Promise<void>}
80
+ */
81
+ disconnect(): Promise<void>;
82
+ /**
83
+ * resetCoverage resets the execution counts of the coverage.
84
+ *
85
+ * @returns {Promise<void>}
86
+ */
87
+ resetCoverage(): Promise<void>;
88
+ /**
89
+ * collectCoverage collects coverage data since the last time
90
+ * {@link resetCoverage} was called.
91
+ *
92
+ * @param {string} wd
93
+ * @param {string} testFile
94
+ * @param {...string} scopes - one or more additional scope segments e.g. testClass, testName
95
+ * @returns {Promise<TestCoverage>}
96
+ */
97
+ collectCoverage(wd: string, testFile: string, ...scopes: string[]): Promise<TestCoverage>;
98
+ private testKey;
99
+ private coveredFiles;
100
+ private enable;
101
+ private disable;
102
+ private startPreciseCoverage;
103
+ private stopPreciseCoverage;
104
+ private takePreciseCoverage;
105
+ }
106
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA0CG;AAaH;;;GAGG;AACH,MAAM,WAAW,YAAY;IAC3B;;;;;SAKK;IACL,OAAO,EAAE,MAAM,CAAC;IAChB;;;OAGG;IACH,YAAY,EAAE,MAAM,EAAE,CAAC;CACxB;AAED;;;GAGG;AACH,qBAAa,mBAAmB;IAC9B,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAoB;IAC5C,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAwD;;IAU7E;;;;OAIG;IACG,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;IAM9B;;;;OAIG;IACG,UAAU,IAAI,OAAO,CAAC,IAAI,CAAC;IAMjC;;;;OAIG;IACG,aAAa,IAAI,OAAO,CAAC,IAAI,CAAC;IAIpC;;;;;;;;OAQG;IACG,eAAe,CACnB,EAAE,EAAE,MAAM,EACV,QAAQ,EAAE,MAAM,EAChB,GAAG,MAAM,EAAE,MAAM,EAAE,GAClB,OAAO,CAAC,YAAY,CAAC;IAUxB,OAAO,CAAC,OAAO;IAKf,OAAO,CAAC,YAAY;YAcN,MAAM;YAIN,OAAO;YAIP,oBAAoB;YAOpB,mBAAmB;YAInB,mBAAmB;CAMlC"}
package/dist/index.js ADDED
@@ -0,0 +1,138 @@
1
+ /**
2
+ * This module contains the CircleCI v8 coverage collector, used to gather
3
+ * coverage data for CircleCI's Smarter Testing JavaScript plugins.
4
+ *
5
+ * The coverage collector can be used to collect precise coverage during the
6
+ * test run lifecycles.
7
+ *
8
+ * @example
9
+ * ```ts
10
+ * import { V8CoverageCollector } from '@circleci/v8-coverage-collector';
11
+ *
12
+ * export default class TestHookExample {
13
+ * private inspector: V8CoverageCollector;
14
+ *
15
+ * constructor() {
16
+ * this.inspector = new V8CoverageCollector();
17
+ * }
18
+ *
19
+ * async onBeforeAll(): Promise<void> {
20
+ * await this.inspector.connect()
21
+ * }
22
+ *
23
+ * async onBeforeTest(): Promise<void> {
24
+ * await this.inspector.resetCoverage();
25
+ * }
26
+ *
27
+ * async onAfterTest(test: ExampleTest): Promise<void> {
28
+ * await this.inspector
29
+ * .collectCoverage(process.cwd(), test.file, test.class, test.name)
30
+ * .then((result) => {
31
+ * test.meta.testKey = result.testKey;
32
+ * test.meta.coveredFiles = result.coveredFiles;
33
+ * });
34
+ * }
35
+ *
36
+ * async onAfterAll(): Promise<void> {
37
+ * await this.inspector.disconnect()
38
+ * }
39
+ * }
40
+ * ```
41
+ *
42
+ * @module
43
+ */
44
+ import inspector from 'node:inspector';
45
+ import { promisify } from 'node:util';
46
+ import { relative } from 'node:path';
47
+ import { fileURLToPath } from 'node:url';
48
+ /**
49
+ * V8CoverageCollector provides APIs for collecting coverage using the
50
+ * v8 Profile APIs.
51
+ */
52
+ export class V8CoverageCollector {
53
+ constructor() {
54
+ this.session = new inspector.Session();
55
+ this.post = promisify(this.session.post.bind(this.session));
56
+ }
57
+ /**
58
+ * connect connects and enables the v8 Profiler APIs.
59
+ *
60
+ * @returns {Promise<void>}
61
+ */
62
+ async connect() {
63
+ this.session.connect();
64
+ await this.enable();
65
+ await this.startPreciseCoverage();
66
+ }
67
+ /**
68
+ * disconnect disconnects and disables the v8 Profiler APIs.
69
+ *
70
+ * @returns {Promise<void>}
71
+ */
72
+ async disconnect() {
73
+ await this.stopPreciseCoverage();
74
+ await this.disable();
75
+ this.session.disconnect();
76
+ }
77
+ /**
78
+ * resetCoverage resets the execution counts of the coverage.
79
+ *
80
+ * @returns {Promise<void>}
81
+ */
82
+ async resetCoverage() {
83
+ await this.takePreciseCoverage();
84
+ }
85
+ /**
86
+ * collectCoverage collects coverage data since the last time
87
+ * {@link resetCoverage} was called.
88
+ *
89
+ * @param {string} wd
90
+ * @param {string} testFile
91
+ * @param {...string} scopes - one or more additional scope segments e.g. testClass, testName
92
+ * @returns {Promise<TestCoverage>}
93
+ */
94
+ async collectCoverage(wd, testFile, ...scopes) {
95
+ return this.takePreciseCoverage().then((result) => {
96
+ const testKey = this.testKey(wd, testFile, ...scopes);
97
+ return {
98
+ testKey: testKey,
99
+ coveredFiles: this.coveredFiles(wd, result),
100
+ };
101
+ });
102
+ }
103
+ testKey(wd, testFile, ...scopes) {
104
+ const s = [relative(wd, testFile), ...scopes];
105
+ return `${s.join('!!')}|run`;
106
+ }
107
+ coveredFiles(wd, result) {
108
+ return result
109
+ .map((s) => s.url)
110
+ .filter((s) => {
111
+ const url = URL.canParse(s) && new URL(s);
112
+ return (url &&
113
+ url.protocol === 'file:' &&
114
+ !url.pathname.includes('node_modules'));
115
+ })
116
+ .map((url) => relative(wd, fileURLToPath(url)));
117
+ }
118
+ async enable() {
119
+ await this.post('Profiler.enable');
120
+ }
121
+ async disable() {
122
+ await this.post('Profiler.disable');
123
+ }
124
+ async startPreciseCoverage() {
125
+ await this.post('Profiler.startPreciseCoverage', {
126
+ callCount: true,
127
+ detailed: false,
128
+ });
129
+ }
130
+ async stopPreciseCoverage() {
131
+ await this.post('Profiler.stopPreciseCoverage');
132
+ }
133
+ async takePreciseCoverage() {
134
+ const result = (await this.post('Profiler.takePreciseCoverage'));
135
+ return result.result;
136
+ }
137
+ }
138
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA0CG;AAEH,OAAO,SAAS,MAAM,gBAAgB,CAAC;AACvC,OAAO,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AACtC,OAAO,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAC;AACrC,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AA2BzC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C;QACF,OAAO,MAAM,CAAC,MAAM,CAAC;IACvB,CAAC;CACF"}
package/package.json ADDED
@@ -0,0 +1,40 @@
1
+ {
2
+ "name": "@circleci/v8-coverage-collector",
3
+ "version": "0.2.0",
4
+ "description": "",
5
+ "type": "module",
6
+ "main": "./dist/index.js",
7
+ "types": "./dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "default": "./dist/index.js"
12
+ }
13
+ },
14
+ "files": [
15
+ "dist"
16
+ ],
17
+ "publishConfig": {
18
+ "access": "public"
19
+ },
20
+ "scripts": {
21
+ "build": "tsc",
22
+ "test": "vitest run",
23
+ "lint": "oxlint --quiet",
24
+ "format": "prettier . --check"
25
+ },
26
+ "devDependencies": {
27
+ "@prettier/plugin-oxc": "^0.2.0",
28
+ "@types/node": "^24.0.0",
29
+ "oxlint": "^1.43.0",
30
+ "prettier": "^3.8.1",
31
+ "typescript": "^6.0.0",
32
+ "vitest": "^4.1.4"
33
+ },
34
+ "engines": {
35
+ "node": ">=20.0.0"
36
+ },
37
+ "dependencies": {
38
+ "npm": "~11.15.0"
39
+ }
40
+ }