@@ -6,6 +6,7 @@ import { expect } from "expect";
66import * as circus from "jest-circus" ;
77import { inspect } from "util" ;
88import { isWorkerThread } from "piscina" ;
9+ import { CoverageInstrumenter } from "collect-v8-coverage" ;
910
1011/** @typedef {{ failures: number, passes: number, pending: number, start: number, end: number } } Stats */
1112/** @typedef {{ ancestors: string[], title: string, duration: number, errors: Error[], skipped: boolean } } InternalTestResult */
@@ -54,6 +55,7 @@ export default async function run({
5455 updateSnapshot,
5556 testNamePattern,
5657 port,
58+ collectV8Coverage,
5759} ) {
5860 const projectSnapshotSerializers = await initialSetup ( test . context . config ) ;
5961
@@ -67,6 +69,13 @@ export default async function run({
6769 /** @type {Array<InternalTestResult> } */
6870 const results = [ ] ;
6971
72+ let instrumenter = null ;
73+ if ( collectV8Coverage ) {
74+ instrumenter = new CoverageInstrumenter ( ) ;
75+
76+ await instrumenter . startInstrumenting ( ) ;
77+ }
78+
7079 const { tests, hasFocusedTests } = await loadTests ( test . path ) ;
7180
7281 const snapshotResolver = await snapshot . buildSnapshotResolver (
@@ -86,13 +95,18 @@ export default async function run({
8695 await runTestBlock ( tests , hasFocusedTests , testNamePatternRE , results , stats ) ;
8796 stats . end = performance . now ( ) ;
8897
98+ let v8Coverage = undefined ;
99+ if ( instrumenter ) {
100+ v8Coverage = await instrumenter . stopInstrumenting ( ) ;
101+ }
102+
89103 snapshotState . save ( ) ;
90104
91105 // Restore the project-level serializers, so that serializers
92106 // installed by one test file don't leak to other files.
93107 arrayReplace ( snapshot . getSerializers ( ) , projectSnapshotSerializers ) ;
94108
95- return toTestResult ( stats , results , test ) ;
109+ return toTestResult ( stats , results , test , v8Coverage ) ;
96110}
97111
98112async function loadTests ( testFile ) {
@@ -236,9 +250,10 @@ function callAsync(fn) {
236250 * @param {Stats } stats
237251 * @param {Array<InternalTestResult> } tests
238252 * @param {import("@jest/test-result").Test } testInput
253+ * @param {import("@jest/test-result").V8CoverageResult } v8Coverage
239254 * @returns {import("@jest/test-result").TestResult }
240255 */
241- function toTestResult ( stats , tests , { path, context } ) {
256+ function toTestResult ( stats , tests , { path, context } , v8Coverage ) {
242257 const { start, end } = stats ;
243258 const runtime = end - start ;
244259
@@ -285,6 +300,7 @@ function toTestResult(stats, tests, { path, context }) {
285300 title : test . title ,
286301 } ;
287302 } ) ,
303+ v8Coverage,
288304 } ;
289305}
290306
0 commit comments