Skip to content

Commit

Permalink
[test-visibility] Fix latest version of vitest (#4565)
Browse files Browse the repository at this point in the history
  • Loading branch information
juan-fernandez committed Aug 5, 2024
1 parent 6662112 commit fb80cec
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 25 deletions.
2 changes: 1 addition & 1 deletion integration-tests/vitest/vitest.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const {
} = require('../../packages/dd-trace/src/plugins/util/test')

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

versions.forEach((version) => {
describe(`vitest@${version}`, () => {
Expand Down
70 changes: 47 additions & 23 deletions packages/datadog-instrumentations/src/vitest.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ function isReporterPackageNew (vitestPackage) {
return vitestPackage.e?.name === 'BaseSequencer'
}

function isReporterPackageNewest (vitestPackage) {
return vitestPackage.h?.name === 'BaseSequencer'
}

function getChannelPromise (channelToPublishTo) {
return new Promise(resolve => {
sessionAsyncResource.runInAsyncScope(() => {
Expand Down Expand Up @@ -146,6 +150,21 @@ function getSortWrapper (sort) {
}
}

function getCreateCliWrapper (vitestPackage, frameworkVersion) {
shimmer.wrap(vitestPackage, 'c', oldCreateCli => function () {
if (!testSessionStartCh.hasSubscribers) {
return oldCreateCli.apply(this, arguments)
}
sessionAsyncResource.runInAsyncScope(() => {
const processArgv = process.argv.slice(2).join(' ')
testSessionStartCh.publish({ command: `vitest ${processArgv}`, frameworkVersion })
})
return oldCreateCli.apply(this, arguments)
})

return vitestPackage
}

addHook({
name: 'vitest',
versions: ['>=1.6.0'],
Expand Down Expand Up @@ -206,12 +225,25 @@ addHook({
return vitestPackage
})

// There are multiple index* files across different versions of vitest,
// so we check for the existence of BaseSequencer to determine if we are in the right file
addHook({
name: 'vitest',
versions: ['>=1.6.0 <2.0.0'],
filePattern: 'dist/vendor/index.*'
}, (vitestPackage) => {
if (isReporterPackage(vitestPackage)) {
shimmer.wrap(vitestPackage.B.prototype, 'sort', getSortWrapper)
}

return vitestPackage
})

addHook({
name: 'vitest',
versions: ['>=2.0.0'],
versions: ['>=2.0.0 <2.0.5'],
filePattern: 'dist/vendor/index.*'
}, (vitestPackage) => {
// there are multiple index* files so we have to check the exported values
if (isReporterPackageNew(vitestPackage)) {
shimmer.wrap(vitestPackage.e.prototype, 'sort', getSortWrapper)
}
Expand All @@ -221,12 +253,11 @@ addHook({

addHook({
name: 'vitest',
versions: ['>=1.6.0'],
filePattern: 'dist/vendor/index.*'
versions: ['>=2.0.5'],
filePattern: 'dist/chunks/index.*'
}, (vitestPackage) => {
// there are multiple index* files so we have to check the exported values
if (isReporterPackage(vitestPackage)) {
shimmer.wrap(vitestPackage.B.prototype, 'sort', getSortWrapper)
if (isReporterPackageNewest(vitestPackage)) {
shimmer.wrap(vitestPackage.h.prototype, 'sort', getSortWrapper)
}

return vitestPackage
Expand All @@ -235,30 +266,23 @@ addHook({
// Can't specify file because compiled vitest includes hashes in their files
addHook({
name: 'vitest',
versions: ['>=1.6.0'],
versions: ['>=1.6.0 <2.0.5'],
filePattern: 'dist/vendor/cac.*'
}, (vitestPackage, frameworkVersion) => {
shimmer.wrap(vitestPackage, 'c', oldCreateCli => function () {
if (!testSessionStartCh.hasSubscribers) {
return oldCreateCli.apply(this, arguments)
}
sessionAsyncResource.runInAsyncScope(() => {
const processArgv = process.argv.slice(2).join(' ')
testSessionStartCh.publish({ command: `vitest ${processArgv}`, frameworkVersion })
})
return oldCreateCli.apply(this, arguments)
})
}, getCreateCliWrapper)

return vitestPackage
})
addHook({
name: 'vitest',
versions: ['>=2.0.5'],
filePattern: 'dist/chunks/cac.*'
}, getCreateCliWrapper)

// test suite start and finish
// only relevant for workers
addHook({
name: '@vitest/runner',
versions: ['>=1.6.0'],
file: 'dist/index.js'
}, vitestPackage => {
}, (vitestPackage, frameworkVersion) => {
shimmer.wrap(vitestPackage, 'startTests', startTests => async function (testPath) {
let testSuiteError = null
if (!testSuiteStartCh.hasSubscribers) {
Expand All @@ -267,7 +291,7 @@ addHook({

const testSuiteAsyncResource = new AsyncResource('bound-anonymous-fn')
testSuiteAsyncResource.runInAsyncScope(() => {
testSuiteStartCh.publish(testPath[0])
testSuiteStartCh.publish({ testSuiteAbsolutePath: testPath[0], frameworkVersion })
})
const startTestsResponse = await startTests.apply(this, arguments)

Expand Down
3 changes: 2 additions & 1 deletion packages/datadog-plugin-vitest/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ class VitestPlugin extends CiPlugin {
).finish()
})

this.addSub('ci:vitest:test-suite:start', (testSuiteAbsolutePath) => {
this.addSub('ci:vitest:test-suite:start', ({ testSuiteAbsolutePath, frameworkVersion }) => {
this.frameworkVersion = frameworkVersion
const testSessionSpanContext = this.tracer.extract('text_map', {
'x-datadog-trace-id': process.env.DD_CIVISIBILITY_TEST_SESSION_ID,
'x-datadog-parent-id': process.env.DD_CIVISIBILITY_TEST_MODULE_ID
Expand Down

0 comments on commit fb80cec

Please sign in to comment.