Skip to content

Commit dcf3c7e

Browse files
[test optimization] Fix vitest latest release (#5123)
1 parent 34499f3 commit dcf3c7e

File tree

5 files changed

+56
-19
lines changed

5 files changed

+56
-19
lines changed

integration-tests/vitest/vitest.spec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ const { DD_HOST_CPU_COUNT } = require('../../packages/dd-trace/src/plugins/util/
3535

3636
const NUM_RETRIES_EFD = 3
3737

38-
const versions = ['2.1.8'] // was previously 'latest', but v3 breaks this test
38+
const versions = ['1.6.0', 'latest']
3939

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

packages/datadog-instrumentations/src/vitest.js

+25-12
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,9 @@ function getSortWrapper (sort) {
179179
const knownTestsResponse = await getChannelPromise(knownTestsCh)
180180
if (!knownTestsResponse.err) {
181181
knownTests = knownTestsResponse.knownTests
182-
const testFilepaths = await this.ctx.getTestFilepaths()
182+
const getFilePaths = this.ctx.getTestFilepaths || this.ctx._globTestFilepaths
183+
184+
const testFilepaths = await getFilePaths.call(this.ctx)
183185

184186
isEarlyFlakeDetectionFaultyCh.publish({
185187
knownTests: knownTests.vitest || {},
@@ -492,15 +494,6 @@ addHook({
492494
return vitestPackage
493495
})
494496

495-
addHook({
496-
name: 'vitest',
497-
versions: ['>=2.1.0'],
498-
filePattern: 'dist/chunks/RandomSequencer.*'
499-
}, (randomSequencerPackage) => {
500-
shimmer.wrap(randomSequencerPackage.B.prototype, 'sort', getSortWrapper)
501-
return randomSequencerPackage
502-
})
503-
504497
addHook({
505498
name: 'vitest',
506499
versions: ['>=2.0.5 <2.1.0'],
@@ -513,6 +506,24 @@ addHook({
513506
return vitestPackage
514507
})
515508

509+
addHook({
510+
name: 'vitest',
511+
versions: ['>=2.1.0 <3.0.0'],
512+
filePattern: 'dist/chunks/RandomSequencer.*'
513+
}, (randomSequencerPackage) => {
514+
shimmer.wrap(randomSequencerPackage.B.prototype, 'sort', getSortWrapper)
515+
return randomSequencerPackage
516+
})
517+
518+
addHook({
519+
name: 'vitest',
520+
versions: ['>=3.0.0'],
521+
filePattern: 'dist/chunks/resolveConfig.*'
522+
}, (randomSequencerPackage) => {
523+
shimmer.wrap(randomSequencerPackage.B.prototype, 'sort', getSortWrapper)
524+
return randomSequencerPackage
525+
})
526+
516527
// Can't specify file because compiled vitest includes hashes in their files
517528
addHook({
518529
name: 'vitest',
@@ -533,15 +544,17 @@ addHook({
533544
versions: ['>=1.6.0'],
534545
file: 'dist/index.js'
535546
}, (vitestPackage, frameworkVersion) => {
536-
shimmer.wrap(vitestPackage, 'startTests', startTests => async function (testPath) {
547+
shimmer.wrap(vitestPackage, 'startTests', startTests => async function (testPaths) {
537548
let testSuiteError = null
538549
if (!testSuiteStartCh.hasSubscribers) {
539550
return startTests.apply(this, arguments)
540551
}
552+
// From >=3.0.1, the first arguments changes from a string to an object containing the filepath
553+
const testSuiteAbsolutePath = testPaths[0]?.filepath || testPaths[0]
541554

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

packages/dd-trace/src/ci-visibility/test-api-manual/test-api-manual-plugin.js

+20-3
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,16 @@ class TestApiManualPlugin extends CiPlugin {
1313

1414
constructor (...args) {
1515
super(...args)
16+
this._isEnvDataCalcualted = false
1617
this.sourceRoot = process.cwd()
1718

18-
this.addSub('dd-trace:ci:manual:test:start', ({ testName, testSuite }) => {
19+
this.unconfiguredAddSub('dd-trace:ci:manual:test:start', ({ testName, testSuite }) => {
1920
const store = storage.getStore()
2021
const testSuiteRelative = getTestSuitePath(testSuite, this.sourceRoot)
2122
const testSpan = this.startTestSpan(testName, testSuiteRelative)
2223
this.enter(testSpan, store)
2324
})
24-
this.addSub('dd-trace:ci:manual:test:finish', ({ status, error }) => {
25+
this.unconfiguredAddSub('dd-trace:ci:manual:test:finish', ({ status, error }) => {
2526
const store = storage.getStore()
2627
const testSpan = store && store.span
2728
if (testSpan) {
@@ -33,14 +34,30 @@ class TestApiManualPlugin extends CiPlugin {
3334
finishAllTraceSpans(testSpan)
3435
}
3536
})
36-
this.addSub('dd-trace:ci:manual:test:addTags', (tags) => {
37+
this.unconfiguredAddSub('dd-trace:ci:manual:test:addTags', (tags) => {
3738
const store = storage.getStore()
3839
const testSpan = store && store.span
3940
if (testSpan) {
4041
testSpan.addTags(tags)
4142
}
4243
})
4344
}
45+
46+
// To lazily calculate env data.
47+
unconfiguredAddSub (channelName, handler) {
48+
this.addSub(channelName, (...args) => {
49+
if (!this._isEnvDataCalcualted) {
50+
this._isEnvDataCalcualted = true
51+
this.configure(this._config, true)
52+
}
53+
return handler(...args)
54+
})
55+
}
56+
57+
configure (config, shouldGetEnvironmentData) {
58+
this._config = config
59+
super.configure(config, shouldGetEnvironmentData)
60+
}
4461
}
4562

4663
module.exports = TestApiManualPlugin

packages/dd-trace/src/plugins/ci_plugin.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -184,14 +184,18 @@ module.exports = class CiPlugin extends Plugin {
184184
}
185185
}
186186

187-
configure (config) {
187+
configure (config, shouldGetEnvironmentData = true) {
188188
super.configure(config)
189189

190-
if (config.isTestDynamicInstrumentationEnabled) {
190+
if (config.isTestDynamicInstrumentationEnabled && !this.di) {
191191
const testVisibilityDynamicInstrumentation = require('../ci-visibility/dynamic-instrumentation')
192192
this.di = testVisibilityDynamicInstrumentation
193193
}
194194

195+
if (!shouldGetEnvironmentData) {
196+
return
197+
}
198+
195199
this.testEnvironmentMetadata = getTestEnvironmentMetadata(this.constructor.id, this.config)
196200

197201
const {

packages/dd-trace/src/proxy.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,10 @@ class Tracer extends NoopProxy {
166166
if (config.isManualApiEnabled) {
167167
const TestApiManualPlugin = require('./ci-visibility/test-api-manual/test-api-manual-plugin')
168168
this._testApiManualPlugin = new TestApiManualPlugin(this)
169-
this._testApiManualPlugin.configure({ ...config, enabled: true })
169+
// `shouldGetEnvironmentData` is passed as false so that we only lazily calculate it
170+
// This is the only place where we need to do this because the rest of the plugins
171+
// are lazily configured when the library is imported.
172+
this._testApiManualPlugin.configure({ ...config, enabled: true }, false)
170173
}
171174
}
172175
if (config.ciVisAgentlessLogSubmissionEnabled) {

0 commit comments

Comments
 (0)