stubProperty

import { stubProperty } from "jsr:@ende/testing/unstable-stub-property";

function ยท experimental

function stubProperty<Self, Prop extends keyof Self>(self: Self, property: Prop, value: Self[Prop]): Disposable

Stubs a property on an object, retaining the attributes of the original property descriptor as far as possible.

self: Self

The object to stub the property on.

property: Prop

The property to stub.

value: Self[Prop]

The value to stub the property with.

Returns

A disposable that restores the original property when disposed.

Example: Usage

import { stubProperty } from "@ende/testing/unstable-stub-property";
import { assertEquals } from "@ende/assert";

const obj = { foo: "bar" };
{
 using stub = stubProperty(obj, "foo", "baz");
 assertEquals(obj.foo, "baz");
}
// After disposing, the property returns to its original value.
assertEquals(obj.foo, "bar");

Source