@@ -7,13 +7,14 @@ import path from "path";
77
88import { findUpFile , findUpRoot } from "./findUpDir.mjs" ;
99import cmdLineOptions from "./options.mjs" ;
10- import { exec } from "./utils.mjs" ;
10+ import { exec , ExecError } from "./utils.mjs" ;
1111
1212const mochaJs = path . resolve ( findUpRoot ( ) , "node_modules" , "mocha" , "bin" , "_mocha" ) ;
1313export const localBaseline = "tests/baselines/local/" ;
1414export const refBaseline = "tests/baselines/reference/" ;
1515export const localRwcBaseline = "internal/baselines/rwc/local" ;
1616export const refRwcBaseline = "internal/baselines/rwc/reference" ;
17+ export const coverageDir = "coverage" ;
1718
1819/**
1920 * @param {string } runJs
@@ -35,11 +36,13 @@ export async function runConsoleTests(runJs, defaultReporter, runInParallel, opt
3536 const keepFailed = cmdLineOptions . keepFailed ;
3637 const shards = + cmdLineOptions . shards || undefined ;
3738 const shardId = + cmdLineOptions . shardId || undefined ;
39+ const coverage = cmdLineOptions . coverage ;
3840 if ( ! cmdLineOptions . dirty ) {
3941 if ( options . watching ) {
4042 console . log ( chalk . yellowBright ( `[watch] cleaning test directories...` ) ) ;
4143 }
4244 await cleanTestDirs ( ) ;
45+ await cleanCoverageDir ( ) ;
4346
4447 if ( options . token ?. signaled ) {
4548 return ;
@@ -128,21 +131,28 @@ export async function runConsoleTests(runJs, defaultReporter, runInParallel, opt
128131 /** @type {Error | undefined } */
129132 let error ;
130133
134+ const savedNodeEnv = process . env . NODE_ENV ;
135+ const savedNodeV8Coverage = process . env . NODE_V8_COVERAGE ;
131136 try {
132- setNodeEnvToDevelopment ( ) ;
137+ process . env . NODE_ENV = "development" ;
138+ if ( coverage ) {
139+ process . env . NODE_V8_COVERAGE = path . resolve ( coverageDir , "tmp" ) ;
140+ }
133141
134- const { exitCode } = await exec ( process . execPath , args , { token : options . token } ) ;
135- if ( exitCode !== 0 ) {
136- errorStatus = exitCode ;
137- error = new Error ( `Process exited with status code ${ errorStatus } .` ) ;
142+ await exec ( process . execPath , args , { token : options . token } ) ;
143+ if ( coverage ) {
144+ await exec ( "npm" , [ "--prefer-offline" , "exec" , "--" , "c8" , "report" ] , { token : options . token } ) ;
138145 }
139146 }
140147 catch ( e ) {
141- errorStatus = undefined ;
148+ errorStatus = e instanceof ExecError ? e . exitCode ?? undefined : undefined ;
142149 error = /** @type {Error } */ ( e ) ;
143150 }
144151 finally {
145- restoreSavedNodeEnv ( ) ;
152+ if ( coverage ) {
153+ process . env . NODE_V8_COVERAGE = savedNodeV8Coverage ;
154+ }
155+ process . env . NODE_ENV = savedNodeEnv ;
146156 }
147157
148158 await del ( "test.config" ) ;
@@ -169,6 +179,10 @@ export async function cleanTestDirs() {
169179 await fs . promises . mkdir ( localBaseline , { recursive : true } ) ;
170180}
171181
182+ async function cleanCoverageDir ( ) {
183+ await del ( [ coverageDir ] ) ;
184+ }
185+
172186/**
173187 * used to pass data from command line directly to run.js
174188 * @param {string } tests
@@ -200,17 +214,6 @@ export function writeTestConfigFile(tests, runners, light, taskConfigsFolder, wo
200214 fs . writeFileSync ( "test.config" , testConfigContents ) ;
201215}
202216
203- /** @type {string | undefined } */
204- let savedNodeEnv ;
205- function setNodeEnvToDevelopment ( ) {
206- savedNodeEnv = process . env . NODE_ENV ;
207- process . env . NODE_ENV = "development" ;
208- }
209-
210- function restoreSavedNodeEnv ( ) {
211- process . env . NODE_ENV = savedNodeEnv ;
212- }
213-
214217function deleteTemporaryProjectOutput ( ) {
215218 return del ( path . join ( localBaseline , "projectOutput/" ) ) ;
216219}
0 commit comments