Stub
import { Stub } from "jsr:@ende/testing/unstable-stub";interface · experimental
interface Stub<Self = any, Args extends unknown[] = any[], Return = any> extends MethodSpy<Self, Args, Return>An instance method replacement that records all calls made to it.
Properties
fake
fake: (this: Self, ...args: Args) => ReturnThe function that is used instead of the original.
stub
import { stub } from "jsr:@ende/testing/unstable-stub";function · experimental
function stub<Self, Prop extends keyof Self>(self: Self, property: Prop): Stub<Self, GetParametersFromProp<Self, Prop>, GetReturnFromProp<Self, Prop>>Replaces an instance method with a Stub with empty implementation.
self: SelfThe instance to replace a method of.
property: PropThe property of the instance to replace.
Returns
The stub function which replaced the original.
Example: Usage
import { assertSpyCalls } from "@ende/testing/mock";
import { stub } from "@ende/testing/unstable-stub";
const obj = {
method() {
// some inconventient feature for testing
},
};
const methodStub = stub(obj, "method");
for (const _ of Array(5)) {
obj.method();
}
assertSpyCalls(methodStub, 5);
function · experimental
function stub<Self, Prop extends keyof Self>(self: Self, property: Prop, func: (this: Self, ...args: GetParametersFromProp<Self, Prop>) => GetReturnFromProp<Self, Prop>): Stub<Self, GetParametersFromProp<Self, Prop>, GetReturnFromProp<Self, Prop>>Replaces an instance method with a Stub with the given implementation.
self: SelfThe instance to replace a method of.
property: PropThe property of the instance to replace.
func: (this: Self, ...args: GetParametersFromProp<Self, Prop>) => GetReturnFromProp<Self, Prop>The fake implementation of the function.
Returns
The stub function which replaced the original.
Example: Usage
import { stub } from "@ende/testing/unstable-stub";
import { assertEquals } from "@ende/assert";
const obj = {
method(): number {
return Math.random();
},
};
const methodStub = stub(obj, "method", () => 0.5);
assertEquals(obj.method(), 0.5);
function · experimental
function stub<Self, Prop extends keyof Self>(self: Self, property: Prop, descriptor: Omit<PropertyDescriptor, "configurable">): Stub<Self, GetParametersFromProp<Self, Prop>, GetReturnFromProp<Self, Prop>> & { get: Spy<Self, GetParametersFromProp<Self, Prop>, GetReturnFromProp<Self, Prop>>; set: Spy<Self, GetParametersFromProp<Self, Prop>, GetReturnFromProp<Self, Prop>> }Replaces an instance property setter or getter with a Stub with the given implementation.
self: SelfThe instance to replace a method of.
property: PropThe property of the instance to replace.
descriptor: Omit<PropertyDescriptor, "configurable">The javascript property descriptor with fake implementation of the getter and setter.
Returns
The stub with get and set properties which are spys of the setter and getter.
Example: Usage
import { assertSpyCalls } from "@ende/testing/mock";
import { stub } from "@ende/testing/unstable-stub";
import { assertEquals } from "@ende/assert";
const obj = {
prop: "foo",
};
const getterStub = stub(obj, "prop", {
get: function () {
return "bar";
},
});
assertEquals(obj.prop, "bar");
assertSpyCalls(getterStub.get, 1);
function · experimental
function stub<Self, Args extends unknown[], Return>(self: Self, property: keyof Self, descriptorOrFunction?: ((this: Self, ...args: Args) => Return) | Omit<PropertyDescriptor, "configurable">): Stub<Self, Args, Return>