Skip to content

Commit 63f4c03

Browse files
Fix vitest edge case for undefined errors (#5809)
1 parent 4740380 commit 63f4c03

File tree

2 files changed

+33
-28
lines changed

2 files changed

+33
-28
lines changed

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

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -218,37 +218,38 @@ class VitestPlugin extends CiPlugin {
218218
hasFailedAllRetries,
219219
attemptToFixFailed
220220
}) => {
221-
if (span) {
222-
if (shouldSetProbe && this.di) {
223-
const probeInformation = this.addDiProbe(error)
224-
if (probeInformation) {
225-
const { file, line, stackIndex, setProbePromise } = probeInformation
226-
this.runningTestProbe = { file, line }
227-
this.testErrorStackIndex = stackIndex
228-
promises.setProbePromise = setProbePromise
229-
}
221+
if (!span) {
222+
return
223+
}
224+
if (shouldSetProbe && this.di && error?.stack) {
225+
const probeInformation = this.addDiProbe(error)
226+
if (probeInformation) {
227+
const { file, line, stackIndex, setProbePromise } = probeInformation
228+
this.runningTestProbe = { file, line }
229+
this.testErrorStackIndex = stackIndex
230+
promises.setProbePromise = setProbePromise
230231
}
231-
this.telemetry.ciVisEvent(TELEMETRY_EVENT_FINISHED, 'test', {
232-
hasCodeowners: !!span.context()._tags[TEST_CODE_OWNERS]
233-
})
234-
span.setTag(TEST_STATUS, 'fail')
232+
}
233+
this.telemetry.ciVisEvent(TELEMETRY_EVENT_FINISHED, 'test', {
234+
hasCodeowners: !!span.context()._tags[TEST_CODE_OWNERS]
235+
})
236+
span.setTag(TEST_STATUS, 'fail')
235237

236-
if (error) {
237-
span.setTag('error', error)
238-
}
239-
if (hasFailedAllRetries) {
240-
span.setTag(TEST_HAS_FAILED_ALL_RETRIES, 'true')
241-
}
242-
if (attemptToFixFailed) {
243-
span.setTag(TEST_MANAGEMENT_ATTEMPT_TO_FIX_PASSED, 'false')
244-
}
245-
if (duration) {
246-
span.finish(span._startTime + duration - MILLISECONDS_TO_SUBTRACT_FROM_FAILED_TEST_DURATION) // milliseconds
247-
} else {
248-
span.finish() // `duration` is empty for retries, so we'll use clock time
249-
}
250-
finishAllTraceSpans(span)
238+
if (error) {
239+
span.setTag('error', error)
240+
}
241+
if (hasFailedAllRetries) {
242+
span.setTag(TEST_HAS_FAILED_ALL_RETRIES, 'true')
243+
}
244+
if (attemptToFixFailed) {
245+
span.setTag(TEST_MANAGEMENT_ATTEMPT_TO_FIX_PASSED, 'false')
246+
}
247+
if (duration) {
248+
span.finish(span._startTime + duration - MILLISECONDS_TO_SUBTRACT_FROM_FAILED_TEST_DURATION) // milliseconds
249+
} else {
250+
span.finish() // `duration` is empty for retries, so we'll use clock time
251251
}
252+
finishAllTraceSpans(span)
252253
})
253254

254255
this.addSub('ci:vitest:test:skip', ({ testName, testSuiteAbsolutePath, isNew, isDisabled }) => {

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,10 @@ module.exports = class CiPlugin extends Plugin {
435435
}
436436

437437
addDiProbe (err) {
438+
if (!err?.stack) {
439+
log.warn('Can not add breakpoint if the test error does not have a stack')
440+
return
441+
}
438442
const [file, line, stackIndex] = getFileAndLineNumberFromError(err, this.repositoryRoot)
439443

440444
if (!file || !Number.isInteger(line)) {

0 commit comments

Comments
 (0)