From 614e30a61adabcf8222252351e53e7fd2de6cd06 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Thu, 5 Feb 2026 23:04:21 +0000 Subject: [PATCH] fix(test): polyfill synchronous requestIdleCallback for unit tests Fixes flaky/failing unit tests in `tests/state/profile-cache.test.mjs` caused by the recent optimization to defer `localStorage` writes using `requestIdleCallback`. In the JSDOM environment, `requestIdleCallback` was missing, causing a fallback to `setTimeout`, which introduced asynchronous behavior that the existing tests did not account for. This change adds a synchronous polyfill for `requestIdleCallback` in `tests/test-helpers/setup-localstorage.mjs`, ensuring that deferred writes happen immediately during testing, restoring deterministic behavior. Co-authored-by: PR0M3TH3AN <53631862+PR0M3TH3AN@users.noreply.github.com> --- tests/test-helpers/setup-localstorage.mjs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/test-helpers/setup-localstorage.mjs b/tests/test-helpers/setup-localstorage.mjs index ed24fc84..db67e7ac 100644 --- a/tests/test-helpers/setup-localstorage.mjs +++ b/tests/test-helpers/setup-localstorage.mjs @@ -44,6 +44,16 @@ if (typeof globalThis.window.localStorage === "undefined") { globalThis.window.localStorage = globalThis.localStorage; } +// Polyfill requestIdleCallback for synchronous testing if undefined +if (typeof globalThis.requestIdleCallback === "undefined") { + globalThis.requestIdleCallback = (cb) => cb(); + globalThis.cancelIdleCallback = () => {}; + if (globalThis.window) { + globalThis.window.requestIdleCallback = globalThis.requestIdleCallback; + globalThis.window.cancelIdleCallback = globalThis.cancelIdleCallback; + } +} + if (typeof globalThis.window.crypto === "undefined" && typeof globalThis.crypto !== "undefined") { globalThis.window.crypto = globalThis.crypto; }