Skip to content

feat(browserstack-service): test-level custom metadata hook handling (beforeEach/afterEach) for WDIO+Mocha#60

Open
anish353 wants to merge 1 commit into
mainfrom
test_level_custom_metadata
Open

feat(browserstack-service): test-level custom metadata hook handling (beforeEach/afterEach) for WDIO+Mocha#60
anish353 wants to merge 1 commit into
mainfrom
test_level_custom_metadata

Conversation

@anish353

Copy link
Copy Markdown
Collaborator

What is this about?

Adds correct beforeEach / afterEach hook-timing handling for the browser.setCustomTags feature already on main, for the WDIO + Mocha framework.

WDIO's Mocha runner fires user hooks outside the SDK's per-test tracking span: beforeEach runs before beforeTest tracks the test instance, and afterEach runs after afterTest finishes it. As a result, custom tags set inside those hooks previously either attached to the wrong test or were dropped from the payload. This change makes hook-set tags land on the intended test:

  • Buffer + flush for beforeEach — tags set before a per-test context exists are buffered and flushed into the test at test start, via a new TEST/PRE observer in CustomTagsModule (mints an onBeforeTest flush). Parity with Java @Before.
  • Hook-window routing — the service layer records the open Mocha hook window (beforeHook / afterHook) so CustomTagsModule can route a setCustomTags call to the right test (the CLI framework never sees Mocha's beforeEach / afterEach).
  • Defer TestRunFinished past afterEach — the TEST/POST send is deferred past the after-each hook window so tags set in afterEach still make the payload (serialized from the instance's live data at send time). Flushed at the next test's boundary, or from service.after() for the worker's last test.

This ports webdriverio PR anish353/webdriverio#3 (commit 5f377f114) into the migrated standalone repo, rewriting the packages/wdio-browserstack-service/packages/browserstack-service/ path prefix and reconciling against pre-existing drift in testHubModule.ts (LOG_REPORT/deferred re-entrancy) and service.ts (CLI-flow early-return in beforeHook; the mocha-window-set is placed before the CLI early-return so it runs in CLI mode). customTags.ts and customTagsModule.ts applied verbatim. It is standalone and independent of the title-based-TC work — no title-derived tagging symbols are introduced.

Verification (live)

Built and injected @wdio/browserstack-service into a real WDIO + Mocha sample and ran a hook spec (beforeEach / test-body / afterEach all calling setCustomTags).

  • Automate build: https://automation.browserstack.com/builds/tdznh5vcunet2lhd9vprsdohksuumpy1yuzrfwpc — both tests PASSED.
  • Test 1's TestRunFinished custom_metadata (from sdk-cli.log, build tdznh5vc…) shows the expected union across all three hook windows:
    • testTagBefore = ["Browserstack","Before"] (set in beforeEach → buffered + flushed at test start)
    • testTagInTest = ["Browserstack"] (set in test body)
    • testTagAfter = ["Browserstack","After","BrowserstackAfterChanged"] (in-test value union'd with the afterEach value — proves the after-each deferral)
  • Test 2 (worker's last test, flushed from service.after()) carries only its own testTagInTest2 = ["SecondTest"] with no cross-test leakage — proving both flush paths (next-test boundary and worker end) and correct per-test routing.

npm run build — 0 TypeScript errors. npm run lint — passes.

Related Jira task/s

SDK-6843 (test-level custom metadata hook handling for WDIO + Mocha setCustomTags).

Release (mandatory for every PR — required for the ready-for-review label)

Version bump: (required — tick exactly one)

  • minor (backwards-compatible feature)
  • patch (bug fix or other small change)

Release notes type: (optional)

  • New Feature
  • Bug Fix
  • Other Improvement

Release notes (customer-facing): (optional but encouraged)

  • Custom tags (browser.setCustomTags) set inside Mocha beforeEach / afterEach hooks now reliably land on the intended test's custom metadata, unioning and deduping with tags set in the test body (parity with Java @Before / @After).

Release notes (internal): (required — engineer-facing; what actually changed / why)

  • WDIO's Mocha runner fires user hooks outside the SDK's per-test tracking span, so hook-set custom tags landed on the wrong test or were dropped. Fixed by: (a) buffering pre-test tags in CustomTagsModule and flushing them via a new TEST/PRE onBeforeTest observer; (b) recording the open Mocha hook window in service.beforeHook/afterHook (via customTags.ts classifyMochaHookTitle / setCurrentMochaHookWindow) so setCustomTags routes to the correct test; (c) deferring the TEST/POST (TestRunFinished) send in TestHubModule past the after-each window (pendingTestFinish + flushPendingTestFinishEvent, stateOverride TEST/POST), flushed at the next test's INIT_TEST boundary or from service.after() for the last test.
  • Port of webdriverio PR anish353/webdriverio#3; reconciled against pre-existing drift in testHubModule.ts and service.ts on the standalone main. Changeset added (minor).

Checklist

  • Ready to review
  • Has it been tested locally?

PR Validations

Run Tests: Comment RUN_TESTS to trigger sanity tests.

…(beforeEach/afterEach) for WDIO+Mocha

Adds correct beforeEach/afterEach hook-timing handling for the setCustomTags
feature already on main. WDIO's Mocha runner fires user hooks OUTSIDE the SDK's
per-test tracking span: beforeEach runs before beforeTest tracks the test
instance, and afterEach runs after afterTest finishes it — so tags set in those
hooks previously either landed on the wrong test or missed the payload.

- Buffer tags set before a per-test context exists (e.g. from beforeEach) and
  flush them into the test at test start (a new TEST/PRE observer in
  CustomTagsModule minting an onBeforeTest flush).
- The service layer records the open Mocha hook window (beforeHook/afterHook) so
  CustomTagsModule can route a setCustomTags call to the right test.
- Defer the TestRunFinished (TEST/POST) send past the after-each hook window so
  tags set in afterEach still make the payload; flushed at the next test's
  boundary, or from service.after() for the worker's last test.

Ports webdriverio PR anish353/webdriverio#3 into the migrated standalone repo,
reconciling against pre-existing drift in testHubModule.ts and service.ts.
Standalone on main, independent of the title-based-TC work.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@anish353
anish353 requested a review from a team as a code owner July 17, 2026 12:05
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.

1 participant