Implementation:DevExpress Testcafe TestRunTracker
| Knowledge Sources | |
|---|---|
| Domains | Async_Context, Test_Execution |
| Last Updated | 2026-02-12 12:00 GMT |
Overview
Concrete utility that tracks the association between asynchronous execution contexts and their corresponding test runs, enabling TestCafe to resolve which test run is active when user code calls API methods.
Description
TestRunTracker uses Node.js async_hooks to track the relationship between async operations and test runs. When a test function executes, the tracker registers the current async context ID with the test run. Any subsequent async operations (callbacks, promises, timers) inherit this context. This allows methods like t.click() to automatically find the correct test run even when called from deeply nested async callbacks without explicit parameter passing.
Usage
This is a core infrastructure component that enables the t proxy to work. It runs automatically and is not called directly by test authors.
Code Reference
Source Location
- Repository: DevExpress_Testcafe
- File: src/api/test-run-tracker.ts
- Lines: 1-126
Signature
class TestRunTracker {
private _contextToTestRunMap: Map<number, TestRun>;
private _hook: AsyncHook;
public enabled: boolean;
public addTrackingMarkerToFunction (testRunId: string, fn: Function): Function;
public resolveContextTestRun (): TestRun | null;
public getContextTestRunId (): string | null;
public ensureEnabled (): void;
}
export default new TestRunTracker();
Import
import testRunTracker from '../api/test-run-tracker';
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| testRunId | string | Yes | ID of the test run to associate with the async context |
| fn | Function | Yes | Function to mark with the test run context |
Outputs
| Name | Type | Description |
|---|---|---|
| resolveContextTestRun() | TestRun or null | The test run associated with the current async context |
| Wrapped function | Function | Function with injected test run tracking |
Usage Examples
import testRunTracker from '../api/test-run-tracker';
// Internal: mark a test function with its test run context
const wrappedFn = testRunTracker.addTrackingMarkerToFunction(testRunId, userTestFn);
// Internal: resolve the current test run from any async context
const testRun = testRunTracker.resolveContextTestRun();
if (testRun)
testRun.executeCommand(command);