Skip to content

Commit 12481ec

Browse files
committed
fix(test-utils): Use epoch-based timestamps in event proxy server
The timestamp filtering used process.hrtime() which returns time relative to an arbitrary process-local reference point. Since the event proxy server and Playwright test runner are separate Node.js processes, their hrtime values were not comparable. This caused waitForTransaction() to sometimes match transactions from previous tests (stale events leaking from the buffer), leading to flaky test failures like the react-router-7-lazy-routes duration assertion. Fix: Use Date.now() (epoch milliseconds) plus a sub-millisecond component from performance.now() to get high-resolution timestamps that are comparable across processes.
1 parent d62b54e commit 12481ec

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

dev-packages/test-utils/src/event-proxy-server.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -410,10 +410,23 @@ async function retrieveCallbackServerPort(serverName: string): Promise<string> {
410410
}
411411

412412
/**
413-
* We do nanosecond checking because the waitFor* calls and the fetch requests may come very shortly after one another.
413+
* Get a high-resolution timestamp that is comparable across processes.
414+
*
415+
* We use Date.now() as the base (epoch milliseconds) plus a sub-millisecond
416+
* component from performance.now() to get microsecond-level precision.
417+
*
418+
* NOTE: We cannot use process.hrtime() because it returns time relative to an
419+
* arbitrary process-local reference point. Since the event proxy server and
420+
* Playwright test runner are separate processes, their hrtime values are not
421+
* comparable, which was causing stale events from previous tests to leak into
422+
* later tests.
414423
*/
415424
function getNanosecondTimestamp(): number {
416-
const NS_PER_SEC = 1e9;
417-
const [seconds, nanoseconds] = process.hrtime();
418-
return seconds * NS_PER_SEC + nanoseconds;
425+
// Date.now() gives us epoch milliseconds (comparable across processes)
426+
// performance.now() gives us sub-millisecond precision within this process
427+
// We use the fractional part of performance.now() to add microseconds
428+
const epochMs = Date.now();
429+
const subMs = performance.now() % 1; // Get just the fractional part (0.0 to 0.999...)
430+
// Convert to nanoseconds: epoch_ms * 1e6 + sub_ms * 1e6
431+
return epochMs * 1e6 + Math.floor(subMs * 1e6);
419432
}

0 commit comments

Comments
 (0)