[test optimization] Propagate test span context during vitest test execution#7882
Conversation
…ecution Wrap vitest test functions and beforeEach/afterEach hooks to run inside the test span's AsyncLocalStorage context, ensuring HTTP requests and custom tags are properly linked to the test span. Fixes #7874 Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
Overall package sizeSelf size: 5.06 MB Dependency sizes| name | version | self size | total size | |------|---------|-----------|------------| | import-in-the-middle | 3.0.0 | 81.15 kB | 815.98 kB | | dc-polyfill | 0.1.10 | 26.73 kB | 26.73 kB |🤖 This report was automatically generated by heaviest-objects-in-the-universe |
|
✅ Tests 🎉 All green!❄️ No new flaky tests detected 🎯 Code Coverage (details) 🔗 Commit SHA: e68e937 | Docs | Datadog PR Page | Was this helpful? React with 👍/👎 or give us feedback! |
vitest test execution
BenchmarksBenchmark execution time: 2026-03-30 11:50:13 Comparing candidate commit e68e937 in PR branch Found 0 performance improvements and 0 performance regressions! Performance is the same for 226 metrics, 34 unstable metrics. |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #7882 +/- ##
==========================================
- Coverage 74.62% 74.51% -0.11%
==========================================
Files 768 768
Lines 35673 35728 +55
==========================================
+ Hits 26620 26624 +4
- Misses 9053 9104 +51 Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
…ion test Verify that HTTP spans from vitest's beforeEach cleanup pattern (returning a function from beforeEach) are also linked as children of the test span. Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
f8dece7 to
e68e937
Compare
Summary
@vitest/runner'sgetFn/setFnto run inside the test span'sAsyncLocalStoragecontext via a newci:vitest:test:fnchannel (mirroring Jest'stestFnChpattern)beforeEach/afterEachhooks by walking the suite hierarchy and usinggetHooks(vitest 4+ WeakMap) orsuite.hooks(older versions)addHookfor@vitest/runner(vitest <4) andfindExportByNameon bundled chunks (vitest 4+)Without this fix,
testStartCh.runStores(ctx, () => {})only activated the store during the empty callback — the actual test function and hooks ran after it returned, so HTTP spans became orphaned root spans andtracer.scope().active()returnednull.Fixes #7874
Test plan
tracer.scope().active()appear on the test spanbeforeEach/afterEachhooks are children of the test spanmasterand pass with the fix🤖 Generated with Claude Code