Skip to main content

method MockTracker.prototype.fn

Overload 1

#MockTracker.prototype.fn<F extends Function = NoOpFunction>(
original?: F,
): Mock<F>

This function is used to create a mock function.

The following example creates a mock function that increments a counter by one on each invocation. The times option is used to modify the mock behavior such that the first two invocations add two to the counter instead of one.

test('mocks a counting function', (t) => {
  let cnt = 0;

  function addOne() {
    cnt++;
    return cnt;
  }

  function addTwo() {
    cnt += 2;
    return cnt;
  }

  const fn = t.mock.fn(addOne, addTwo, { times: 2 });

  assert.strictEqual(fn(), 2);
  assert.strictEqual(fn(), 4);
  assert.strictEqual(fn(), 5);
  assert.strictEqual(fn(), 6);
});

Type Parameters #

#F extends Function = NoOpFunction

Parameters #

#original: F
optional

An optional function to create a mock on.

#options: MockFunctionOptions
optional

Optional configuration options for the mock function.

Return Type #

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

Overload 2

#MockTracker.prototype.fn<
F extends Function = NoOpFunction,
Implementation extends Function = F,
>
(
original?: F,
implementation?: Implementation,
): Mock<F | Implementation>

Type Parameters #

#F extends Function = NoOpFunction
#Implementation extends Function = F

Parameters #

#original: F
optional
#implementation: Implementation
optional
#options: MockFunctionOptions
optional

Return Type #

Did you find what you needed?

Privacy policy