Skip to content

Commit 59e9a2a

Browse files
[test optimization] Fix active span being null in cypress (#4863)
1 parent 9146f26 commit 59e9a2a

1 file changed

Lines changed: 46 additions & 44 deletions

File tree

packages/datadog-plugin-cypress/src/cypress-plugin.js

Lines changed: 46 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -654,55 +654,57 @@ class CypressPlugin {
654654
return this.activeTestSpan ? { traceId: this.activeTestSpan.context().toTraceId() } : {}
655655
},
656656
'dd:afterEach': ({ test, coverage }) => {
657+
if (!this.activeTestSpan) {
658+
log.warn('There is no active test span in dd:afterEach handler')
659+
return null
660+
}
657661
const { state, error, isRUMActive, testSourceLine, testSuite, testName, isNew, isEfdRetry } = test
658-
if (this.activeTestSpan) {
659-
if (coverage && this.isCodeCoverageEnabled && this.tracer._tracer._exporter?.exportCoverage) {
660-
const coverageFiles = getCoveredFilenamesFromCoverage(coverage)
661-
const relativeCoverageFiles = coverageFiles.map(file => getTestSuitePath(file, this.rootDir))
662-
if (!relativeCoverageFiles.length) {
663-
incrementCountMetric(TELEMETRY_CODE_COVERAGE_EMPTY)
664-
}
665-
distributionMetric(TELEMETRY_CODE_COVERAGE_NUM_FILES, {}, relativeCoverageFiles.length)
666-
const { _traceId, _spanId } = this.testSuiteSpan.context()
667-
const formattedCoverage = {
668-
sessionId: _traceId,
669-
suiteId: _spanId,
670-
testId: this.activeTestSpan.context()._spanId,
671-
files: relativeCoverageFiles
672-
}
673-
this.tracer._tracer._exporter.exportCoverage(formattedCoverage)
674-
}
675-
const testStatus = CYPRESS_STATUS_TO_TEST_STATUS[state]
676-
this.activeTestSpan.setTag(TEST_STATUS, testStatus)
677-
678-
if (error) {
679-
this.activeTestSpan.setTag('error', error)
680-
}
681-
if (isRUMActive) {
682-
this.activeTestSpan.setTag(TEST_IS_RUM_ACTIVE, 'true')
683-
}
684-
if (testSourceLine) {
685-
this.activeTestSpan.setTag(TEST_SOURCE_START, testSourceLine)
686-
}
687-
if (isNew) {
688-
this.activeTestSpan.setTag(TEST_IS_NEW, 'true')
689-
if (isEfdRetry) {
690-
this.activeTestSpan.setTag(TEST_IS_RETRY, 'true')
691-
}
662+
if (coverage && this.isCodeCoverageEnabled && this.tracer._tracer._exporter?.exportCoverage) {
663+
const coverageFiles = getCoveredFilenamesFromCoverage(coverage)
664+
const relativeCoverageFiles = coverageFiles.map(file => getTestSuitePath(file, this.rootDir))
665+
if (!relativeCoverageFiles.length) {
666+
incrementCountMetric(TELEMETRY_CODE_COVERAGE_EMPTY)
692667
}
693-
const finishedTest = {
694-
testName,
695-
testStatus,
696-
finishTime: this.activeTestSpan._getTime(), // we store the finish time here
697-
testSpan: this.activeTestSpan
668+
distributionMetric(TELEMETRY_CODE_COVERAGE_NUM_FILES, {}, relativeCoverageFiles.length)
669+
const { _traceId, _spanId } = this.testSuiteSpan.context()
670+
const formattedCoverage = {
671+
sessionId: _traceId,
672+
suiteId: _spanId,
673+
testId: this.activeTestSpan.context()._spanId,
674+
files: relativeCoverageFiles
698675
}
699-
if (this.finishedTestsByFile[testSuite]) {
700-
this.finishedTestsByFile[testSuite].push(finishedTest)
701-
} else {
702-
this.finishedTestsByFile[testSuite] = [finishedTest]
676+
this.tracer._tracer._exporter.exportCoverage(formattedCoverage)
677+
}
678+
const testStatus = CYPRESS_STATUS_TO_TEST_STATUS[state]
679+
this.activeTestSpan.setTag(TEST_STATUS, testStatus)
680+
681+
if (error) {
682+
this.activeTestSpan.setTag('error', error)
683+
}
684+
if (isRUMActive) {
685+
this.activeTestSpan.setTag(TEST_IS_RUM_ACTIVE, 'true')
686+
}
687+
if (testSourceLine) {
688+
this.activeTestSpan.setTag(TEST_SOURCE_START, testSourceLine)
689+
}
690+
if (isNew) {
691+
this.activeTestSpan.setTag(TEST_IS_NEW, 'true')
692+
if (isEfdRetry) {
693+
this.activeTestSpan.setTag(TEST_IS_RETRY, 'true')
703694
}
704-
// test spans are finished at after:spec
705695
}
696+
const finishedTest = {
697+
testName,
698+
testStatus,
699+
finishTime: this.activeTestSpan._getTime(), // we store the finish time here
700+
testSpan: this.activeTestSpan
701+
}
702+
if (this.finishedTestsByFile[testSuite]) {
703+
this.finishedTestsByFile[testSuite].push(finishedTest)
704+
} else {
705+
this.finishedTestsByFile[testSuite] = [finishedTest]
706+
}
707+
// test spans are finished at after:spec
706708
this.ciVisEvent(TELEMETRY_EVENT_FINISHED, 'test', {
707709
hasCodeOwners: !!this.activeTestSpan.context()._tags[TEST_CODE_OWNERS],
708710
isNew,

0 commit comments

Comments
 (0)