Skip to content

fix(vitest): prevent negative test durations#9380

Merged
BridgeAR merged 1 commit into
masterfrom
juan-fernandez/vitest-negative-test-duration
Jul 15, 2026
Merged

fix(vitest): prevent negative test durations#9380
BridgeAR merged 1 commit into
masterfrom
juan-fernandez/vitest-negative-test-duration

Conversation

@juan-fernandez

Copy link
Copy Markdown
Collaborator

What does this PR do?

Prevents failed Vitest tests shorter than five milliseconds from producing negative test span durations.

The duration adjustment is now clamped at zero. A Vitest core integration test runs a real failing test through the worker instrumentation, plugin, serialization, and fake intake paths with a deterministic one-millisecond framework duration.

Motivation

Vitest reports the failed task duration to the plugin, which subtracts five milliseconds to avoid overlap with suite spans. Durations below that threshold previously finished before their start time.

The direct diagnostic-channel test in #9342 only exercised the arithmetic with a fabricated span. This version incorporates Ruben's production fix and verifies the original failure at the integration boundary instead.

Additional Notes

Before the production change, the regression test reported -4,000,000 ns with both Vitest 1.6.0 and the latest supported version.

Validation:

  • SPEC=vitest.core npm run test:integration:vitest -- --grep "does not report negative durations" — 2 passing
  • Targeted ESLint for all five changed files
  • git diff --check

Clamp short failed-test durations at zero and cover the regression through a real Vitest integration run.

Co-authored-by: Ruben Bridgewater <[email protected]>
@dd-octo-sts

dd-octo-sts Bot commented Jul 15, 2026

Copy link
Copy Markdown
Contributor

Overall package size

Self size: 6.77 MB
Deduped: 7.43 MB
No deduping: 7.43 MB

Dependency sizes | name | version | self size | total size | |------|---------|-----------|------------| | import-in-the-middle | 3.3.1 | 122.62 kB | 438.86 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
juan-fernandez marked this pull request as ready for review July 15, 2026 13:13
@juan-fernandez
juan-fernandez requested a review from a team as a code owner July 15, 2026 13:13
@juan-fernandez

Copy link
Copy Markdown
Collaborator Author

@codex review

@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 duration-clamping fix is arithmetically correct and the condition change from if (duration) to if (Number.isFinite(duration) && duration >= 0) is strictly safer — it now also falls back to clock time for Infinity and negative raw durations from Vitest, both of which the old truthy check silently accepted and could produce bad spans. The integration test correctly exercises the fix: the custom runner sets task.result.duration = 1 before the startTests post-processing loop reads it, and the assert.strictEqual(Number(tests[0].duration), 0) assertion matches the clamped result of Math.max(1−5, 0)=0.

Was this helpful? React 👍 or 👎

📊 Validated against 16 scenarios · Open Bits AI session

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

@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: f60bc1b080

ℹ️ 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".

@pr-commenter

pr-commenter Bot commented Jul 15, 2026

Copy link
Copy Markdown

Benchmarks

Benchmark execution time: 2026-07-15 13:23:41

Comparing candidate commit f60bc1b in PR branch juan-fernandez/vitest-negative-test-duration with baseline commit 57429c3 in branch master.

📊 Benchmarking dashboard

Found 0 performance improvements and 0 performance regressions! Performance is the same for 2311 metrics, 47 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 [-169.081ms; +236.361ms] or [-6.443%; +9.007%]

scenario:appsec-appsec-enabled-26

  • unstable execution_time [-237.776ms; +231.625ms] or [-9.264%; +9.025%]

scenario:appsec-appsec-enabled-with-attacks-24

  • unstable execution_time [-160.127ms; +157.828ms] or [-5.194%; +5.120%]

scenario:appsec-appsec-enabled-with-attacks-26

  • unstable execution_time [-156.744ms; +201.432ms] or [-5.436%; +6.986%]

scenario:appsec-control-20

  • unstable execution_time [-138.282ms; +114.851ms] or [-8.403%; +6.979%]

scenario:appsec-control-24

  • unstable execution_time [-121.564ms; +101.617ms] or [-9.849%; +8.233%]

scenario:appsec-control-26

  • unstable execution_time [-122.218ms; +118.304ms] or [-9.880%; +9.563%]

scenario:appsec-iast-no-vulnerability-iast-enabled-always-active-20

  • unstable execution_time [-16857.262µs; +15386.062µs] or [-6.609%; +6.032%]

scenario:appsec-iast-no-vulnerability-iast-enabled-default-config-20

  • unstable execution_time [-12.097ms; +19.030ms] or [-4.703%; +7.399%]

scenario:appsec-iast-with-vulnerability-iast-enabled-always-active-20

  • unstable execution_time [-29.917ms; +39.479ms] or [-5.392%; +7.115%]

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

  • unstable cpu_user_time [-2562.873ms; +2472.967ms] or [-29.190%; +28.166%]
  • unstable execution_time [-2569.817ms; +2494.632ms] or [-26.977%; +26.188%]
  • unstable instructions [-21807.3M instructions; +20702.8M instructions] or [-30.319%; +28.784%]
  • unstable max_rss_usage [-10446.522KB; +9964.122KB] or [-6.575%; +6.271%]
  • unstable throughput [-713.204op/s; +691.840op/s] or [-20.267%; +19.660%]

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

  • unstable cpu_user_time [-2550.651ms; +2351.229ms] or [-29.021%; +26.752%]
  • unstable execution_time [-2561.777ms; +2351.941ms] or [-26.914%; +24.709%]
  • unstable instructions [-21919.3M instructions; +20606.6M instructions] or [-30.442%; +28.619%]
  • unstable max_rss_usage [-10495.526KB; +10093.926KB] or [-6.602%; +6.350%]
  • unstable throughput [-665.798op/s; +715.225op/s] or [-18.975%; +20.384%]

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

  • unstable cpu_user_time [-3.554s; +0.471s] or [-34.454%; +4.565%]
  • unstable execution_time [-3.614s; +0.506s] or [-32.713%; +4.578%]
  • unstable instructions [-31.7G instructions; +4.2G instructions] or [-36.605%; +4.851%]
  • unstable throughput [-102.754op/s; +702.899op/s] or [-3.337%; +22.827%]

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

  • unstable cpu_user_time [-1.916s; +4.088s] or [-21.877%; +46.678%]
  • unstable execution_time [-1.919s; +4.104s] or [-20.276%; +43.360%]
  • unstable instructions [-16.2G instructions; +34.9G instructions] or [-22.529%; +48.595%]
  • unstable max_rss_usage [-7.963MB; +16.924MB] or [-5.015%; +10.658%]
  • unstable throughput [-1133.445op/s; +530.119op/s] or [-32.092%; +15.009%]

scenario:dogstatsd-aggregated-20

  • unstable cpu_usage_percentage [-7.408%; +4.553%]
  • unstable execution_time [-90.014ms; +145.405ms] or [-6.979%; +11.274%]
  • unstable throughput [-909534.350op/s; +557934.327op/s] or [-7.727%; +4.740%]

scenario:dogstatsd-with-tags-20

  • unstable cpu_user_time [-146.402ms; +459.237ms] or [-3.006%; +9.429%]
  • unstable execution_time [-147.652ms; +462.388ms] or [-2.983%; +9.342%]
  • unstable throughput [-165541.021op/s; +49240.839op/s] or [-9.756%; +2.902%]

scenario:log-with-debug-20

  • unstable max_rss_usage [-8.249MB; +5.268MB] or [-7.191%; +4.593%]

scenario:plugin-claude-agent-sdk-compact-stream-scan-26

  • unstable cpu_user_time [-3565.972µs; +2846.572µs] or [-5.898%; +4.708%]

scenario:plugin-graphql-long-with-depth-on-max-20

  • unstable cpu_user_time [-719.597ms; +778.945ms] or [-5.610%; +6.072%]
  • unstable execution_time [-725.975ms; +788.120ms] or [-5.535%; +6.009%]
  • unstable throughput [-3.603op/s; +3.333op/s] or [-5.869%; +5.430%]

scenario:plugin-pg-service-20

  • unstable cpu_usage_percentage [-8.651%; +5.429%]
  • unstable execution_time [-132.089ms; +212.524ms] or [-8.082%; +13.004%]
  • unstable throughput [-338645.723op/s; +209142.993op/s] or [-9.113%; +5.628%]

scenario:plugin-pg-service-26

  • unstable cpu_usage_percentage [-6.739%; +6.714%]
  • unstable execution_time [-85.152ms; +80.813ms] or [-9.353%; +8.877%]
  • unstable throughput [-462901.864op/s; +503025.726op/s] or [-6.894%; +7.491%]

scenario:test-optimization-large-suite-20

  • unstable max_rss_usage [-4.127MB; +9.195MB] or [-5.039%; +11.227%]

@datadog-datadog-prod-us1-2

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

Copy link
Copy Markdown

Tests

🎉 All green!

🧪 All tests passed
❄️ No new flaky tests detected

🔄 Datadog auto-retried 1 job - 1 passed on retry View in Datadog

🎯 Code Coverage (details)
Patch Coverage: 100.00%
Overall Coverage: 96.61% (+0.00%)

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

@codecov

codecov Bot commented Jul 15, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 96.30%. Comparing base (57429c3) to head (f60bc1b).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #9380      +/-   ##
==========================================
- Coverage   96.65%   96.30%   -0.35%     
==========================================
  Files         923      923              
  Lines      122862   122946      +84     
  Branches    21061    21024      -37     
==========================================
- Hits       118749   118403     -346     
- Misses       4113     4543     +430     
Flag Coverage Δ
aiguard 53.64% <ø> (-0.05%) ⬇️
aiguard-integration 57.10% <ø> (ø)
apm-bucket-0 53.87% <ø> (-0.05%) ⬇️
apm-bucket-1 58.45% <ø> (-0.05%) ⬇️
apm-bucket-2 56.17% <ø> (-0.05%) ⬇️
apm-capabilities-tracing 60.63% <0.00%> (-0.01%) ⬇️
apm-integrations-aerospike 53.03% <ø> (-0.05%) ⬇️
apm-integrations-confluentinc-kafka-javascript 57.17% <ø> (-0.06%) ⬇️
apm-integrations-couchbase 53.36% <ø> (-0.05%) ⬇️
apm-integrations-http 58.03% <ø> (-0.05%) ⬇️
apm-integrations-kafkajs 57.82% <ø> (-0.05%) ⬇️
apm-integrations-next 54.25% <ø> (-0.05%) ⬇️
apm-integrations-prisma 54.25% <ø> (-0.05%) ⬇️
apm-integrations-tedious 53.13% <ø> (-0.05%) ⬇️
appsec 68.86% <ø> (-0.04%) ⬇️
appsec-express_fastify_graphql 65.73% <ø> (-0.04%) ⬇️
appsec-integration 51.93% <ø> (-0.02%) ⬇️
appsec-kafka_ldapjs_lodash 59.16% <ø> (-0.04%) ⬇️
appsec-mongodb-core_mongoose_mysql 62.39% <ø> (-0.04%) ⬇️
appsec-next 52.85% <ø> (-0.07%) ⬇️
appsec-node-serialize_passport_postgres 62.04% <ø> (-0.04%) ⬇️
appsec-sourcing_stripe_template 60.43% <ø> (-0.04%) ⬇️
debugger 65.69% <ø> (-0.02%) ⬇️
instrumentations-bucket-0 48.87% <ø> (-0.05%) ⬇️
instrumentations-bucket-1 55.08% <ø> (-0.05%) ⬇️
instrumentations-bucket-10 56.70% <ø> (-0.05%) ⬇️
instrumentations-bucket-11 48.87% <ø> (-0.05%) ⬇️
instrumentations-bucket-12 49.04% <ø> (-0.05%) ⬇️
instrumentations-bucket-13 48.81% <ø> (-0.05%) ⬇️
instrumentations-bucket-2 50.01% <ø> (-0.05%) ⬇️
instrumentations-bucket-3 53.98% <ø> (-0.05%) ⬇️
instrumentations-bucket-4 49.28% <ø> (-0.05%) ⬇️
instrumentations-bucket-5 53.13% <ø> (-0.06%) ⬇️
instrumentations-bucket-6 55.72% <ø> (-0.05%) ⬇️
instrumentations-bucket-7 53.62% <ø> (-0.05%) ⬇️
instrumentations-bucket-8 54.76% <ø> (-0.05%) ⬇️
instrumentations-bucket-9 56.18% <ø> (-0.04%) ⬇️
instrumentations-instrumentation-couchbase 48.27% <ø> (-0.05%) ⬇️
instrumentations-integration-esbuild 34.05% <ø> (-0.02%) ⬇️
llmobs-ai_anthropic_bedrock 57.77% <ø> (-0.04%) ⬇️
llmobs-bucket-1 57.10% <ø> (-0.04%) ⬇️
llmobs-openai 57.50% <ø> (-0.04%) ⬇️
llmobs-sdk 60.06% <ø> (-0.05%) ⬇️
llmobs-vertex-ai 54.31% <ø> (-0.05%) ⬇️
master-coverage 96.30% <100.00%> (?)
openfeature ?
openfeature-unit 49.90% <ø> (-0.05%) ⬇️
platform-core_esbuild_instrumentations-misc 38.52% <ø> (-0.04%) ⬇️
platform-integration 62.32% <ø> (ø)
platform-shimmer_unit-guardrails_webpack 37.32% <ø> (-0.04%) ⬇️
plugins-bucket-0 ?
plugins-bucket-1 55.19% <ø> (ø)
plugins-bucket-11 57.53% <ø> (-0.04%) ⬇️
plugins-bucket-18 57.14% <ø> (-0.05%) ⬇️
plugins-bucket-19 55.44% <ø> (-0.07%) ⬇️
plugins-bucket-20 57.66% <ø> (-0.05%) ⬇️
plugins-bucket-4 53.87% <ø> (-0.05%) ⬇️
plugins-bullmq_cassandra_cookie 57.47% <ø> (-0.05%) ⬇️
plugins-cookie-parser_crypto_dd-trace-api 52.41% <ø> (-0.05%) ⬇️
plugins-fetch_fs_generic-pool 54.66% <ø> (+0.03%) ⬆️
plugins-google-cloud-pubsub_grpc_handlebars 59.91% <ø> (-0.05%) ⬇️
plugins-hapi_hono_ioredis 55.85% <ø> (-0.05%) ⬇️
plugins-jest_knex_langgraph 51.78% <ø> (-0.03%) ⬇️
plugins-ldapjs_light-my-request_limitd-client 53.50% <ø> (-0.05%) ⬇️
plugins-lodash_mariadb_memcached 54.29% <ø> (-0.05%) ⬇️
plugins-moleculer_mongodb_mongodb-core 57.39% <ø> (-0.05%) ⬇️
plugins-mongoose_multer_mysql 54.78% <ø> (-0.05%) ⬇️
plugins-mysql2_nats_node-serialize 56.32% <ø> (-0.05%) ⬇️
plugins-opensearch_passport-http_pino 55.18% <ø> (-0.05%) ⬇️
plugins-postgres_process_pug 54.37% <ø> (-0.05%) ⬇️
plugins-redis_router_sequelize 57.19% <ø> (-0.03%) ⬇️
plugins-test-and-upstream-rhea_undici_url 57.06% <ø> (-0.05%) ⬇️
plugins-valkey_vm_winston 53.94% <ø> (-0.05%) ⬇️
plugins-ws 54.92% <ø> (-0.05%) ⬇️
profiling 58.25% <ø> (-0.04%) ⬇️
serverless-aws-sdk-aws-sdk 50.75% <ø> (-0.06%) ⬇️
serverless-aws-sdk-bedrockruntime 50.74% <ø> (-0.05%) ⬇️
serverless-aws-sdk-client 52.08% <ø> (-0.05%) ⬇️
serverless-aws-sdk-dynamodb 51.80% <ø> (-0.05%) ⬇️
serverless-aws-sdk-eventbridge 46.44% <ø> (-0.06%) ⬇️
serverless-aws-sdk-kinesis 54.79% <ø> (-0.05%) ⬇️
serverless-aws-sdk-lambda 52.86% <ø> (-0.05%) ⬇️
serverless-aws-sdk-s3 51.58% <ø> (-0.05%) ⬇️
serverless-aws-sdk-serverless-peer-service 54.81% <ø> (-0.05%) ⬇️
serverless-aws-sdk-sns 55.53% <ø> (-0.05%) ⬇️
serverless-aws-sdk-sqs 55.96% <ø> (-0.05%) ⬇️
serverless-aws-sdk-stepfunctions 51.35% <ø> (-0.05%) ⬇️
serverless-aws-sdk-util 48.53% <ø> (-0.05%) ⬇️
serverless-bucket-0 55.11% <ø> (ø)
serverless-bucket-1 56.06% <ø> (-0.05%) ⬇️
test-optimization-cucumber 72.99% <ø> (+0.03%) ⬆️
test-optimization-cypress 66.44% <ø> (+0.18%) ⬆️
test-optimization-jest 74.37% <ø> (+0.03%) ⬆️
test-optimization-mocha 74.74% <ø> (+0.08%) ⬆️
test-optimization-playwright-playwright-atr 61.45% <ø> (-0.03%) ⬇️
test-optimization-playwright-playwright-efd 61.64% <ø> (-0.03%) ⬇️
test-optimization-playwright-playwright-final-status 61.61% <ø> (-0.04%) ⬇️
test-optimization-playwright-playwright-impacted-tests 61.33% <ø> (+0.13%) ⬆️
test-optimization-playwright-playwright-reporting 61.22% <ø> (+0.03%) ⬆️
test-optimization-playwright-playwright-test-management 62.17% <ø> (-0.15%) ⬇️
test-optimization-playwright-playwright-test-span 61.36% <ø> (-0.09%) ⬇️
test-optimization-selenium 60.74% <ø> (-0.11%) ⬇️
test-optimization-testopt 59.23% <0.00%> (+0.15%) ⬆️
test-optimization-vitest 71.30% <100.00%> (-0.06%) ⬇️

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.

@BridgeAR
BridgeAR merged commit 9aaca0e into master Jul 15, 2026
774 of 775 checks passed
@BridgeAR
BridgeAR deleted the juan-fernandez/vitest-negative-test-duration branch July 15, 2026 14:11
dd-octo-sts Bot pushed a commit that referenced this pull request Jul 16, 2026
Clamp short failed-test durations at zero and cover the regression through a real Vitest integration run.

Co-authored-by: Ruben Bridgewater <[email protected]>
@dd-octo-sts dd-octo-sts Bot mentioned this pull request Jul 16, 2026
dd-octo-sts Bot pushed a commit that referenced this pull request Jul 16, 2026
Clamp short failed-test durations at zero and cover the regression through a real Vitest integration run.

Co-authored-by: Ruben Bridgewater <[email protected]>
@dd-octo-sts dd-octo-sts Bot mentioned this pull request Jul 16, 2026
juan-fernandez added a commit that referenced this pull request Jul 16, 2026
Clamp short failed-test durations at zero and cover the regression through a real Vitest integration run.

Co-authored-by: Ruben Bridgewater <[email protected]>
juan-fernandez added a commit that referenced this pull request Jul 16, 2026
Clamp short failed-test durations at zero and cover the regression through a real Vitest integration run.

Co-authored-by: Ruben Bridgewater <[email protected]>
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