Implementation:Webdriverio Webdriverio BStackCleanup Class
| Knowledge Sources | |
|---|---|
| Domains | BrowserStack, Process_Management |
| Last Updated | 2026-02-12 00:00 GMT |
Overview
The BStackCleanup class manages cleanup operations for the BrowserStack service after test execution, handling observability build stopping, funnel data submission, and performance data upload.
Description
BStackCleanup is a static utility class that runs as a separate Node.js process via the exit handler mechanism. It parses command-line arguments (--observability, --funnelData, --performanceData) to determine which cleanup operations to execute. The startCleanup method orchestrates the flow: reading funnel data from a temporary file, executing observability cleanup (stopping the build via stopBuildUpstream), updating the funnel data with stop status, sending the funnel data to BrowserStack analytics, and uploading performance event data. This ensures that even if the main process exits unexpectedly, critical build-stop and analytics events are still dispatched.
Usage
This class is invoked automatically by the exit handler when the main process terminates. The file self-invokes BStackCleanup.startCleanup() at the bottom of the module, so it runs immediately when the script is executed as a child process.
Code Reference
Source Location
- Repository: Webdriverio_Webdriverio
- File: packages/wdio-browserstack-service/src/cleanup.ts
- Lines: 10-106
Signature
export default class BStackCleanup {
static async startCleanup(): Promise<void>
static async executeObservabilityCleanup(funnelData: FunnelData): Promise<void>
static updateO11yStopData(funnelData: FunnelData, status: string, error?: unknown): void
static async sendFunnelData(funnelData: FunnelData): Promise<void>
static getFunnelDataFromFile(filePath: string): FunnelData | null
static removeFunnelDataFile(filePath?: string): void
}
Import
import BStackCleanup from './cleanup.js'
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| process.argv | string[] |
Yes | Command-line arguments: --observability, --funnelData <path>, --performanceData
|
| funnelData file | JSON file |
No | Temporary file containing serialized FunnelData object
|
| process.env.BROWSERSTACK_TESTHUB_JWT | string |
No | JWT token required for observability build stop API call |
| process.env.BROWSERSTACK_TESTHUB_UUID | string |
No | Build UUID for constructing the build report URL |
Outputs
| Name | Type | Description |
|---|---|---|
| Build stop API call | HTTP PUT | Stops the observability build via stopBuildUpstream
|
| Funnel data event | HTTP POST | Sends funnel instrumentation data to BrowserStack analytics endpoint |
| Performance data upload | HTTP POST | Uploads performance events data via PerformanceTester
|
| Console output | string |
Prints the build report URL to the console |
Usage Examples
Exit handler spawning cleanup process
// In exitHandler.ts, when the process is exiting
import { fork } from 'node:child_process'
const cleanupArgs = ['--observability', '--funnelData', funnelDataFilePath]
const child = fork('./cleanup.js', cleanupArgs, { detached: true })
child.unref()
Self-invocation at module load
// At the bottom of cleanup.ts
void BStackCleanup.startCleanup()