Skip to content

Commit

Permalink
fix jest tests
Browse files Browse the repository at this point in the history
  • Loading branch information
juan-fernandez committed Jan 9, 2025
1 parent 0f248df commit 05f8598
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 35 deletions.
50 changes: 24 additions & 26 deletions integration-tests/jest/jest.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@ const {
TEST_SESSION_NAME,
TEST_LEVEL_EVENT_TYPES,
DI_ERROR_DEBUG_INFO_CAPTURED,
DI_DEBUG_ERROR_FILE,
DI_DEBUG_ERROR_SNAPSHOT_ID,
DI_DEBUG_ERROR_LINE
DI_DEBUG_ERROR_PREFIX,
DI_DEBUG_ERROR_FILE_SUFFIX,
DI_DEBUG_ERROR_SNAPSHOT_ID_SUFFIX,
DI_DEBUG_ERROR_LINE_SUFFIX
} = require('../../packages/dd-trace/src/plugins/util/test')
const { DD_HOST_CPU_COUNT } = require('../../packages/dd-trace/src/plugins/util/env')
const { ERROR_MESSAGE } = require('../../packages/dd-trace/src/constants')
Expand Down Expand Up @@ -2426,11 +2427,12 @@ describe('jest CommonJS', () => {
assert.equal(retriedTests.length, 1)
const [retriedTest] = retriedTests

assert.notProperty(retriedTest.meta, DI_ERROR_DEBUG_INFO_CAPTURED)
assert.notProperty(retriedTest.meta, DI_DEBUG_ERROR_FILE)
assert.notProperty(retriedTest.metrics, DI_DEBUG_ERROR_LINE)
assert.notProperty(retriedTest.meta, DI_DEBUG_ERROR_SNAPSHOT_ID)
const hasDebugTags = Object.keys(retriedTest.meta)
.some(property => property.startsWith(DI_DEBUG_ERROR_PREFIX) || property === DI_ERROR_DEBUG_INFO_CAPTURED)

assert.isFalse(hasDebugTags)
})

const logsPromise = receiver
.gatherPayloadsMaxTimeout(({ url }) => url.endsWith('/api/v2/logs'), (payloads) => {
if (payloads.length > 0) {
Expand Down Expand Up @@ -2472,10 +2474,10 @@ describe('jest CommonJS', () => {
assert.equal(retriedTests.length, 1)
const [retriedTest] = retriedTests

assert.notProperty(retriedTest.meta, DI_ERROR_DEBUG_INFO_CAPTURED)
assert.notProperty(retriedTest.meta, DI_DEBUG_ERROR_FILE)
assert.notProperty(retriedTest.metrics, DI_DEBUG_ERROR_LINE)
assert.notProperty(retriedTest.meta, DI_DEBUG_ERROR_SNAPSHOT_ID)
const hasDebugTags = Object.keys(retriedTest.meta)
.some(property => property.startsWith(DI_DEBUG_ERROR_PREFIX) || property === DI_ERROR_DEBUG_INFO_CAPTURED)

assert.isFalse(hasDebugTags)
})
const logsPromise = receiver
.gatherPayloadsMaxTimeout(({ url }) => url.endsWith('/api/v2/logs'), (payloads) => {
Expand Down Expand Up @@ -2522,15 +2524,15 @@ describe('jest CommonJS', () => {
const [retriedTest] = retriedTests

assert.propertyVal(retriedTest.meta, DI_ERROR_DEBUG_INFO_CAPTURED, 'true')
assert.propertyVal(
retriedTest.meta,
DI_DEBUG_ERROR_FILE,
'ci-visibility/dynamic-instrumentation/dependency.js'

assert.isTrue(
retriedTest.meta[`${DI_DEBUG_ERROR_PREFIX}.0.${DI_DEBUG_ERROR_FILE_SUFFIX}`]
.endsWith('ci-visibility/dynamic-instrumentation/dependency.js')
)
assert.equal(retriedTest.metrics[DI_DEBUG_ERROR_LINE], 4)
assert.exists(retriedTest.meta[DI_DEBUG_ERROR_SNAPSHOT_ID])
assert.equal(retriedTest.metrics[`${DI_DEBUG_ERROR_PREFIX}.0.${DI_DEBUG_ERROR_LINE_SUFFIX}`], 4)
assert.exists(retriedTest.meta[`${DI_DEBUG_ERROR_PREFIX}.0.${DI_DEBUG_ERROR_SNAPSHOT_ID_SUFFIX}`])

snapshotIdByTest = retriedTest.meta[DI_DEBUG_ERROR_SNAPSHOT_ID]
snapshotIdByTest = retriedTest.meta[`${DI_DEBUG_ERROR_PREFIX}.0.${DI_DEBUG_ERROR_SNAPSHOT_ID_SUFFIX}`]
spanIdByTest = retriedTest.span_id.toString()
traceIdByTest = retriedTest.trace_id.toString()

Expand Down Expand Up @@ -2603,14 +2605,10 @@ describe('jest CommonJS', () => {
assert.equal(retriedTests.length, 1)
const [retriedTest] = retriedTests

assert.propertyVal(retriedTest.meta, DI_ERROR_DEBUG_INFO_CAPTURED, 'true')
assert.propertyVal(
retriedTest.meta,
DI_DEBUG_ERROR_FILE,
'ci-visibility/dynamic-instrumentation/dependency.js'
)
assert.equal(retriedTest.metrics[DI_DEBUG_ERROR_LINE], 4)
assert.exists(retriedTest.meta[DI_DEBUG_ERROR_SNAPSHOT_ID])
const hasDebugTags = Object.keys(retriedTest.meta)
.some(property => property.startsWith(DI_DEBUG_ERROR_PREFIX) || property === DI_ERROR_DEBUG_INFO_CAPTURED)

assert.isFalse(hasDebugTags)
})
const logsPromise = receiver
.gatherPayloadsMaxTimeout(({ url }) => url.endsWith('/api/v2/logs'), (payloads) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/datadog-plugin-jest/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ class JestPlugin extends CiPlugin {
)
this.activeTestSpan.setTag(
`${DI_DEBUG_ERROR_PREFIX}.${stackIndex}.${DI_DEBUG_ERROR_LINE_SUFFIX}`,
snapshot.probe.location.lines[0]
Number(snapshot.probe.location.lines[0])
)

const activeTestSpanContext = this.activeTestSpan.context()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,13 @@ async function processScriptWithInlineSourceMap (params) {
let generatedPosition

// Map to the generated position. We'll attempt with the full file path first, then with the basename.
try {
generatedPosition = consumer.generatedPositionFor({
source: file,
line,
column: 0
})
} catch (e) {
// TODO: figure out why sometimes the full path doesn't work
generatedPosition = consumer.generatedPositionFor({
source: file,
line,
column: 0
})
if (generatedPosition.line === null) {
generatedPosition = consumer.generatedPositionFor({
source: path.basename(file),
line,
Expand All @@ -156,5 +156,11 @@ async function processScriptWithInlineSourceMap (params) {

consumer.destroy()

// If we can't find the line, just return the original line
if (generatedPosition.line === null) {
log.error(`Could not find generated position for ${file}:${line}`)
return line
}

return generatedPosition.line
}
2 changes: 1 addition & 1 deletion packages/dd-trace/src/plugins/ci_plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ module.exports = class CiPlugin extends Plugin {
addDiProbe (err, onHitBreakpoint) {
const [file, line, stackIndex] = getFileAndLineNumberFromError(err, this.repositoryRoot)

if (!file || !line || !stackIndex) {
if (!file || !Number.isInteger(line)) {
log.warn('Could not add breakpoint for dynamic instrumentation')
return
}
Expand Down

0 comments on commit 05f8598

Please sign in to comment.