Skip to content

Commit 1d91059

Browse files
BridgeARgiamir
authored andcommitted
fix(vitest): fix coverage not being reported when value is zero (#5681)
* fix(vitest): fix coverage not being reported when value is zero * Add zero coverage test case As drive-by improve the test to use async/await. --------- Co-authored-by: Giamir Buoncristiani <giamir.buoncristiani@gmail.com>
1 parent 01b7e82 commit 1d91059

File tree

3 files changed

+76
-15
lines changed

3 files changed

+76
-15
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { describe, test, expect } from 'vitest'
2+
import { sum } from './sum'
3+
4+
describe('code coverage', () => {
5+
test('passes', () => {
6+
expect(typeof sum === 'function').toBe(true)
7+
})
8+
})

integration-tests/vitest/vitest.spec.js

+67-14
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ const {
4747
TEST_RETRY_REASON_TYPES
4848
} = require('../../packages/dd-trace/src/plugins/util/test')
4949
const { DD_HOST_CPU_COUNT } = require('../../packages/dd-trace/src/plugins/util/env')
50+
const { once } = require('node:events')
5051

5152
const NUM_RETRIES_EFD = 3
5253

@@ -396,7 +397,7 @@ versions.forEach((version) => {
396397
const coverageProviders = ['v8', 'istanbul']
397398

398399
coverageProviders.forEach((coverageProvider) => {
399-
it(`reports code coverage for ${coverageProvider} provider`, (done) => {
400+
it(`reports code coverage for ${coverageProvider} provider`, async () => {
400401
let codeCoverageExtracted
401402
const eventsPromise = receiver
402403
.gatherPayloadsMaxTimeout(({ url }) => url.endsWith('/api/v2/citestcycle'), (payloads) => {
@@ -415,7 +416,7 @@ versions.forEach((version) => {
415416
...getCiVisAgentlessConfig(receiver.port),
416417
NODE_OPTIONS: '--import dd-trace/register.js -r dd-trace/ci/init',
417418
COVERAGE_PROVIDER: coverageProvider,
418-
TEST_DIR: 'ci-visibility/vitest-tests/coverage-test*'
419+
TEST_DIR: 'ci-visibility/vitest-tests/coverage-test.mjs'
419420
},
420421
stdio: 'inherit'
421422
}
@@ -428,20 +429,72 @@ versions.forEach((version) => {
428429
testOutput += chunk.toString()
429430
})
430431

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]
443457
})
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()
444475
})
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+
)
445498
})
446499
}
447500
// maybe only latest version?

packages/datadog-plugin-vitest/src/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ class VitestPlugin extends CiPlugin {
349349
this.testModuleSpan.setTag('error', error)
350350
this.testSessionSpan.setTag('error', error)
351351
}
352-
if (testCodeCoverageLinesTotal) {
352+
if (testCodeCoverageLinesTotal !== undefined) {
353353
this.testModuleSpan.setTag(TEST_CODE_COVERAGE_LINES_PCT, testCodeCoverageLinesTotal)
354354
this.testSessionSpan.setTag(TEST_CODE_COVERAGE_LINES_PCT, testCodeCoverageLinesTotal)
355355
}

0 commit comments

Comments
 (0)