API reference › @evolu/common › Test › testStubGlobal
function testStubGlobal(key: PropertyKey, value: unknown): Disposable;
Defined in: packages/common/src/Test.ts:44
Temporarily replaces a global property for a test scope.
Node.js can mock existing globals with t.mock.property, but it rejects a
property that does not exist. That catches misspelled mock targets, but it
cannot model optional platform globals whose presence varies. Use this helper
to provide such a global, for example scheduler, or replace a global with
undefined to exercise a fallback.
The original property descriptor is restored on disposal. A property that did not exist is deleted. Prefer dependency injection when available because global properties are shared by concurrently running tests.
Example
import { assertEqual, assertFalse, testStubGlobal } from "@evolu/common";
const key = Symbol("test global");
{
using _stub = testStubGlobal(key, 42);
assertEqual(Reflect.get(globalThis, key), 42);
}
assertFalse(Reflect.has(globalThis, key));