[ci-visibility] Fix active span in mocha hooks#2167
Conversation
Codecov Report
@@ Coverage Diff @@
## master #2167 +/- ##
==========================================
+ Coverage 92.96% 93.00% +0.03%
==========================================
Files 213 213
Lines 8515 8534 +19
==========================================
+ Hits 7916 7937 +21
+ Misses 599 597 -2
📣 Codecov can now indicate which changes are the most critical in Pull Requests. Learn more |
| if (!testStartCh.hasSubscribers || isRetry(this.test)) { | ||
| return runTest.apply(this, arguments) | ||
| shimmer.wrap(Runner.prototype, 'run', run => function () { | ||
| if (!testStartCh.hasSubscribers) { |
There was a problem hiding this comment.
run is only called once per test run, so we set up here every listener that we need
| status = 'fail' | ||
| } | ||
|
|
||
| if (!test.parent._afterEach.length) { |
There was a problem hiding this comment.
if there are afterEach hooks, we don't finish the test yet
| return | ||
| } | ||
| const asyncResource = new AsyncResource('bound-anonymous-fn') | ||
| testToAr.set(test.fn, asyncResource) |
There was a problem hiding this comment.
we store the test's async resource here so we can run the hooks with the correct resource
| if (isLastAfterEach) { | ||
| const asyncResource = getTestAsyncResource(test) | ||
| asyncResource.runInAsyncScope(() => { | ||
| testFinishCh.publish('pass') |
There was a problem hiding this comment.
we finish the test on the last afterEach
| // we store the original function, not to lose it | ||
| originalFns.set(newFn, this.fn) | ||
|
|
||
| this.fn = newFn |
There was a problem hiding this comment.
this piece is so that the test function and hook functions are called with the correct context (we bind the asyncResource)
Implements proper span parenting for Jest's beforeAll and afterAll hooks to ensure they are traced as children of the test suite span, similar to the Mocha implementation in PR DataDog#2167. Changes: - Add hook type detection functions (getHookType, isSuiteLevelHook) - Create suite-level channel (suiteFnCh) for suite hook execution - Modify hook wrapping to use appropriate context (suite vs test) - Store suite context in DatadogEnvironment and suiteContexts map - Update plugin to handle suite function channel binding - Add comprehensive tests for hook instrumentation This ensures beforeAll/afterAll hooks create spans parented to the test suite span, while beforeEach/afterEach remain parented to test spans. Fixes DataDog#5677
What does this PR do?
Fix the active spans in mocha hooks.
This rewrites the whole mocha plugin to fix the active span in
beforeEachandafterEachhooks.Motivation
Fixes #2157
Plugin Checklist
Additional Notes