diff --git a/integration-tests/ci-visibility.spec.js b/integration-tests/ci-visibility.spec.js index 8b716d560e3..60ddd12dbd0 100644 --- a/integration-tests/ci-visibility.spec.js +++ b/integration-tests/ci-visibility.spec.js @@ -230,6 +230,28 @@ testFrameworks.forEach(({ }).catch(done) }) }) + it('reports timeout error message', (done) => { + childProcess = fork('ci-visibility/run-jest.js', { + cwd, + env: { + ...getCiVisAgentlessConfig(receiver.port), + NODE_OPTIONS: '-r dd-trace/ci/init', + RUN_IN_PARALLEL: true, + TEST_REGEX: 'timeout-test/timeout-test.js' + }, + stdio: 'pipe' + }) + childProcess.stdout.on('data', (chunk) => { + testOutput += chunk.toString() + }) + childProcess.stderr.on('data', (chunk) => { + testOutput += chunk.toString() + }) + childProcess.on('message', () => { + assert.include(testOutput, 'Exceeded timeout of 100 ms for a test while waiting for `done()` to be called.') + done() + }) + }) } it('can run tests and report spans', (done) => { diff --git a/integration-tests/ci-visibility/run-jest.js b/integration-tests/ci-visibility/run-jest.js index 21090204b2e..b8710650029 100644 --- a/integration-tests/ci-visibility/run-jest.js +++ b/integration-tests/ci-visibility/run-jest.js @@ -4,7 +4,7 @@ const options = { projects: [__dirname], testPathIgnorePatterns: ['/node_modules/'], cache: false, - testRegex: /test\/ci-visibility-test/, + testRegex: process.env.TEST_REGEX ? new RegExp(process.env.TEST_REGEX) : /test\/ci-visibility-test/, coverage: true, runInBand: true } diff --git a/integration-tests/ci-visibility/timeout-test/timeout-test.js b/integration-tests/ci-visibility/timeout-test/timeout-test.js new file mode 100644 index 00000000000..5a1691a8728 --- /dev/null +++ b/integration-tests/ci-visibility/timeout-test/timeout-test.js @@ -0,0 +1,9 @@ +/* eslint-disable */ +jest.setTimeout(100) +describe('ci visibility', () => { + it('will timeout', (done) => { + setTimeout(() => { + done() + }, 200) + }) +}) diff --git a/packages/datadog-instrumentations/src/jest.js b/packages/datadog-instrumentations/src/jest.js index e76240a2dad..00eeea36908 100644 --- a/packages/datadog-instrumentations/src/jest.js +++ b/packages/datadog-instrumentations/src/jest.js @@ -129,8 +129,7 @@ function getWrappedEnvironment (BaseEnvironment, jestVersion) { suite: this.testSuite, runner: 'jest-circus', testParameters, - frameworkVersion: jestVersion, - testStartLine: getTestLineStart(event.test.asyncError, this.testSuite) + frameworkVersion: jestVersion }) originalTestFns.set(event.test, event.test.fn) event.test.fn = asyncResource.bind(event.test.fn) @@ -145,7 +144,10 @@ function getWrappedEnvironment (BaseEnvironment, jestVersion) { const formattedError = formatJestError(event.test.errors[0]) testErrCh.publish(formattedError) } - testRunFinishCh.publish(status) + testRunFinishCh.publish({ + status, + testStartLine: getTestLineStart(event.test.asyncError, this.testSuite) + }) // restore in case it is retried event.test.fn = originalTestFns.get(event.test) }) @@ -471,7 +473,7 @@ function jasmineAsyncInstallWraper (jasmineAsyncInstallExport, jestVersion) { const formattedError = formatJestError(spec.result.failedExpectations[0].error) testErrCh.publish(formattedError) } - testRunFinishCh.publish(specStatusToTestStatus[spec.result.status]) + testRunFinishCh.publish({ status: specStatusToTestStatus[spec.result.status] }) onComplete.apply(this, arguments) }) arguments[0] = callback diff --git a/packages/datadog-plugin-jest/src/index.js b/packages/datadog-plugin-jest/src/index.js index b22e2862b23..624d640d513 100644 --- a/packages/datadog-plugin-jest/src/index.js +++ b/packages/datadog-plugin-jest/src/index.js @@ -166,9 +166,12 @@ class JestPlugin extends CiPlugin { this.enter(span, store) }) - this.addSub('ci:jest:test:finish', (status) => { + this.addSub('ci:jest:test:finish', ({ status, testStartLine }) => { const span = storage.getStore().span span.setTag(TEST_STATUS, status) + if (testStartLine) { + span.setTag(TEST_SOURCE_START, testStartLine) + } span.finish() finishAllTraceSpans(span) }) @@ -197,8 +200,10 @@ class JestPlugin extends CiPlugin { const extraTags = { [JEST_TEST_RUNNER]: runner, [TEST_PARAMETERS]: testParameters, - [TEST_FRAMEWORK_VERSION]: frameworkVersion, - [TEST_SOURCE_START]: testStartLine + [TEST_FRAMEWORK_VERSION]: frameworkVersion + } + if (testStartLine) { + extraTags[TEST_SOURCE_START] = testStartLine } return super.startTestSpan(name, suite, this.testSuiteSpan, extraTags)