Mmethod
Bun

method

test.default.MockTracker.method

method<MockedObject extends object, MethodName extends string | number | symbol>(
object: MockedObject,
methodName: MethodName,
): MockedObject[MethodName] extends Function ? Mock<any[any]> : never;

This function is used to create a mock on an existing object method. The following example demonstrates how a mock is created on an existing object method.

test('spies on an object method', (t) => {
  const number = {
    value: 5,
    subtract(a) {
      return this.value - a;
    },
  };

  t.mock.method(number, 'subtract');
  assert.strictEqual(number.subtract.mock.calls.length, 0);
  assert.strictEqual(number.subtract(3), 2);
  assert.strictEqual(number.subtract.mock.calls.length, 1);

  const call = number.subtract.mock.calls[0];

  assert.deepStrictEqual(call.arguments, [3]);
  assert.strictEqual(call.result, 2);
  assert.strictEqual(call.error, undefined);
  assert.strictEqual(call.target, undefined);
  assert.strictEqual(call.this, number);
});
@param object

The object whose method is being mocked.

@param methodName

The identifier of the method on object to mock. If object[methodName] is not a function, an error is thrown.

@param options

Optional configuration options for the mock method.

@returns

The mocked method. The mocked method contains a special mock property, which is an instance of MockFunctionContext, and can be used for inspecting and changing the behavior of the mocked method.

method<MockedObject extends object, MethodName extends string | number | symbol, Implementation extends Function>(
object: MockedObject,
methodName: MethodName,
implementation: Implementation,
): MockedObject[MethodName] extends Function ? Mock<Implementation | any[any]> : never;
method<MockedObject extends object>(
object: MockedObject,
methodName: keyof MockedObject,
): Mock<Function>;
method<MockedObject extends object>(
object: MockedObject,
methodName: keyof MockedObject,
implementation: Function,
): Mock<Function>;