On this page

C

MockFunctionContext

History

The MockFunctionContext class is used to inspect or manipulate the behavior of mocks created via the MockTracker APIs.

P

ctx.calls

History
Type:Array

A getter that returns a copy of the internal array used to track calls to the mock. Each entry in the array is an object with the following properties.

Attributes
arguments:Array
An array of the arguments passed to the mock function.
error?:any
If the mocked function threw then this property contains the thrown value. Default: undefined.
result:any
The value returned by the mocked function.
stack:Error
An Error object whose stack can be used to determine the callsite of the mocked function invocation.
If the mocked function is a constructor, this field contains the class being constructed. Otherwise this will be undefined.
this:any
The mocked function's this value.
M

ctx.callCount

History
ctx.callCount(): integer
Returns:integer
The number of times that this mock has been invoked.

This function returns the number of times that this mock has been invoked. This function is more efficient than checking ctx.calls.length because ctx.calls is a getter that creates a copy of the internal call tracking array.

M

ctx.mockImplementation

History
ctx.mockImplementation(implementation): void
Attributes
implementation:Function | AsyncFunction
The function to be used as the mock's new implementation.

This function is used to change the behavior of an existing mock.

The following example creates a mock function using t.mock.fn(), calls the mock function, and then changes the mock implementation to a different function.

test('changes a mock behavior', (t) => {
  let cnt = 0;

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

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

  const fn = t.mock.fn(addOne);

  assert.strictEqual(fn(), 1);
  fn.mock.mockImplementation(addTwo);
  assert.strictEqual(fn(), 3);
  assert.strictEqual(fn(), 5);
});
M

ctx.mockImplementationOnce

History
ctx.mockImplementationOnce(implementation, onCall?): void
Attributes
implementation:Function | AsyncFunction
The function to be used as the mock's implementation for the invocation number specified by onCall.
onCall?:integer
The invocation number that will use implementation. If the specified invocation has already occurred then an exception is thrown. Default: The number of the next invocation.

This function is used to change the behavior of an existing mock for a single invocation. Once invocation onCall has occurred, the mock will revert to whatever behavior it would have used had mockImplementationOnce() not been called.

The following example creates a mock function using t.mock.fn(), calls the mock function, changes the mock implementation to a different function for the next invocation, and then resumes its previous behavior.

test('changes a mock behavior once', (t) => {
  let cnt = 0;

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

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

  const fn = t.mock.fn(addOne);

  assert.strictEqual(fn(), 1);
  fn.mock.mockImplementationOnce(addTwo);
  assert.strictEqual(fn(), 3);
  assert.strictEqual(fn(), 4);
});
M

ctx.resetCalls

History
ctx.resetCalls(): void

Resets the call history of the mock function.

M

ctx.restore

History
ctx.restore(): void

Resets the implementation of the mock function to its original behavior. The mock can still be used after calling this function.