Skip to content

fix(vitest): prevent negative test durations#9342

Closed
BridgeAR wants to merge 1 commit into
masterfrom
BridgeAR/2026-07-14-clamp-vitest-duration
Closed

fix(vitest): prevent negative test durations#9342
BridgeAR wants to merge 1 commit into
masterfrom
BridgeAR/2026-07-14-clamp-vitest-duration

Conversation

@BridgeAR

Copy link
Copy Markdown
Member

Summary

Failed Vitest tests shorter than five milliseconds finished before their start time because the overlap adjustment was not bounded.

Test plan

  • Run exact 0, 1, 4, and 5 ms duration boundaries plus invalid inputs
  • Verify the plugin spec is exercised by CI
  • Check changed-line and branch coverage

Failed tests shorter than five milliseconds finished before their start time because the overlap adjustment was not bounded.
@dd-octo-sts

dd-octo-sts Bot commented Jul 14, 2026

Copy link
Copy Markdown
Contributor

Overall package size

Self size: 6.74 MB
Deduped: 7.4 MB
No deduping: 7.4 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

@BridgeAR
BridgeAR marked this pull request as ready for review July 14, 2026 16:39
@BridgeAR
BridgeAR requested a review from a team as a code owner July 14, 2026 16:39
@datadog-datadog-us1-prod

datadog-datadog-us1-prod Bot commented Jul 14, 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.57% (+0.00%)

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

@datadog-datadog-us1-prod datadog-datadog-us1-prod 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 PR fixes a critical bug where failed Vitest tests finishing in under 5ms would produce negative span durations in trace data. The fix adds proper validation (checking for finite, non-negative durations) and clamps the result with Math.max to prevent negatives. New test suite comprehensively covers boundary cases (0–5ms) and invalid inputs. No production code beyond this single location is affected.

Was this helpful? React 👍 or 👎

📊 Validated against 9 scenarios · Open Bits AI session

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

@pr-commenter

pr-commenter Bot commented Jul 14, 2026

Copy link
Copy Markdown

Benchmarks

Benchmark execution time: 2026-07-14 16:49:20

Comparing candidate commit 9346ecd in PR branch BridgeAR/2026-07-14-clamp-vitest-duration with baseline commit 0ad9953 in branch master.

📊 Benchmarking dashboard

Found 0 performance improvements and 0 performance regressions! Performance is the same for 2314 metrics, 44 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 [-210.867ms; +218.688ms] or [-7.914%; +8.207%]

scenario:appsec-appsec-enabled-26

  • unstable execution_time [-234.516ms; +228.700ms] or [-9.191%; +8.963%]

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

  • unstable execution_time [-155.234ms; +165.242ms] or [-5.008%; +5.331%]

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

  • unstable execution_time [-190.905ms; +185.810ms] or [-6.563%; +6.388%]

scenario:appsec-control-20

  • unstable execution_time [-118.041ms; +140.227ms] or [-7.161%; +8.507%]

scenario:appsec-control-24

  • unstable execution_time [-114.264ms; +111.575ms] or [-9.221%; +9.004%]

scenario:appsec-control-26

  • unstable execution_time [-126.596ms; +128.633ms] or [-10.216%; +10.380%]

scenario:appsec-iast-no-vulnerability-control-20

  • unstable execution_time [-12961.277µs; +13972.477µs] or [-5.031%; +5.423%]

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

  • unstable execution_time [-16.372ms; +14.090ms] or [-6.402%; +5.510%]

scenario:appsec-iast-with-vulnerability-control-20

  • unstable execution_time [-27.734ms; +34.469ms] or [-5.064%; +6.294%]

scenario:child_process-file-args-24

  • unstable cpu_usage_percentage [-6.646%; +3.644%]
  • unstable execution_time [-20.505ms; +44.005ms] or [-4.324%; +9.280%]
  • unstable throughput [-173412.578op/s; +80491.535op/s] or [-7.872%; +3.654%]

scenario:child_process-shell-string-24

  • unstable cpu_usage_percentage [-3.873%; +6.331%]
  • unstable execution_time [-22.661ms; +13.496ms] or [-6.846%; +4.077%]

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

  • unstable cpu_user_time [-1550.944ms; +683.978ms] or [-17.874%; +7.882%]
  • unstable execution_time [-1615.273ms; +732.573ms] or [-17.196%; +7.799%]
  • unstable instructions [-13.1G instructions; +5.7G instructions] or [-18.369%; +7.997%]
  • unstable throughput [-229.779op/s; +408.992op/s] or [-6.568%; +11.690%]

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

  • unstable cpu_user_time [-4.301s; +0.024s] or [-39.393%; +0.221%]
  • unstable execution_time [-4.331s; +0.077s] or [-37.117%; +0.659%]
  • unstable instructions [-38.4G instructions; +0.1G instructions] or [-41.666%; +0.115%]
  • unstable throughput [-25.401op/s; +865.217op/s] or [-0.863%; +29.393%]

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

  • unstable cpu_user_time [-3100.685ms; +1810.839ms] or [-33.270%; +19.430%]
  • unstable execution_time [-3143.770ms; +1853.915ms] or [-31.312%; +18.465%]
  • unstable instructions [-26.7G instructions; +15.6G instructions] or [-34.793%; +20.379%]
  • unstable max_rss_usage [-11.280MB; +7.155MB] or [-7.017%; +4.451%]
  • unstable throughput [-512.366op/s; +906.229op/s] or [-15.323%; +27.102%]

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

  • unstable cpu_user_time [-1836.140ms; +3153.010ms] or [-22.063%; +37.887%]
  • unstable execution_time [-1837.764ms; +3168.148ms] or [-20.341%; +35.066%]
  • unstable instructions [-15.8G instructions; +26.8G instructions] or [-23.140%; +39.323%]
  • unstable max_rss_usage [-8.265MB; +13.065MB] or [-5.268%; +8.327%]
  • unstable throughput [-857.729op/s; +473.423op/s] or [-23.553%; +13.000%]

scenario:dogstatsd-with-tags-20

  • unstable cpu_user_time [-245.088ms; +397.205ms] or [-5.033%; +8.157%]
  • unstable execution_time [-243.632ms; +394.888ms] or [-4.924%; +7.981%]
  • unstable throughput [-141157.306op/s; +84335.126op/s] or [-8.320%; +4.971%]

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

  • unstable cpu_usage_percentage [-5.529%; +4.598%]

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

  • unstable cpu_user_time [-719.925ms; +708.135ms] or [-5.615%; +5.523%]
  • unstable execution_time [-718486.136µs; +720415.636µs] or [-5.483%; +5.498%]
  • unstable throughput [-3.338op/s; +3.308op/s] or [-5.433%; +5.384%]

scenario:plugin-pg-service-26

  • unstable cpu_usage_percentage [-7.268%; +7.338%]
  • unstable execution_time [-112.493ms; +95.819ms] or [-12.051%; +10.265%]
  • unstable throughput [-508138.929op/s; +589733.382op/s] or [-7.703%; +8.940%]

scenario:plugin-pino-json-log-injection-24

  • unstable execution_time [-125.926ms; +176.041ms] or [-4.377%; +6.118%]

@codecov

codecov Bot commented Jul 14, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 96.61%. Comparing base (0ad9953) to head (9346ecd).

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #9342   +/-   ##
=======================================
  Coverage   96.61%   96.61%           
=======================================
  Files         920      920           
  Lines      122236   122238    +2     
  Branches    21070    21078    +8     
=======================================
+ Hits       118097   118099    +2     
  Misses       4139     4139           
Flag Coverage Δ
aiguard 53.55% <ø> (-0.05%) ⬇️
aiguard-integration 56.97% <ø> (ø)
apm-bucket-0 53.78% <ø> (-0.05%) ⬇️
apm-bucket-1 57.99% <ø> (-0.12%) ⬇️
apm-bucket-2 56.10% <ø> (-0.05%) ⬇️
apm-capabilities-tracing 60.35% <0.00%> (-0.03%) ⬇️
apm-integrations-aerospike 52.92% <ø> (-0.05%) ⬇️
apm-integrations-confluentinc-kafka-javascript 57.04% <ø> (-0.05%) ⬇️
apm-integrations-couchbase 53.27% <ø> (-0.04%) ⬇️
apm-integrations-http 57.94% <ø> (-0.05%) ⬇️
apm-integrations-kafkajs 57.69% <ø> (-0.05%) ⬇️
apm-integrations-next 54.23% <ø> (-0.05%) ⬇️
apm-integrations-prisma 54.16% <ø> (-0.05%) ⬇️
apm-integrations-tedious 53.03% <ø> (-0.05%) ⬇️
appsec 68.97% <ø> (+<0.01%) ⬆️
appsec-express_fastify_graphql 65.66% <ø> (-0.04%) ⬇️
appsec-integration 51.86% <ø> (ø)
appsec-kafka_ldapjs_lodash 59.07% <ø> (-0.04%) ⬇️
appsec-mongodb-core_mongoose_mysql 62.22% <ø> (-0.04%) ⬇️
appsec-next 52.84% <ø> (-0.04%) ⬇️
appsec-node-serialize_passport_postgres 61.85% <ø> (-0.04%) ⬇️
appsec-sourcing_stripe_template 60.31% <ø> (-0.04%) ⬇️
debugger 65.71% <ø> (-0.07%) ⬇️
instrumentations-bucket-0 48.75% <ø> (-0.05%) ⬇️
instrumentations-bucket-1 54.95% <ø> (-0.05%) ⬇️
instrumentations-bucket-10 56.61% <ø> (-0.05%) ⬇️
instrumentations-bucket-11 48.74% <ø> (-0.05%) ⬇️
instrumentations-bucket-12 48.85% <ø> (-0.05%) ⬇️
instrumentations-bucket-13 48.68% <ø> (-0.05%) ⬇️
instrumentations-bucket-2 49.74% <ø> (-0.05%) ⬇️
instrumentations-bucket-3 53.82% <ø> (-0.05%) ⬇️
instrumentations-bucket-4 49.16% <ø> (-0.05%) ⬇️
instrumentations-bucket-5 53.00% <ø> (-0.05%) ⬇️
instrumentations-bucket-6 55.58% <ø> (-0.05%) ⬇️
instrumentations-bucket-7 53.59% <ø> (-0.05%) ⬇️
instrumentations-bucket-8 54.63% <ø> (-0.05%) ⬇️
instrumentations-bucket-9 56.08% <ø> (-0.04%) ⬇️
instrumentations-instrumentation-couchbase 48.13% <ø> (-0.05%) ⬇️
instrumentations-integration-esbuild 33.89% <ø> (ø)
llmobs-ai_anthropic_bedrock 57.73% <ø> (-0.04%) ⬇️
llmobs-bucket-1 57.00% <ø> (-0.04%) ⬇️
llmobs-openai 57.39% <ø> (-0.04%) ⬇️
llmobs-sdk 59.96% <ø> (-0.05%) ⬇️
llmobs-vertex-ai 54.22% <ø> (-0.05%) ⬇️
master-coverage 96.61% <100.00%> (?)
openfeature 54.60% <ø> (-0.01%) ⬇️
openfeature-unit 49.78% <ø> (-0.05%) ⬇️
platform-core_esbuild_instrumentations-misc 38.37% <ø> (-0.03%) ⬇️
platform-integration 62.33% <ø> (ø)
platform-shimmer_unit-guardrails_webpack 37.20% <ø> (-0.03%) ⬇️
plugins-bucket-0 53.17% <ø> (-0.05%) ⬇️
plugins-bucket-1 55.02% <ø> (ø)
plugins-bucket-11 57.45% <ø> (-0.05%) ⬇️
plugins-bucket-18 57.06% <ø> (-0.05%) ⬇️
plugins-bucket-19 55.34% <ø> (-0.07%) ⬇️
plugins-bucket-20 57.42% <ø> (-0.05%) ⬇️
plugins-bucket-4 53.84% <ø> (-0.05%) ⬇️
plugins-bullmq_cassandra_cookie 57.34% <ø> (-0.05%) ⬇️
plugins-cookie-parser_crypto_dd-trace-api 52.31% <ø> (-0.05%) ⬇️
plugins-fetch_fs_generic-pool 54.45% <ø> (-0.13%) ⬇️
plugins-google-cloud-pubsub_grpc_handlebars 59.84% <ø> (-0.05%) ⬇️
plugins-hapi_hono_ioredis 55.84% <ø> (-0.05%) ⬇️
plugins-jest_knex_langgraph 51.66% <ø> (-0.05%) ⬇️
plugins-ldapjs_light-my-request_limitd-client 53.43% <ø> (-0.05%) ⬇️
plugins-lodash_mariadb_memcached 54.20% <ø> (-0.05%) ⬇️
plugins-moleculer_mongodb_mongodb-core 57.28% <ø> (-0.05%) ⬇️
plugins-mongoose_multer_mysql 54.69% <ø> (-0.05%) ⬇️
plugins-mysql2_nats_node-serialize 56.19% <ø> (-0.05%) ⬇️
plugins-opensearch_passport-http_pino 55.04% <ø> (-0.05%) ⬇️
plugins-postgres_process_pug 54.28% <ø> (-0.05%) ⬇️
plugins-redis_router_sequelize 56.99% <ø> (-0.05%) ⬇️
plugins-test-and-upstream-rhea_undici_url 57.01% <ø> (-0.03%) ⬇️
plugins-valkey_vitest_vm 53.46% <100.00%> (?)
plugins-valkey_vm_winston ?
plugins-winston_ws 55.25% <ø> (?)
plugins-ws ?
profiling 58.20% <ø> (-0.04%) ⬇️
serverless-aws-sdk-aws-sdk 50.66% <ø> (-0.04%) ⬇️
serverless-aws-sdk-bedrockruntime 50.68% <ø> (-0.05%) ⬇️
serverless-aws-sdk-client 51.93% <ø> (-0.05%) ⬇️
serverless-aws-sdk-dynamodb 51.74% <ø> (-0.05%) ⬇️
serverless-aws-sdk-eventbridge 46.22% <ø> (-0.05%) ⬇️
serverless-aws-sdk-kinesis 54.70% <ø> (-0.05%) ⬇️
serverless-aws-sdk-lambda 52.78% <ø> (-0.05%) ⬇️
serverless-aws-sdk-s3 51.53% <ø> (-0.05%) ⬇️
serverless-aws-sdk-serverless-peer-service 54.71% <ø> (-0.05%) ⬇️
serverless-aws-sdk-sns 55.47% <ø> (-0.05%) ⬇️
serverless-aws-sdk-sqs 55.90% <ø> (-0.05%) ⬇️
serverless-aws-sdk-stepfunctions 51.25% <ø> (-0.05%) ⬇️
serverless-aws-sdk-util 48.43% <ø> (-0.05%) ⬇️
serverless-bucket-0 55.17% <ø> (ø)
serverless-bucket-1 56.00% <ø> (-0.05%) ⬇️
test-optimization-cucumber 72.96% <ø> (+0.02%) ⬆️
test-optimization-cypress 66.35% <ø> (+0.11%) ⬆️
test-optimization-jest 74.22% <ø> (-0.03%) ⬇️
test-optimization-mocha 74.67% <ø> (+0.06%) ⬆️
test-optimization-playwright-playwright-atr 61.53% <ø> (+0.09%) ⬆️
test-optimization-playwright-playwright-efd 61.72% <ø> (+0.03%) ⬆️
test-optimization-playwright-playwright-final-status 61.70% <ø> (+0.02%) ⬆️
test-optimization-playwright-playwright-impacted-tests 61.41% <ø> (+0.19%) ⬆️
test-optimization-playwright-playwright-reporting 61.30% <ø> (+0.02%) ⬆️
test-optimization-playwright-playwright-test-management 62.27% <ø> (-0.09%) ⬇️
test-optimization-playwright-playwright-test-span 61.43% <ø> (-0.03%) ⬇️
test-optimization-selenium 60.64% <ø> (-0.14%) ⬇️
test-optimization-testopt 59.13% <0.00%> (+0.08%) ⬆️
test-optimization-vitest 71.53% <100.00%> (+0.01%) ⬆️

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 juan-fernandez left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

thanks!


const testErrorCh = channel('ci:vitest:test:error')

describe('VitestPlugin', () => {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

any way we can keep these in the vitest core integration tests? I'd like to avoid having yet another test file for vitest

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This kind of test is actually more difficult to achieve with an integration test, since the time the test takes is a real time and I am unsure how to write such a test to be deterministic

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I can look into it

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

@juan-fernandez

Copy link
Copy Markdown
Collaborator

#9380 should have better coverage than this

@BridgeAR

Copy link
Copy Markdown
Member Author

Superseded

@BridgeAR BridgeAR closed this Jul 15, 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