Skip to content

fix(jest): report tests from custom environments#9194

Merged
juan-fernandez merged 4 commits into
masterfrom
juan-fernandez/fix-jest-custom-environment-events
Jul 3, 2026
Merged

fix(jest): report tests from custom environments#9194
juan-fernandez merged 4 commits into
masterfrom
juan-fernandez/fix-jest-custom-environment-events

Conversation

@juan-fernandez

@juan-fernandez juan-fernandez commented Jul 2, 2026

Copy link
Copy Markdown
Collaborator

What does this PR do?

Ensures dd-trace still reports Jest test events when a project uses a custom Jest environment that overrides handleTestEvent without calling super.handleTestEvent.

The Jest environment wrapper now detects custom handleTestEvent overrides and runs Datadog's event handler after the custom handler when the event has not already been handled.

Motivation

While validating Test Optimization against webpack/webpack, the runbook reported session, module, and suite events, but zero test events. The project uses a custom Jest environment, and that environment shadows Datadog's Jest circus handleTestEvent hook. As a result, per-test events never reached the intake even though the broader Jest lifecycle was instrumented.

@dd-octo-sts

dd-octo-sts Bot commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

Overall package size

Self size: 6.53 MB
Deduped: 7.59 MB
No deduping: 7.59 MB

Dependency sizes | name | version | self size | total size | |------|---------|-----------|------------| | import-in-the-middle | 3.2.0 | 104.26 kB | 843.44 kB | | opentracing | 0.14.7 | 194.81 kB | 194.81 kB | | dc-polyfill | 0.1.11 | 25.74 kB | 25.74 kB |

🤖 This report was automatically generated by heaviest-objects-in-the-universe

@juan-fernandez

Copy link
Copy Markdown
Collaborator Author

@codex review

@juan-fernandez

Copy link
Copy Markdown
Collaborator Author

/datadog autotest review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 8038fc8927

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread packages/datadog-instrumentations/src/jest.js Outdated
Comment thread packages/datadog-instrumentations/src/jest.js
@datadog-datadog-prod-us1-2

datadog-datadog-prod-us1-2 Bot commented Jul 2, 2026

Copy link
Copy Markdown

Tests

🎉 All green!

🧪 All tests passed
❄️ No new flaky tests detected

🎯 Code Coverage (details)
Patch Coverage: 78.95%
Overall Coverage: 87.86% (-0.00%)

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: c96ddba | Docs | Datadog PR Page | Give us feedback!

@pr-commenter

pr-commenter Bot commented Jul 2, 2026

Copy link
Copy Markdown

Benchmarks

Benchmark execution time: 2026-07-03 06:31:00

Comparing candidate commit c96ddba in PR branch juan-fernandez/fix-jest-custom-environment-events with baseline commit f3c7006 in branch master.

📊 Benchmarking dashboard

Found 0 performance improvements and 0 performance regressions! Performance is the same for 2251 metrics, 35 unstable metrics.

Explanation

This is an A/B test comparing a candidate commit's performance against that of a baseline commit. Performance changes are noted in the tables below as:

  • 🟩 = significantly better candidate vs. baseline
  • 🟥 = significantly worse candidate vs. baseline

We compute a confidence interval (CI) over the relative difference of means between metrics from the candidate and baseline commits, considering the baseline as the reference.

If the CI is entirely outside the configured SIGNIFICANT_IMPACT_THRESHOLD (or the deprecated UNCONFIDENCE_THRESHOLD), the change is considered significant.

Feel free to reach out to #apm-benchmarking-platform on Slack if you have any questions.

More details about the CI and significant changes

You can imagine this CI as a range of values that is likely to contain the true difference of means between the candidate and baseline commits.

CIs of the difference of means are often centered around 0%, because often changes are not that big:

---------------------------------(------|---^--------)-------------------------------->
                              -0.6%    0%  0.3%     +1.2%
                                 |          |        |
         lower bound of the CI --'          |        |
sample mean (center of the CI) -------------'        |
         upper bound of the CI ----------------------'

As described above, a change is considered significant if the CI is entirely outside the configured SIGNIFICANT_IMPACT_THRESHOLD (or the deprecated UNCONFIDENCE_THRESHOLD).

For instance, for an execution time metric, this confidence interval indicates a significantly worse performance:

----------------------------------------|---------|---(---------^---------)---------->
                                       0%        1%  1.3%      2.2%      3.1%
                                                  |   |         |         |
       significant impact threshold --------------'   |         |         |
                      lower bound of CI --------------'         |         |
       sample mean (center of the CI) --------------------------'         |
                      upper bound of CI ----------------------------------'

Unstable benchmarks

These benchmarks have a confidence interval too wide to call a change; treat them as noise rather than signal.

scenario:appsec-appsec-enabled-24

  • unstable execution_time [-178.813ms; +184.974ms] or [-6.786%; +7.020%]

scenario:appsec-appsec-enabled-26

  • unstable execution_time [-199.249ms; +202.023ms] or [-7.934%; +8.045%]

scenario:appsec-control-20

  • unstable execution_time [-134.055ms; +168.860ms] or [-7.745%; +9.756%]

scenario:appsec-control-24

  • unstable execution_time [-105066.290µs; +105837.057µs] or [-8.629%; +8.692%]

scenario:appsec-control-26

  • unstable execution_time [-111532.526µs; +112652.431µs] or [-9.253%; +9.346%]

scenario:debugger-line-probe-with-snapshot-default-24

  • unstable cpu_user_time [-2384.204ms; +2056.801ms] or [-26.193%; +22.597%]
  • unstable execution_time [-2466.144ms; +2172.153ms] or [-25.121%; +22.126%]
  • unstable instructions [-20.1G instructions; +17.6G instructions] or [-26.946%; +23.552%]
  • unstable max_rss_usage [-10308.394KB; +9393.194KB] or [-6.487%; +5.911%]
  • unstable throughput [-575.537op/s; +702.261op/s] or [-17.090%; +20.853%]

scenario:debugger-line-probe-with-snapshot-default-26

  • unstable cpu_user_time [-1838.237ms; +595.784ms] or [-19.689%; +6.381%]
  • unstable execution_time [-1846.930ms; +608.234ms] or [-18.395%; +6.058%]
  • unstable instructions [-16.4G instructions; +5.4G instructions] or [-21.200%; +6.964%]
  • unstable throughput [-139.368op/s; +409.323op/s] or [-4.258%; +12.506%]

scenario:debugger-line-probe-with-snapshot-minimal-24

  • unstable cpu_user_time [-1466.211ms; +503.054ms] or [-17.009%; +5.836%]
  • unstable execution_time [-1491.067ms; +498.372ms] or [-16.033%; +5.359%]
  • unstable instructions [-12.4G instructions; +4.0G instructions] or [-17.658%; +5.704%]
  • unstable throughput [-164.343op/s; +447.144op/s] or [-4.683%; +12.740%]

scenario:debugger-line-probe-with-snapshot-minimal-26

  • unstable cpu_user_time [-1930.675ms; +567.420ms] or [-20.716%; +6.088%]
  • unstable execution_time [-1952.511ms; +579.747ms] or [-19.512%; +5.793%]
  • unstable instructions [-17.3G instructions; +4.9G instructions] or [-22.333%; +6.324%]
  • unstable throughput [-121.965op/s; +438.004op/s] or [-3.709%; +13.320%]

scenario:debugger-line-probe-without-snapshot-24

  • unstable cpu_user_time [-1674.212ms; +600.902ms] or [-21.012%; +7.542%]
  • unstable execution_time [-1686.234ms; +642.142ms] or [-19.476%; +7.417%]
  • unstable instructions [-14.5G instructions; +5.1G instructions] or [-22.295%; +7.866%]
  • unstable throughput [-204.067op/s; +473.589op/s] or [-5.344%; +12.403%]

scenario:debugger-line-probe-without-snapshot-26

  • unstable cpu_user_time [-1815.417ms; +668.339ms] or [-19.415%; +7.147%]
  • unstable execution_time [-1863.783ms; +697.995ms] or [-18.562%; +6.951%]
  • unstable instructions [-16.0G instructions; +5.6G instructions] or [-20.640%; +7.248%]
  • unstable throughput [-165.318op/s; +393.598op/s] or [-5.044%; +12.009%]

scenario:dogstatsd-with-tags-20

  • unstable cpu_user_time [-486.369ms; +171.583ms] or [-9.967%; +3.516%]
  • unstable execution_time [-487.659ms; +169.010ms] or [-9.839%; +3.410%]
  • unstable throughput [-57738.138op/s; +167195.033op/s] or [-3.410%; +9.875%]

scenario:plugin-graphql-long-with-depth-and-collapse-off-20

  • unstable max_rss_usage [-35.218MB; +48.414MB] or [-8.994%; +12.364%]

scenario:test-optimization-large-suite-20

  • unstable max_rss_usage [-4355.296KB; +3968.296KB] or [-5.554%; +5.061%]

@datadog-datadog-prod-us1-2 datadog-datadog-prod-us1-2 Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Datadog Autotest: PASS

More details

The wrapper logic in wrapCustomHandleTestEvent is correct across all adversarial scenarios: normal environments (no wrapper installed), custom envs without super (DD fires after custom), custom envs with super (DD fires exactly once via dedup), sync/async mismatches (returns thenable preserving custom return value), sync throws and async rejections (propagate cleanly without triggering DD), and multi-level inheritance (DD always fires after the outermost override). The module-scope handledJestEvents WeakSet is safe because Jest creates fresh event objects per dispatch and workers are isolated processes.

Was this helpful? React 👍 or 👎

📊 Validated against 12 scenarios · Open Bits AI session

🤖 Datadog Autotest · Commit 8038fc8 · What is Autotest? · Any feedback? Reach out in #autotest

@codecov

codecov Bot commented Jul 2, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 94.73684% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 93.64%. Comparing base (f3c7006) to head (c96ddba).
⚠️ Report is 8 commits behind head on master.

Files with missing lines Patch % Lines
packages/datadog-instrumentations/src/jest.js 94.73% 3 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##           master    #9194   +/-   ##
=======================================
  Coverage   93.64%   93.64%           
=======================================
  Files         898      898           
  Lines       52365    52421   +56     
  Branches    12324    12340   +16     
=======================================
+ Hits        49037    49091   +54     
- Misses       3328     3330    +2     
Flag Coverage Δ
aiguard 34.88% <ø> (-0.08%) ⬇️
aiguard-integration 41.96% <ø> (ø)
apm-bucket-0 34.84% <ø> (-0.08%) ⬇️
apm-bucket-1 40.51% <ø> (-0.07%) ⬇️
apm-bucket-2 37.50% <ø> (-0.07%) ⬇️
apm-capabilities-tracing 48.89% <ø> (+<0.01%) ⬆️
apm-integrations-aerospike 33.13% <ø> (-0.08%) ⬇️
apm-integrations-confluentinc-kafka-javascript 40.03% <ø> (-0.08%) ⬇️
apm-integrations-couchbase 33.41% <ø> (+0.08%) ⬆️
apm-integrations-http 42.05% <ø> (-0.07%) ⬇️
apm-integrations-kafkajs 40.27% <ø> (-0.08%) ⬇️
apm-integrations-next 29.59% <ø> (-0.11%) ⬇️
apm-integrations-prisma 35.18% <ø> (-0.07%) ⬇️
apm-integrations-tedious 34.05% <ø> (-0.08%) ⬇️
appsec 57.83% <ø> (-0.15%) ⬇️
appsec-express_fastify_graphql 53.83% <ø> (-0.07%) ⬇️
appsec-integration 35.63% <5.26%> (-0.06%) ⬇️
appsec-kafka_ldapjs_lodash 43.76% <ø> (-0.07%) ⬇️
appsec-mongodb-core_mongoose_mysql 48.91% <ø> (-0.06%) ⬇️
appsec-next 28.04% <5.26%> (-0.15%) ⬇️
appsec-node-serialize_passport_postgres 48.08% <ø> (-0.06%) ⬇️
appsec-sourcing_stripe_template 45.64% <ø> (-0.06%) ⬇️
debugger 44.64% <ø> (ø)
instrumentations-bucket-0 28.17% <ø> (-0.08%) ⬇️
instrumentations-bucket-1 37.57% <ø> (-0.07%) ⬇️
instrumentations-bucket-10 40.52% <ø> (-0.07%) ⬇️
instrumentations-bucket-11 27.96% <ø> (-0.08%) ⬇️
instrumentations-bucket-12 28.79% <ø> (-0.08%) ⬇️
instrumentations-bucket-13 27.79% <ø> (-0.08%) ⬇️
instrumentations-bucket-2 30.36% <ø> (-0.08%) ⬇️
instrumentations-bucket-3 36.06% <ø> (-0.08%) ⬇️
instrumentations-bucket-4 28.58% <ø> (-0.08%) ⬇️
instrumentations-bucket-5 36.42% <ø> (-0.07%) ⬇️
instrumentations-bucket-6 38.43% <ø> (-0.08%) ⬇️
instrumentations-bucket-7 36.16% <ø> (-0.07%) ⬇️
instrumentations-bucket-8 37.11% <ø> (-0.07%) ⬇️
instrumentations-bucket-9 39.62% <ø> (-0.07%) ⬇️
instrumentations-instrumentation-couchbase 46.48% <ø> (ø)
instrumentations-integration-esbuild 24.47% <5.26%> (-0.04%) ⬇️
llmobs-ai_anthropic_bedrock 39.68% <ø> (-0.07%) ⬇️
llmobs-google-genai_langchain_vertex-ai 37.10% <ø> (-0.06%) ⬇️
llmobs-openai 39.74% <ø> (-0.07%) ⬇️
llmobs-sdk 43.59% <ø> (-0.08%) ⬇️
master-coverage 93.64% <94.73%> (?)
openfeature 37.94% <ø> (+<0.01%) ⬆️
openfeature-unit 50.47% <ø> (ø)
platform-core_esbuild_instrumentations-misc 22.93% <5.26%> (-0.04%) ⬇️
platform-integration 47.59% <ø> (+0.01%) ⬆️
platform-shimmer_unit-guardrails_webpack 18.44% <5.26%> (-0.04%) ⬇️
plugins-bucket-0 36.52% <ø> (-0.07%) ⬇️
plugins-bucket-1 39.78% <ø> (ø)
plugins-bucket-11 38.63% <ø> (-0.08%) ⬇️
plugins-bucket-17 39.22% <ø> (-0.07%) ⬇️
plugins-bucket-18 42.18% <ø> (-0.07%) ⬇️
plugins-bucket-19 39.97% <ø> (+0.17%) ⬆️
plugins-bucket-20 43.38% <ø> (-0.13%) ⬇️
plugins-bucket-4 37.88% <ø> (-0.07%) ⬇️
plugins-bullmq_cassandra_cookie 39.93% <ø> (-0.07%) ⬇️
plugins-cookie-parser_crypto_dd-trace-api 33.23% <ø> (-0.08%) ⬇️
plugins-fetch_fs_generic-pool 36.21% <ø> (-0.08%) ⬇️
plugins-google-cloud-pubsub_grpc_handlebars 43.09% <ø> (-0.07%) ⬇️
plugins-hapi_hono_ioredis 37.95% <ø> (-0.07%) ⬇️
plugins-jest_knex_langgraph 32.61% <ø> (-0.08%) ⬇️
plugins-ldapjs_light-my-request_limitd-client 27.92% <ø> (-0.02%) ⬇️
plugins-lodash_mariadb_memcached 35.18% <ø> (-0.08%) ⬇️
plugins-mongodb_mongodb-core_mongoose 36.46% <ø> (-0.07%) ⬇️
plugins-multer_mysql_mysql2 35.15% <ø> (-0.08%) ⬇️
plugins-nats_node-serialize_opensearch 37.31% <ø> (-0.08%) ⬇️
plugins-passport-http_pino_postgres 35.53% <ø> (-0.08%) ⬇️
plugins-process_pug_redis 34.27% <ø> (-0.08%) ⬇️
plugins-undici_url_valkey 36.03% <ø> (-0.08%) ⬇️
plugins-vm_winston_ws 37.72% <ø> (-0.08%) ⬇️
profiling 43.81% <ø> (-0.07%) ⬇️
serverless-aws-sdk-aws-sdk 33.32% <ø> (-0.07%) ⬇️
serverless-aws-sdk-bedrockruntime 32.19% <ø> (-0.07%) ⬇️
serverless-aws-sdk-client 37.16% <ø> (ø)
serverless-aws-sdk-dynamodb 34.19% <ø> (-0.07%) ⬇️
serverless-aws-sdk-eventbridge 27.36% <ø> (-0.07%) ⬇️
serverless-aws-sdk-kinesis 37.55% <ø> (-0.07%) ⬇️
serverless-aws-sdk-lambda 34.64% <ø> (-0.07%) ⬇️
serverless-aws-sdk-s3 32.63% <ø> (-0.07%) ⬇️
serverless-aws-sdk-serverless-peer-service 39.65% <ø> (-0.08%) ⬇️
serverless-aws-sdk-sns 38.52% <ø> (+0.05%) ⬆️
serverless-aws-sdk-sqs 38.14% <ø> (-0.07%) ⬇️
serverless-aws-sdk-stepfunctions 33.22% <ø> (-0.07%) ⬇️
serverless-aws-sdk-util 47.95% <ø> (ø)
serverless-bucket-0 39.64% <ø> (ø)
serverless-lambda 34.34% <ø> (-0.09%) ⬇️
test-optimization-cucumber 52.54% <ø> (+0.11%) ⬆️
test-optimization-cypress 49.63% <ø> (+0.06%) ⬆️
test-optimization-jest 55.73% <94.73%> (+0.19%) ⬆️
test-optimization-mocha 53.61% <ø> (+0.08%) ⬆️
test-optimization-playwright-playwright-atr 43.53% <ø> (+0.08%) ⬆️
test-optimization-playwright-playwright-efd 43.81% <ø> (+0.08%) ⬆️
test-optimization-playwright-playwright-final-status 43.94% <ø> (+0.11%) ⬆️
test-optimization-playwright-playwright-impacted-tests 43.36% <ø> (ø)
test-optimization-playwright-playwright-reporting 43.47% <ø> (+0.08%) ⬆️
test-optimization-playwright-playwright-test-management 45.15% <ø> (+0.30%) ⬆️
test-optimization-playwright-playwright-test-span 44.71% <ø> (+0.06%) ⬆️
test-optimization-selenium 45.49% <63.15%> (+0.11%) ⬆️
test-optimization-testopt 46.86% <63.15%> (+0.12%) ⬆️
test-optimization-vitest 52.71% <ø> (+0.09%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@juan-fernandez

Copy link
Copy Markdown
Collaborator Author

@codex review

@juan-fernandez

Copy link
Copy Markdown
Collaborator Author

/datadog autotest review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Another round soon, please!

Reviewed commit: c6a2b5cf16

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@juan-fernandez
juan-fernandez marked this pull request as ready for review July 2, 2026 21:46
@juan-fernandez
juan-fernandez requested a review from a team as a code owner July 2, 2026 21:46

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: c6a2b5cf16

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread packages/datadog-instrumentations/src/jest.js

@datadog-datadog-prod-us1-2 datadog-datadog-prod-us1-2 Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Datadog Autotest: PASS

More details

The wrapper correctly handles all three custom-environment patterns (prototype override without super, instance field, async add_test) — 9 adversarial scenarios executed locally all pass. The WeakSet deduplication prevents double-firing when a custom env does call super, and the setup() re-check correctly defers instance-field wrapping until after the subclass constructor completes.

Was this helpful? React 👍 or 👎

📊 Validated against 9 scenarios · Open Bits AI session

🤖 Datadog Autotest · Commit c6a2b5c · What is Autotest? · Any feedback? Reach out in #autotest

@juan-fernandez

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 3c616effad

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread packages/datadog-instrumentations/src/jest.js
@juan-fernandez

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🎉

Reviewed commit: c96ddba079

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@juan-fernandez
juan-fernandez merged commit 0e90a6d into master Jul 3, 2026
688 of 690 checks passed
@juan-fernandez
juan-fernandez deleted the juan-fernandez/fix-jest-custom-environment-events branch July 3, 2026 09:11
@dd-octo-sts dd-octo-sts Bot mentioned this pull request Jul 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants