Skip to content

[test optimization] Fix EFD retries in jest#7637

Merged
juan-fernandez merged 4 commits intomasterfrom
juan-fernandez/fix-retries-efd-jest
Mar 3, 2026
Merged

[test optimization] Fix EFD retries in jest#7637
juan-fernandez merged 4 commits intomasterfrom
juan-fernandez/fix-retries-efd-jest

Conversation

@juan-fernandez
Copy link
Copy Markdown
Collaborator

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

What does this PR do?

Implements duration-based retry count selection for Early Flake Detection (EFD) in Jest. Instead of
retrying every new test a fixed number of times, the retry count is now chosen from the
slow_test_retries bucket map returned by the backend (5s, 10s, 30s, 5m), based on how
long the test's first execution took. Tests that exceed 5 minutes are tagged as slow and not
retried at all.

Motivation

The backend already returns a slow_test_retries map as part of the library configuration, but
it was being ignored — Jest always used a single fixed retry count. This PR wires up the correct
per-duration retry counts.

Additional Notes

Because the duration is only known after the first execution, retry scheduling was moved from the
add_test event (collection phase) to test_done (execution phase). This required a workaround
for jest-circus's internal guards:

  • jest-circus ≤24: during execution state.currentDescribeBlock points to the ROOT block,
    whose tests loop has already finished. Retries added there would never run. Fix: temporarily set
    state.currentDescribeBlock = event.test.parent so retries land in the still-iterating children
    array.
  • jest-circus ≥27: state.hasStarted = true causes test() to throw "Cannot add a test after tests have started running". Fix: temporarily set state.hasStarted = false while
    scheduling retries.

Both state fields are restored immediately after retryTest() returns. All [email protected] and
jest@latest integration tests pass.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Mar 2, 2026

Overall package size

Self size: 4.87 MB
Deduped: 5.71 MB
No deduping: 5.71 MB

Dependency sizes | name | version | self size | total size | |------|---------|-----------|------------| | import-in-the-middle | 2.0.6 | 81.92 kB | 816.75 kB | | dc-polyfill | 0.1.10 | 26.73 kB | 26.73 kB |

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

@codecov
Copy link
Copy Markdown

codecov bot commented Mar 2, 2026

Codecov Report

❌ Patch coverage is 20.00000% with 8 lines in your changes missing coverage. Please review.
✅ Project coverage is 80.29%. Comparing base (d7bee55) to head (27dbad3).
⚠️ Report is 4 commits behind head on master.

Files with missing lines Patch % Lines
packages/dd-trace/src/plugins/util/test.js 0.00% 5 Missing ⚠️
packages/datadog-plugin-jest/src/index.js 0.00% 3 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #7637      +/-   ##
==========================================
+ Coverage   80.27%   80.29%   +0.02%     
==========================================
  Files         734      738       +4     
  Lines       31644    31897     +253     
==========================================
+ Hits        25401    25611     +210     
- Misses       6243     6286      +43     
Flag Coverage Δ
aiguard-macos 38.93% <ø> (-0.12%) ⬇️
aiguard-ubuntu 39.04% <ø> (-0.12%) ⬇️
aiguard-windows 38.78% <ø> (-0.11%) ⬇️
apm-capabilities-tracing-macos 48.83% <11.11%> (+0.32%) ⬆️
apm-capabilities-tracing-ubuntu 48.87% <11.11%> (+0.33%) ⬆️
apm-capabilities-tracing-windows 48.61% <11.11%> (+0.37%) ⬆️
apm-integrations-child-process 38.51% <ø> (-0.11%) ⬇️
apm-integrations-couchbase-18 37.44% <ø> (-0.11%) ⬇️
apm-integrations-couchbase-eol 37.91% <ø> (-0.11%) ⬇️
apm-integrations-oracledb 37.74% <ø> (-0.11%) ⬇️
appsec-express 55.36% <ø> (-0.08%) ⬇️
appsec-fastify 51.67% <ø> (-0.09%) ⬇️
appsec-graphql 51.86% <ø> (-0.09%) ⬇️
appsec-kafka 44.34% <ø> (-0.10%) ⬇️
appsec-ldapjs 44.03% <ø> (-0.10%) ⬇️
appsec-lodash 43.68% <ø> (-0.09%) ⬇️
appsec-macos 58.35% <16.66%> (-0.10%) ⬇️
appsec-mongodb-core 48.85% <ø> (-0.09%) ⬇️
appsec-mongoose 49.53% <ø> (-0.09%) ⬇️
appsec-mysql 50.89% <ø> (-0.09%) ⬇️
appsec-node-serialize 43.20% <ø> (-0.10%) ⬇️
appsec-passport 47.65% <ø> (-0.10%) ⬇️
appsec-postgres 50.62% <ø> (-0.11%) ⬇️
appsec-sourcing 42.60% <ø> (-0.10%) ⬇️
appsec-template 43.37% <ø> (-0.10%) ⬇️
appsec-ubuntu 58.43% <16.66%> (-0.10%) ⬇️
appsec-windows 58.21% <16.66%> (-0.10%) ⬇️
instrumentations-instrumentation-bluebird 32.31% <ø> (-0.11%) ⬇️
instrumentations-instrumentation-body-parser 40.48% <ø> (-0.11%) ⬇️
instrumentations-instrumentation-child_process 37.83% <ø> (-0.11%) ⬇️
instrumentations-instrumentation-cookie-parser 34.31% <ø> (-0.10%) ⬇️
instrumentations-instrumentation-express 34.64% <ø> (-0.10%) ⬇️
instrumentations-instrumentation-express-mongo-sanitize 34.44% <ø> (-0.10%) ⬇️
instrumentations-instrumentation-express-session 40.11% <ø> (-0.11%) ⬇️
instrumentations-instrumentation-fs 31.92% <ø> (-0.11%) ⬇️
instrumentations-instrumentation-generic-pool 29.73% <16.66%> (-0.02%) ⬇️
instrumentations-instrumentation-http 39.84% <ø> (-0.11%) ⬇️
instrumentations-instrumentation-knex 32.31% <ø> (-0.11%) ⬇️
instrumentations-instrumentation-mongoose 33.45% <ø> (-0.10%) ⬇️
instrumentations-instrumentation-multer 40.23% <ø> (-0.11%) ⬇️
instrumentations-instrumentation-mysql2 38.30% <ø> (-0.11%) ⬇️
instrumentations-instrumentation-passport 44.02% <ø> (-0.10%) ⬇️
instrumentations-instrumentation-passport-http 43.69% <ø> (-0.10%) ⬇️
instrumentations-instrumentation-passport-local 44.23% <ø> (-0.10%) ⬇️
instrumentations-instrumentation-pg 37.73% <ø> (-0.11%) ⬇️
instrumentations-instrumentation-promise 32.24% <ø> (-0.11%) ⬇️
instrumentations-instrumentation-promise-js 32.24% <ø> (-0.11%) ⬇️
instrumentations-instrumentation-q 32.29% <ø> (-0.11%) ⬇️
instrumentations-instrumentation-url 32.21% <ø> (-0.11%) ⬇️
instrumentations-instrumentation-when 32.26% <ø> (-0.11%) ⬇️
llmobs-ai 41.96% <ø> (-0.11%) ⬇️
llmobs-anthropic 40.30% <ø> (-0.11%) ⬇️
llmobs-bedrock 39.25% <ø> (-0.09%) ⬇️
llmobs-google-genai 39.83% <ø> (-0.10%) ⬇️
llmobs-langchain 39.76% <ø> (-0.09%) ⬇️
llmobs-openai 44.14% <ø> (-0.10%) ⬇️
llmobs-vertex-ai 40.02% <ø> (-0.17%) ⬇️
platform-core 31.53% <ø> (ø)
platform-esbuild 34.48% <ø> (ø)
platform-instrumentations-misc 43.94% <ø> (ø)
platform-shimmer 37.63% <ø> (ø)
platform-unit-guardrails 32.95% <ø> (ø)
plugins-azure-event-hubs 25.71% <ø> (ø)
plugins-azure-service-bus 25.07% <ø> (ø)
plugins-bullmq 43.65% <ø> (-0.11%) ⬇️
plugins-cassandra 37.78% <ø> (-0.11%) ⬇️
plugins-cookie 26.77% <ø> (ø)
plugins-cookie-parser 26.56% <ø> (ø)
plugins-crypto 26.79% <ø> (ø)
plugins-dd-trace-api 38.37% <ø> (-0.12%) ⬇️
plugins-express-mongo-sanitize 26.70% <ø> (ø)
plugins-express-session 26.51% <ø> (ø)
plugins-fastify 42.23% <ø> (-0.11%) ⬇️
plugins-fetch 38.33% <ø> (-0.11%) ⬇️
plugins-fs 38.61% <ø> (-0.12%) ⬇️
plugins-generic-pool 25.75% <ø> (ø)
plugins-google-cloud-pubsub 45.37% <ø> (-0.10%) ⬇️
plugins-grpc 40.93% <ø> (-0.11%) ⬇️
plugins-handlebars 26.75% <ø> (ø)
plugins-hapi 40.12% <ø> (-0.11%) ⬇️
plugins-hono 40.38% <ø> (-0.11%) ⬇️
plugins-ioredis 38.42% <ø> (-0.11%) ⬇️
plugins-knex 26.39% <ø> (ø)
plugins-ldapjs 24.24% <ø> (ø)
plugins-light-my-request 26.12% <ø> (ø)
plugins-limitd-client 32.45% <ø> (-0.26%) ⬇️
plugins-lodash 25.84% <ø> (ø)
plugins-mariadb 39.48% <ø> (-0.11%) ⬇️
plugins-memcached 38.17% <ø> (-0.12%) ⬇️
plugins-microgateway-core 39.16% <ø> (-0.11%) ⬇️
plugins-moleculer 40.50% <ø> (-0.11%) ⬇️
plugins-mongodb 39.19% <ø> (-0.11%) ⬇️
plugins-mongodb-core 39.03% <ø> (-0.11%) ⬇️
plugins-mongoose 38.85% <ø> (-0.11%) ⬇️
plugins-multer 26.51% <ø> (ø)
plugins-mysql 39.16% <ø> (-0.11%) ⬇️
plugins-mysql2 39.26% <ø> (-0.11%) ⬇️
plugins-node-serialize 26.81% <ø> (ø)
plugins-opensearch 37.62% <ø> (-0.11%) ⬇️
plugins-passport-http 26.57% <ø> (ø)
plugins-postgres 35.71% <16.66%> (-0.11%) ⬇️
plugins-process 26.79% <ø> (ø)
plugins-pug 26.77% <ø> (ø)
plugins-redis 38.89% <ø> (-0.11%) ⬇️
plugins-router 42.96% <ø> (-0.11%) ⬇️
plugins-sequelize 25.35% <ø> (ø)
plugins-test-and-upstream-amqp10 38.34% <ø> (-0.11%) ⬇️
plugins-test-and-upstream-amqplib 43.82% <ø> (-0.12%) ⬇️
plugins-test-and-upstream-apollo 39.02% <ø> (-0.10%) ⬇️
plugins-test-and-upstream-avsc 38.70% <ø> (-0.11%) ⬇️
plugins-test-and-upstream-bunyan 33.88% <ø> (-0.11%) ⬇️
plugins-test-and-upstream-connect 40.78% <ø> (-0.11%) ⬇️
plugins-test-and-upstream-graphql 40.13% <ø> (-0.11%) ⬇️
plugins-test-and-upstream-koa 40.36% <ø> (-0.11%) ⬇️
plugins-test-and-upstream-protobufjs 38.93% <ø> (-0.11%) ⬇️
plugins-test-and-upstream-rhea 44.01% <ø> (-0.12%) ⬇️
plugins-undici 39.11% <ø> (-0.10%) ⬇️
plugins-url 26.79% <ø> (ø)
plugins-valkey 38.08% <ø> (-0.11%) ⬇️
plugins-vm 26.79% <ø> (ø)
plugins-winston 34.07% <ø> (-0.11%) ⬇️
plugins-ws 41.87% <ø> (-0.11%) ⬇️
profiling-macos 39.83% <ø> (-0.11%) ⬇️
profiling-ubuntu 39.95% <ø> (-0.11%) ⬇️
profiling-windows 41.16% <ø> (-0.11%) ⬇️
serverless-azure-functions-client 25.43% <ø> (ø)
serverless-azure-functions-eventhubs 25.43% <ø> (ø)
serverless-azure-functions-servicebus 25.43% <ø> (ø)

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

☔ View full report in Codecov by Sentry.
📢 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.

@datadog-datadog-prod-us1

This comment has been minimized.

@pr-commenter
Copy link
Copy Markdown

pr-commenter bot commented Mar 2, 2026

Benchmarks

Benchmark execution time: 2026-03-03 12:32:28

Comparing candidate commit 27dbad3 in PR branch juan-fernandez/fix-retries-efd-jest with baseline commit d7bee55 in branch master.

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

@juan-fernandez juan-fernandez changed the title [do not merge] Fix EFD retries [test optimization] Fix EFD retries in jest Mar 3, 2026
@juan-fernandez
Copy link
Copy Markdown
Collaborator Author

E2E Test Report: SUCCESS ✅

Tested by: Shepherd Agent (autonomous QA for Datadog Test Optimization)

Test Environment

  • Method: Local testing with decap-cms playground (Jest 27.5.1, jest-circus, Node 24)
  • Revision tested: 6add8a03352fd34003899fe0ee89c9250a77cbb8 ("fix logic")
  • Scenario: EFD enabled, all tests new (known_tests_enabled: true, early_flake_detection.enabled: true, faulty_session_threshold: 100, 5s: 10 retries)

Results

Check Status
EFD active on session (test.early_flake.enabled) true
Tests detected as new (test.is_new) ✅ all 12 tests
EFD retries scheduled (10× per test, < 5 s bucket) ✅ 120 retries
Retry tags (test.is_retry, test.retry_reason) early_flake_detection on all 120
No jest-circus registration errors ✅ confirmed
Session status pass
Exit code 0
Total test spans (12 original + 120 retries) ✅ 132

Issues Found & Resolved

An earlier commit on this branch (before "fix logic") was broken on Jest 27.5.1:

  • this.global.test() was called from test_done (execution phase)
  • jest-circus ≥27 throws "Cannot add a test after tests have started running" when state.hasStarted = true
  • This fired 121 times, scheduling zero retries and marking the suite as failed

The "fix logic" commit resolves this by temporarily setting state.hasStarted = false and state.currentDescribeBlock = event.test.parent before calling retryTest(), then immediately restoring both. This correctly bypasses the jest-circus registration guard while keeping state consistent.

Test Methodology

  1. Ran decap-cms (Jest 27.5.1) with --dep ddtracejs=juan-fernandez/fix-retries-efd-jest --scenario efd-new-test --debug
  2. Used mockdog as mock backend with EFD enabled and faulty_session_threshold: 100 (all tests are new)
  3. Verified span tags and retry counts via bin/query-spans summary
  4. Confirmed zero jest-circus errors in debug logs

This E2E test was performed by Shepherd — autonomous QA agent for Datadog Test Optimization

@juan-fernandez juan-fernandez marked this pull request as ready for review March 3, 2026 12:55
@juan-fernandez juan-fernandez requested a review from a team as a code owner March 3, 2026 12:55
@juan-fernandez juan-fernandez merged commit 7ff02ca into master Mar 3, 2026
788 checks passed
@juan-fernandez juan-fernandez deleted the juan-fernandez/fix-retries-efd-jest branch March 3, 2026 13:37
dd-octo-sts bot pushed a commit that referenced this pull request Mar 4, 2026
@dd-octo-sts dd-octo-sts bot mentioned this pull request Mar 4, 2026
crysmags pushed a commit that referenced this pull request Mar 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