Skip to content

Commit

Permalink
[ci-visibility] Fix jest bug when parent id is null (#2848)
Browse files Browse the repository at this point in the history
  • Loading branch information
juan-fernandez authored Mar 1, 2023
1 parent 763009e commit a1211b2
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 1 deletion.
23 changes: 23 additions & 0 deletions integration-tests/ci-visibility.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,29 @@ testFrameworks.forEach(({
await receiver.stop()
})

if (name === 'jest') {
it('does not crash when jest is badly initialized', (done) => {
childProcess = fork('ci-visibility/run-jest-bad-init.js', {
cwd,
env: {
DD_TRACE_AGENT_PORT: receiver.port
},
stdio: 'pipe'
})
childProcess.stdout.on('data', (chunk) => {
testOutput += chunk.toString()
})
childProcess.stderr.on('data', (chunk) => {
testOutput += chunk.toString()
})
childProcess.on('message', () => {
assert.notInclude(testOutput, 'TypeError')
assert.include(testOutput, expectedStdout)
done()
})
})
}

it('can run tests and report spans', (done) => {
receiver.setInfoResponse({ endpoints: [] })
receiver.payloadReceived(({ url }) => url === '/v0.4/traces').then(({ payload }) => {
Expand Down
6 changes: 6 additions & 0 deletions integration-tests/ci-visibility/jestEnvironmentBadInit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// eslint-disable-next-line
require('dd-trace').init({
service: 'dd-trace-bad-init'
})

module.exports = require('jest-environment-node')
20 changes: 20 additions & 0 deletions integration-tests/ci-visibility/run-jest-bad-init.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const jest = require('jest')

const options = {
projects: [__dirname],
testPathIgnorePatterns: ['/node_modules/'],
cache: false,
maxWorkers: '50%',
testRegex: /test\/ci-visibility-test/,
runInBand: true,
testEnvironment: '<rootDir>/ci-visibility/jestEnvironmentBadInit.js'
}

jest.runCLI(
options,
options.projects
).then(() => {
if (process.send) {
process.send('finished')
}
})
5 changes: 4 additions & 1 deletion packages/dd-trace/src/plugins/ci_plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,13 @@ module.exports = class CiPlugin extends Plugin {
const suiteTags = {
[TEST_SUITE_ID]: testSuiteSpan.context().toSpanId(),
[TEST_SESSION_ID]: testSuiteSpan.context().toTraceId(),
[TEST_MODULE_ID]: testSuiteSpan.context()._parentId.toString(10),
[TEST_COMMAND]: testSuiteSpan.context()._tags[TEST_COMMAND],
[TEST_BUNDLE]: testSuiteSpan.context()._tags[TEST_COMMAND]
}
if (testSuiteSpan.context()._parentId) {
suiteTags[TEST_MODULE_ID] = testSuiteSpan.context()._parentId.toString(10)
}

testTags = {
...testTags,
...suiteTags
Expand Down

0 comments on commit a1211b2

Please sign in to comment.