feat(browserstack-service): test-level custom metadata hook handling (beforeEach/afterEach) for WDIO+Mocha (SDK-6843)#3
Open
anish353 wants to merge 1 commit into
Open
Conversation
…(beforeEach/afterEach) for WDIO+Mocha (SDK-6843) 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. This change makes hook-set tags land on the intended test: - 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 that mints an onBeforeTest flush), parity with Java @before. - 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 the TestRunFinished (TEST/POST) send past the after-each hook window so tags set in `afterEach` still make the payload; the payload is 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. Standalone on main, independent of the title-based-TC PR (webdriverio#15378). When webdriverio#15378 merges it will need to reconcile with this change: both add an onBeforeTest method and a TEST/PRE observer to CustomTagsModule (this PR does the buffered-tag flush; webdriverio#15378 adds title-derived tagging) — they must be merged into one method and one observer registration rather than duplicated. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
13 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Proposed changes
Adds correct beforeEach / afterEach hook-timing handling for the
browser.setCustomTagstest-level custom-metadata feature that is already onmain, for the WDIO + Mocha framework.WDIO's Mocha runner fires user hooks outside the SDK's per-test tracking span:
beforeEachruns beforebeforeTesttracks the per-test instance, so asetCustomTagscall there had no test to attach to (it hit the "outside of a resolvable test context; ignoring" path and was dropped).afterEachruns afterafterTestfinishes the test and triggers theTestRunFinished(TEST/POST) send, so tags set inafterEachmissed the payload.This change makes hook-set tags land on the intended test — parity with the Java SDK's
@Before/@Afterbehavior:beforeEach) are buffered inCustomTagsModuleand flushed into the tracked test instance at test start, via a newonBeforeTesthandler registered as aTEST/PREobserver. Then the buffer is reset per test.beforeHook/afterHook) records which Mocha hook window is open (before_each/after_each/before_all/after_all) soCustomTagsModulecan route asetCustomTagscall to the correct test — the CLI framework never sees Mocha'sbeforeEach/afterEach.TestRunFinishedsend past the after-each window. For Mocha, the TEST/POST send is deferred so tags set inafterEachstill make the payload (the payload is serialized from the instance's live data map at send time). It is flushed at the next test's boundary, or fromservice.after()for the worker's last test.Scope note: This is a standalone change on
main, independent of the title-based Test-Case-ID PR (webdriverio#15378). When webdriverio#15378 merges it will need to reconcile with this change: both add anonBeforeTestmethod and aTEST/PREobserver toCustomTagsModule(this PR does the buffered-tag flush; webdriverio#15378 adds title-derived tagging). They should be merged into a single method + single observer registration rather than duplicated.Jira: SDK-6843
Types of changes
Checklist
Live-verified end-to-end against a real BrowserStack Automate + Test Observability session (WDIO + Mocha, Chrome, v9). A spec sets
testTagBeforeinbeforeEach,testTagInTest+testTagAfterin the test body, andtestTagAfteragain inafterEach, across two tests. Test 1'sTestRunFinishedcustom_metadatacame through as the correct union:This proves both the
beforeEachflush (testTagBeforelanded) and theafterEachdeferral (testTagAfterincludesBrowserstackAfterChanged). Test 2 correctly received its ownbeforeEachtag and itsafterEachtag was flushed at worker end viaservice.after().Backport Request
v9and doesn't need to be back-ported#XXXXXFurther comments
The buffer and hook-window tracker are process-local (one module per worker) and Mocha runs tests serially within a worker, so a plain object / module-level variable is safe. The deferred-finish design stashes the finished test's own instance object (INIT_TEST replaces the tracked slot for the next test), so it stays valid across tests, and the payload is read fresh at send time so late merges are included.
Reviewers: @webdriverio/project-committers