Skip to content

Conversation

@smcroskey
Copy link

Description

This PR 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 #2167.

Problem

Currently, Jest's beforeAll and afterAll hooks are not properly instrumented with spans that are parented to the test suite span. This means that any work done in these suite-level hooks is not visible in the trace hierarchy, making it difficult to debug setup/teardown performance issues.

Solution

This implementation follows the same pattern used in the Mocha integration:

  • Differentiates between suite-level hooks (beforeAll/afterAll) and test-level hooks (beforeEach/afterEach)
  • Wraps suite-level hooks with suite context
  • Wraps test-level hooks with test context

Changes

Core Implementation

  • Added hook type detection: getHookType() and isSuiteLevelHook() functions to identify hook types
  • Created suite-level channel: suiteFnCh for suite hook execution
  • Modified hook wrapping logic: Uses appropriate context based on hook type
  • Stored suite context: Added suiteSpanContext to DatadogEnvironment and suiteContexts map
  • Updated plugin bindings: Added ci:jest:suite:fn channel handling

Testing

  • Added comprehensive test suite for hook instrumentation
  • Tests cover basic hooks, nested describe blocks, async hooks, and error scenarios

Related Issues

Fixes #5677

Testing

  • Syntax validation passes
  • Hook type detection works correctly
  • Suite context is properly stored and retrieved
  • Tests added for hook instrumentation
  • Compatible with Jest versions >=24.8.0

Notes

This is a draft PR for initial review. The implementation has been tested locally and follows the established patterns from the Mocha integration.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Include spans from beforeAll hook in jest test suite

1 participant