Skip to content

Commit

Permalink
add e2e test
Browse files Browse the repository at this point in the history
  • Loading branch information
juan-fernandez committed Aug 2, 2024
1 parent 72e1258 commit 751140f
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { describe, test, expect } from 'vitest'
import { sum } from './sum'

describe('code coverage', () => {
test('passes', () => {
expect(sum(1, 2)).to.equal(3)
})
})
7 changes: 6 additions & 1 deletion integration-tests/vitest.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ export default defineConfig({
test: {
include: [
process.env.TEST_DIR || 'ci-visibility/vitest-tests/test-visibility*'
]
],
coverage: {
provider: process.env.COVERAGE_PROVIDER || 'v8',
include: ['ci-visibility/vitest-tests/**'],
reporter: 'text-summary'
}
}
})
69 changes: 65 additions & 4 deletions integration-tests/vitest/vitest.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,24 @@ const {
TEST_STATUS,
TEST_TYPE,
TEST_IS_RETRY,
TEST_CODE_OWNERS
TEST_CODE_OWNERS,
TEST_CODE_COVERAGE_LINES_PCT
} = require('../../packages/dd-trace/src/plugins/util/test')

// tested with 1.6.0
const versions = ['1.6.0', 'latest']

const linePctMatchRegex = /Lines\s+:\s+([\d.]+)%/

versions.forEach((version) => {
describe(`vitest@${version}`, () => {
let sandbox, cwd, receiver, childProcess
let sandbox, cwd, receiver, childProcess, testOutput

before(async function () {
sandbox = await createSandbox([`vitest@${version}`], true)
sandbox = await createSandbox([
`vitest@${version}`,
`@vitest/coverage-istanbul@${version}`,
`@vitest/coverage-v8@${version}`
], true)
cwd = sandbox.folder
})

Expand All @@ -37,6 +43,7 @@ versions.forEach((version) => {
})

afterEach(async () => {
testOutput = ''
childProcess.kill()
await receiver.stop()
})
Expand Down Expand Up @@ -233,5 +240,59 @@ versions.forEach((version) => {
}).catch(done)
})
})

// only works for >=2.0.0
if (version === 'latest') {
const coverageProviders = ['v8', 'istanbul']

coverageProviders.forEach((coverageProvider) => {
it(`reports code coverage for ${coverageProvider} provider`, (done) => {
let codeCoverageExtracted
const eventsPromise = receiver
.gatherPayloadsMaxTimeout(({ url }) => url.endsWith('/api/v2/citestcycle'), (payloads) => {
const events = payloads.flatMap(({ payload }) => payload.events)

const testSession = events.find(event => event.type === 'test_session_end').content

codeCoverageExtracted = testSession.metrics[TEST_CODE_COVERAGE_LINES_PCT]
})

childProcess = exec(
'./node_modules/.bin/vitest run --coverage',
{
cwd,
env: {
...getCiVisAgentlessConfig(receiver.port),
NODE_OPTIONS: '--import dd-trace/register.js -r dd-trace/ci/init',
COVERAGE_PROVIDER: coverageProvider,
TEST_DIR: 'ci-visibility/vitest-tests/coverage-test*'
},
stdio: 'inherit'
}
)

childProcess.stdout.on('data', (chunk) => {
testOutput += chunk.toString()
})
childProcess.stderr.on('data', (chunk) => {
testOutput += chunk.toString()
})

childProcess.on('exit', () => {
eventsPromise.then(() => {
const linePctMatch = testOutput.match(linePctMatchRegex)
const linesPctFromNyc = linePctMatch ? Number(linePctMatch[1]) : null

assert.equal(
linesPctFromNyc,
codeCoverageExtracted,
'coverage reported by vitest does not match extracted coverage'
)
done()
}).catch(done)
})
})
})
}
})
})
2 changes: 1 addition & 1 deletion packages/datadog-instrumentations/src/vitest.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ function getSortWrapper (sort) {

let testCodeCoverageLinesTotal

if (this.ctx.coverageProvider) {
if (this.ctx.coverageProvider?.generateCoverage) {
shimmer.wrap(this.ctx.coverageProvider, 'generateCoverage', generateCoverage => async function () {
const totalCodeCoverage = await generateCoverage.apply(this, arguments)

Expand Down

0 comments on commit 751140f

Please sign in to comment.