@@ -47,6 +47,7 @@ const {
47
47
TEST_RETRY_REASON_TYPES
48
48
} = require ( '../../packages/dd-trace/src/plugins/util/test' )
49
49
const { DD_HOST_CPU_COUNT } = require ( '../../packages/dd-trace/src/plugins/util/env' )
50
+ const { once } = require ( 'node:events' )
50
51
51
52
const NUM_RETRIES_EFD = 3
52
53
@@ -396,7 +397,7 @@ versions.forEach((version) => {
396
397
const coverageProviders = [ 'v8' , 'istanbul' ]
397
398
398
399
coverageProviders . forEach ( ( coverageProvider ) => {
399
- it ( `reports code coverage for ${ coverageProvider } provider` , ( done ) => {
400
+ it ( `reports code coverage for ${ coverageProvider } provider` , async ( ) => {
400
401
let codeCoverageExtracted
401
402
const eventsPromise = receiver
402
403
. gatherPayloadsMaxTimeout ( ( { url } ) => url . endsWith ( '/api/v2/citestcycle' ) , ( payloads ) => {
@@ -415,7 +416,7 @@ versions.forEach((version) => {
415
416
...getCiVisAgentlessConfig ( receiver . port ) ,
416
417
NODE_OPTIONS : '--import dd-trace/register.js -r dd-trace/ci/init' ,
417
418
COVERAGE_PROVIDER : coverageProvider ,
418
- TEST_DIR : 'ci-visibility/vitest-tests/coverage-test* '
419
+ TEST_DIR : 'ci-visibility/vitest-tests/coverage-test.mjs '
419
420
} ,
420
421
stdio : 'inherit'
421
422
}
@@ -428,20 +429,72 @@ versions.forEach((version) => {
428
429
testOutput += chunk . toString ( )
429
430
} )
430
431
431
- childProcess . on ( 'exit' , ( ) => {
432
- eventsPromise . then ( ( ) => {
433
- const linePctMatch = testOutput . match ( linePctMatchRegex )
434
- const linesPctFromNyc = linePctMatch ? Number ( linePctMatch [ 1 ] ) : null
435
-
436
- assert . equal (
437
- linesPctFromNyc ,
438
- codeCoverageExtracted ,
439
- 'coverage reported by vitest does not match extracted coverage'
440
- )
441
- done ( )
442
- } ) . catch ( done )
432
+ await Promise . all ( [
433
+ once ( childProcess , 'exit' ) ,
434
+ eventsPromise
435
+ ] )
436
+
437
+ const linePctMatch = testOutput . match ( linePctMatchRegex )
438
+ const linesPctFromNyc = Number ( linePctMatch [ 1 ] )
439
+
440
+ assert . strictEqual (
441
+ linesPctFromNyc ,
442
+ codeCoverageExtracted ,
443
+ 'coverage reported by vitest does not match extracted coverage'
444
+ )
445
+ } )
446
+ } )
447
+
448
+ it ( 'reports zero code coverage for instanbul provider' , async ( ) => {
449
+ let codeCoverageExtracted
450
+ const eventsPromise = receiver
451
+ . gatherPayloadsMaxTimeout ( ( { url } ) => url . endsWith ( '/api/v2/citestcycle' ) , ( payloads ) => {
452
+ const events = payloads . flatMap ( ( { payload } ) => payload . events )
453
+
454
+ const testSession = events . find ( event => event . type === 'test_session_end' ) . content
455
+
456
+ codeCoverageExtracted = testSession . metrics [ TEST_CODE_COVERAGE_LINES_PCT ]
443
457
} )
458
+
459
+ childProcess = exec (
460
+ './node_modules/.bin/vitest run --coverage' ,
461
+ {
462
+ cwd,
463
+ env : {
464
+ ...getCiVisAgentlessConfig ( receiver . port ) ,
465
+ NODE_OPTIONS : '--import dd-trace/register.js -r dd-trace/ci/init' ,
466
+ COVERAGE_PROVIDER : 'istanbul' ,
467
+ TEST_DIR : 'ci-visibility/vitest-tests/coverage-test-zero.mjs'
468
+ } ,
469
+ stdio : 'inherit'
470
+ }
471
+ )
472
+
473
+ childProcess . stdout . on ( 'data' , ( chunk ) => {
474
+ testOutput += chunk . toString ( )
444
475
} )
476
+ childProcess . stderr . on ( 'data' , ( chunk ) => {
477
+ testOutput += chunk . toString ( )
478
+ } )
479
+
480
+ await Promise . all ( [
481
+ once ( childProcess , 'exit' ) ,
482
+ eventsPromise
483
+ ] )
484
+
485
+ const linePctMatch = testOutput . match ( linePctMatchRegex )
486
+ const linesPctFromNyc = Number ( linePctMatch [ 1 ] )
487
+
488
+ assert . strictEqual (
489
+ linesPctFromNyc ,
490
+ codeCoverageExtracted ,
491
+ 'coverage reported by vitest does not match extracted coverage'
492
+ )
493
+ assert . strictEqual (
494
+ linesPctFromNyc ,
495
+ 0 ,
496
+ 'zero coverage should be reported'
497
+ )
445
498
} )
446
499
}
447
500
// maybe only latest version?
0 commit comments