Skip to content

Commit c719f34

Browse files
fix: make sure latest vitest versions get properly instrumented (#6773)
1 parent 3ffe266 commit c719f34

File tree

2 files changed

+49
-13
lines changed

2 files changed

+49
-13
lines changed

integration-tests/vitest/vitest.spec.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ const {
5353
DD_CAPABILITIES_IMPACTED_TESTS
5454
} = require('../../packages/dd-trace/src/plugins/util/test')
5555
const { DD_HOST_CPU_COUNT } = require('../../packages/dd-trace/src/plugins/util/env')
56+
const { NODE_MAJOR } = require('../../version')
5657

5758
const NUM_RETRIES_EFD = 3
5859

@@ -399,7 +400,10 @@ versions.forEach((version) => {
399400
})
400401

401402
// total code coverage only works for >=2.0.0
402-
if (version === 'latest') {
403+
// v4 dropped support for Node 18. Every test but this once passes, so we'll leave them
404+
// for now. The breaking change is in https://github.com/vitest-dev/vitest/commit/9a0bf2254
405+
// shipped in https://github.com/vitest-dev/vitest/releases/tag/v4.0.0-beta.12
406+
if (version === 'latest' && NODE_MAJOR >= 20) {
403407
const coverageProviders = ['v8', 'istanbul']
404408

405409
coverageProviders.forEach((coverageProvider) => {

packages/datadog-instrumentations/src/vitest.js

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,10 @@ function isCliApiPackage (vitestPackage) {
153153
return vitestPackage.s?.name === 'startVitest'
154154
}
155155

156+
function isTestPackage (testPackage) {
157+
return testPackage.V?.name === 'VitestTestRunner'
158+
}
159+
156160
function getSessionStatus (state) {
157161
if (state.getCountOfFailedTests() > 0) {
158162
return 'fail'
@@ -240,7 +244,9 @@ function getSortWrapper (sort, frameworkVersion) {
240244
if (isFlakyTestRetriesEnabled && !this.ctx.config.retry && flakyTestRetriesCount > 0) {
241245
this.ctx.config.retry = flakyTestRetriesCount
242246
try {
243-
const workspaceProject = this.ctx.getCoreWorkspaceProject()
247+
const workspaceProject = this.ctx.getCoreWorkspaceProject
248+
? this.ctx.getCoreWorkspaceProject()
249+
: this.ctx.getRootProject()
244250
workspaceProject._provided._ddIsFlakyTestRetriesEnabled = isFlakyTestRetriesEnabled
245251
} catch {
246252
log.warn('Could not send library configuration to workers.')
@@ -272,7 +278,9 @@ function getSortWrapper (sort, frameworkVersion) {
272278
// TODO: use this to pass session and module IDs to the worker, instead of polluting process.env
273279
// Note: setting this.ctx.config.provide directly does not work because it's cached
274280
try {
275-
const workspaceProject = this.ctx.getCoreWorkspaceProject()
281+
const workspaceProject = this.ctx.getCoreWorkspaceProject
282+
? this.ctx.getCoreWorkspaceProject()
283+
: this.ctx.getRootProject()
276284
workspaceProject._provided._ddIsKnownTestsEnabled = isKnownTestsEnabled
277285
workspaceProject._provided._ddKnownTests = knownTests
278286
workspaceProject._provided._ddIsEarlyFlakeDetectionEnabled = isEarlyFlakeDetectionEnabled
@@ -290,7 +298,9 @@ function getSortWrapper (sort, frameworkVersion) {
290298

291299
if (isDiEnabled) {
292300
try {
293-
const workspaceProject = this.ctx.getCoreWorkspaceProject()
301+
const workspaceProject = this.ctx.getCoreWorkspaceProject
302+
? this.ctx.getCoreWorkspaceProject()
303+
: this.ctx.getRootProject()
294304
workspaceProject._provided._ddIsDiEnabled = isDiEnabled
295305
} catch {
296306
log.warn('Could not send Dynamic Instrumentation configuration to workers.')
@@ -305,7 +315,9 @@ function getSortWrapper (sort, frameworkVersion) {
305315
} else {
306316
const testManagementTests = receivedTestManagementTests
307317
try {
308-
const workspaceProject = this.ctx.getCoreWorkspaceProject()
318+
const workspaceProject = this.ctx.getCoreWorkspaceProject
319+
? this.ctx.getCoreWorkspaceProject()
320+
: this.ctx.getRootProject()
309321
workspaceProject._provided._ddIsTestManagementTestsEnabled = isTestManagementTestsEnabled
310322
workspaceProject._provided._ddTestManagementAttemptToFixRetries = testManagementAttemptToFixRetries
311323
workspaceProject._provided._ddTestManagementTests = testManagementTests
@@ -321,7 +333,9 @@ function getSortWrapper (sort, frameworkVersion) {
321333
log.error('Could not get modified tests.')
322334
} else {
323335
try {
324-
const workspaceProject = this.ctx.getCoreWorkspaceProject()
336+
const workspaceProject = this.ctx.getCoreWorkspaceProject
337+
? this.ctx.getCoreWorkspaceProject()
338+
: this.ctx.getRootProject()
325339
workspaceProject._provided._ddIsImpactedTestsEnabled = isImpactedTestsEnabled
326340
workspaceProject._provided._ddModifiedFiles = modifiedFiles
327341
} catch {
@@ -443,13 +457,7 @@ function getStartVitestWrapper (cliApiPackage, frameworkVersion) {
443457
return cliApiPackage
444458
}
445459

446-
addHook({
447-
name: 'vitest',
448-
versions: ['>=1.6.0'],
449-
file: 'dist/runners.js'
450-
}, (vitestPackage) => {
451-
const { VitestTestRunner } = vitestPackage
452-
460+
function wrapVitestTestRunner (VitestTestRunner) {
453461
// `onBeforeRunTask` is run before any repetition or attempt is run
454462
// `onBeforeRunTask` is an async function
455463
shimmer.wrap(VitestTestRunner.prototype, 'onBeforeRunTask', onBeforeRunTask => function (task) {
@@ -744,6 +752,30 @@ addHook({
744752

745753
return result
746754
})
755+
}
756+
757+
addHook({
758+
name: 'vitest',
759+
versions: ['>=4.0.0'],
760+
filePattern: 'dist/chunks/test.*'
761+
}, (testPackage) => {
762+
if (!isTestPackage(testPackage)) {
763+
return testPackage
764+
}
765+
766+
wrapVitestTestRunner(testPackage.V)
767+
768+
return testPackage
769+
})
770+
771+
addHook({
772+
name: 'vitest',
773+
versions: ['>=1.6.0 <4.0.0'],
774+
file: 'dist/runners.js'
775+
}, (vitestPackage) => {
776+
const { VitestTestRunner } = vitestPackage
777+
778+
wrapVitestTestRunner(VitestTestRunner)
747779

748780
return vitestPackage
749781
})

0 commit comments

Comments
 (0)