From 0894b4205ea6a35d393866ccb672cbcb266ab98b Mon Sep 17 00:00:00 2001 From: Juan Fernandez Date: Fri, 13 Mar 2026 16:24:06 +0100 Subject: [PATCH] fix(jest): clear per-run collections on run_finish and guard null ctx 1. Clear testsToBeRetried, retriedTestsToNumAttempts, and attemptToFixRetriedTestsStatuses in the run_finish handler to prevent memory accumulation when a jest worker runs multiple test suites sequentially. Note: newTestsTestStatuses and testSuiteAbsolutePathsWithFastCheck are intentionally NOT cleared here because they are read after run_finish in the jestAdapterWrapper result processing. 2. Add null guard on the test context in the test_done handler. If testContexts has no entry for a test, log a warning and return early instead of throwing on ctx.currentStore access. Co-Authored-By: Claude Opus 4.6 (1M context) --- packages/datadog-instrumentations/src/jest.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/packages/datadog-instrumentations/src/jest.js b/packages/datadog-instrumentations/src/jest.js index 4f6e42b5dc7..66395a91fe7 100644 --- a/packages/datadog-instrumentations/src/jest.js +++ b/packages/datadog-instrumentations/src/jest.js @@ -688,11 +688,15 @@ function getWrappedEnvironment (BaseEnvironment, jestVersion) { const mightHitBreakpoint = this.isDiEnabled && numTestExecutions >= 2 const ctx = testContexts.get(event.test) + if (!ctx) { + log.warn('"ci:jest:test_done": no context found for test "%s"', testName) + return + } const finalStatus = this.getFinalStatus(testName, status, - !!ctx?.isNew, - !!ctx?.isModified, + !!ctx.isNew, + !!ctx.isModified, isEfdRetry, isAttemptToFix, numTestExecutions) @@ -747,6 +751,9 @@ function getWrappedEnvironment (BaseEnvironment, jestVersion) { efdDeterminedRetries.clear() efdSlowAbortedTests.clear() efdNewTestCandidates.clear() + retriedTestsToNumAttempts.clear() + attemptToFixRetriedTestsStatuses.clear() + testsToBeRetried.clear() } if (event.name === 'test_skip' || event.name === 'test_todo') { const testName = getJestTestName(event.test, this.getShouldStripSeedFromTestName())