Skip to content

[ci-visibility] Fix active span in mocha hooks#2167

Merged
juan-fernandez merged 4 commits intomasterfrom
juan-fernandez/fix-active-span
Jul 8, 2022
Merged

[ci-visibility] Fix active span in mocha hooks#2167
juan-fernandez merged 4 commits intomasterfrom
juan-fernandez/fix-active-span

Conversation

@juan-fernandez
Copy link
Copy Markdown
Collaborator

@juan-fernandez juan-fernandez commented Jul 5, 2022

What does this PR do?

Fix the active spans in mocha hooks.

This rewrites the whole mocha plugin to fix the active span in beforeEach and afterEach hooks.

Motivation

Fixes #2157

Plugin Checklist

Additional Notes

@codecov
Copy link
Copy Markdown

codecov bot commented Jul 5, 2022

Codecov Report

Merging #2167 (51ef3ef) into master (c9f82be) will increase coverage by 0.03%.
The diff coverage is 98.79%.

@@            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     
Impacted Files Coverage Δ
packages/datadog-instrumentations/src/mocha.js 98.09% <98.71%> (+3.80%) ⬆️
packages/datadog-plugin-mocha/src/index.js 98.03% <100.00%> (-0.47%) ⬇️

📣 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) {
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

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) {
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

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)
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

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')
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

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
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

this piece is so that the test function and hook functions are called with the correct context (we bind the asyncResource)

@juan-fernandez juan-fernandez marked this pull request as ready for review July 7, 2022 15:47
@juan-fernandez juan-fernandez requested a review from a team as a code owner July 7, 2022 15:47
@juan-fernandez juan-fernandez merged commit 437fc50 into master Jul 8, 2022
@juan-fernandez juan-fernandez deleted the juan-fernandez/fix-active-span branch July 8, 2022 09:20
smcroskey added a commit to smcroskey/dd-trace-js that referenced this pull request Dec 10, 2025
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
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.

Mocha integration: beforeAll and first beforeEach hooks are not traced

2 participants