Skip to content

MockTracker

[<Interface>]
type MockTracker =
    abstract member timers: MockTimers
    abstract member ``module`` (specifier: U2<string,URL>, ?options: MockModuleOptions) : MockModuleContext
    abstract member fn<'F, 'Implementation> (?original: 'F, ?implementation: 'Implementation, ?options: MockFunctionOptions) : U2<Intersect<'F,Result<'F,'Implementation>>,Intersect<'Implementation,Result<'F,'Implementation>>>
    abstract member fn<'F> (?original: 'F, ?options: MockFunctionOptions) : Intersect<'F,Members<'F>>
    abstract member getter<'MockedObject, 'Implementation, 'R> (object: 'MockedObject, methodName: typekeyof<'MockedObject,'R>, ?implementation: 'Implementation, ?options: MockFunctionOptions) : U2<Intersect<'Implementation,Result2<'Implementation>>,Result3<'Implementation>>
    abstract member getter<'MockedObject, 'R> (object: 'MockedObject, methodName: typekeyof<'MockedObject,'R>, ?options: MockFunctionOptions) : Result
    abstract member method<'MockedObject> (object: 'MockedObject, methodName: keyof<'MockedObject>, implementation: Function, options: MockMethodOptions) : Result
    abstract member method<'MockedObject> (object: 'MockedObject, methodName: keyof<'MockedObject>, options: MockMethodOptions) : Result
    abstract member method<'MockedObject, 'MethodName, 'Implementation> (object: 'MockedObject, methodName: 'MethodName, implementation: 'Implementation, ?options: MockFunctionOptions) : obj
    abstract member method<'MockedObject, 'MethodName> (object: 'MockedObject, methodName: 'MethodName, ?options: MockFunctionOptions) : obj
    abstract member property<'MockedObject, 'R> (object: 'MockedObject, property: typekeyof<'MockedObject,'R>, ?value: 'R) : Intersect<'MockedObject,Result>
    abstract member reset () : unit
    abstract member restoreAll () : unit
    abstract member setter<'MockedObject, 'Implementation, 'R> (object: 'MockedObject, methodName: typekeyof<'MockedObject,'R>, ?implementation: 'Implementation, ?options: MockFunctionOptions) : U2<Intersect<'Implementation,Result2<'Implementation>>,Result3<'Implementation>>
    abstract member setter<'MockedObject, 'R> (object: 'MockedObject, methodName: typekeyof<'MockedObject,'R>, ?options: MockFunctionOptions) : Result
AssemblyXantham.Fable.Node

The MockTracker class is used to manage mocking functionality. The test runner module provides a top level mock export which is a MockTracker instance. Each test also provides its own MockTracker instance via the test context's mock property.

@since v19.1.0, v18.13.0

Declared inside

Fn
Getter
Method
Property
Setter

Properties

abstract member timers: MockTimers

Methods

abstract member ``module`` (specifier: U2<string,URL>, ?options: MockModuleOptions) : MockModuleContext

This function is used to mock the exports of ECMAScript modules, CommonJS modules, and Node.js builtin modules. Any references to the original module prior to mocking are not impacted.

Only available through the --experimental-test-module-mocks flag.

@since v22.3.0

Parameter
specifierA string identifying the module to mock.
optionsOptional configuration options for the mock module.
abstract member fn<'F, 'Implementation> (?original: 'F, ?implementation: 'Implementation, ?options: MockFunctionOptions) : U2<Intersect<'F,Result<'F,'Implementation>>,Intersect<'Implementation,Result<'F,'Implementation>>>

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);
});

@since v19.1.0, v18.13.0

Parameter
originalAn optional function to create a mock on.
implementationAn optional function used as the mock implementation for original. This is useful for creating mocks that exhibit one behavior for a specified number of calls and then restore the behavior of original.
optionsOptional configuration options for the mock function.

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

abstract member fn<'F> (?original: 'F, ?options: MockFunctionOptions) : Intersect<'F,Members<'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);
});

@since v19.1.0, v18.13.0

Parameter
originalAn optional function to create a mock on.
implementationAn optional function used as the mock implementation for original. This is useful for creating mocks that exhibit one behavior for a specified number of calls and then restore the behavior of original.
optionsOptional configuration options for the mock function.

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

abstract member getter<'MockedObject, 'Implementation, 'R> (object: 'MockedObject, methodName: typekeyof<'MockedObject,'R>, ?implementation: 'Implementation, ?options: MockFunctionOptions) : U2<Intersect<'Implementation,Result2<'Implementation>>,Result3<'Implementation>>

This function is syntax sugar for MockTracker.method with options.getter set to true.

@since v19.3.0, v18.13.0

abstract member getter<'MockedObject, 'R> (object: 'MockedObject, methodName: typekeyof<'MockedObject,'R>, ?options: MockFunctionOptions) : Result

This function is syntax sugar for MockTracker.method with options.getter set to true.

@since v19.3.0, v18.13.0

abstract member method<'MockedObject> (object: 'MockedObject, methodName: keyof<'MockedObject>, implementation: Function, options: MockMethodOptions) : Result

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);
});

@since v19.1.0, v18.13.0

Parameter
objectThe object whose method is being mocked.
methodNameThe identifier of the method on object to mock. If object[methodName] is not a function, an error is thrown.
implementationAn optional function used as the mock implementation for object[methodName].
optionsOptional configuration options for the mock method.

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

abstract member method<'MockedObject> (object: 'MockedObject, methodName: keyof<'MockedObject>, options: MockMethodOptions) : Result

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);
});

@since v19.1.0, v18.13.0

Parameter
objectThe object whose method is being mocked.
methodNameThe identifier of the method on object to mock. If object[methodName] is not a function, an error is thrown.
implementationAn optional function used as the mock implementation for object[methodName].
optionsOptional configuration options for the mock method.

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

abstract member method<'MockedObject, 'MethodName, 'Implementation> (object: 'MockedObject, methodName: 'MethodName, implementation: 'Implementation, ?options: MockFunctionOptions) : obj

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);
});

@since v19.1.0, v18.13.0

Parameter
objectThe object whose method is being mocked.
methodNameThe identifier of the method on object to mock. If object[methodName] is not a function, an error is thrown.
implementationAn optional function used as the mock implementation for object[methodName].
optionsOptional configuration options for the mock method.

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

abstract member method<'MockedObject, 'MethodName> (object: 'MockedObject, methodName: 'MethodName, ?options: MockFunctionOptions) : obj

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);
});

@since v19.1.0, v18.13.0

Parameter
objectThe object whose method is being mocked.
methodNameThe identifier of the method on object to mock. If object[methodName] is not a function, an error is thrown.
implementationAn optional function used as the mock implementation for object[methodName].
optionsOptional configuration options for the mock method.

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

abstract member property<'MockedObject, 'R> (object: 'MockedObject, property: typekeyof<'MockedObject,'R>, ?value: 'R) : Intersect<'MockedObject,Result>

Creates a mock for a property value on an object. This allows you to track and control access to a specific property, including how many times it is read (getter) or written (setter), and to restore the original value after mocking.

test('mocks a property value', (t) => {
const obj = { foo: 42 };
const prop = t.mock.property(obj, 'foo', 100);

assert.strictEqual(obj.foo, 100);
assert.strictEqual(prop.mock.accessCount(), 1);
assert.strictEqual(prop.mock.accesses[0].type, 'get');
assert.strictEqual(prop.mock.accesses[0].value, 100);

obj.foo = 200;
assert.strictEqual(prop.mock.accessCount(), 2);
assert.strictEqual(prop.mock.accesses[1].type, 'set');
assert.strictEqual(prop.mock.accesses[1].value, 200);

prop.mock.restore();
assert.strictEqual(obj.foo, 42);
});

@since v22.20.0

Parameter
objectThe object whose value is being mocked.
propertyNameThe identifier of the property on object to mock.
valueAn optional value used as the mock value for object[propertyName]. Default: The original property value.

Returns. A proxy to the mocked object. The mocked object contains a special mock property, which is an instance of MockPropertyContext, and can be used for inspecting and changing the behavior of the mocked property.

abstract member reset () : unit

This function restores the default behavior of all mocks that were previously created by this MockTracker and disassociates the mocks from the MockTracker instance. Once disassociated, the mocks can still be used, but the MockTracker instance can no longer be used to reset their behavior or otherwise interact with them.

After each test completes, this function is called on the test context's MockTracker. If the global MockTracker is used extensively, calling this function manually is recommended.

@since v19.1.0, v18.13.0

abstract member restoreAll () : unit

This function restores the default behavior of all mocks that were previously created by this MockTracker. Unlike mock.reset(), mock.restoreAll() does not disassociate the mocks from the MockTracker instance.

@since v19.1.0, v18.13.0

abstract member setter<'MockedObject, 'Implementation, 'R> (object: 'MockedObject, methodName: typekeyof<'MockedObject,'R>, ?implementation: 'Implementation, ?options: MockFunctionOptions) : U2<Intersect<'Implementation,Result2<'Implementation>>,Result3<'Implementation>>

This function is syntax sugar for MockTracker.method with options.setter set to true.

@since v19.3.0, v18.13.0

abstract member setter<'MockedObject, 'R> (object: 'MockedObject, methodName: typekeyof<'MockedObject,'R>, ?options: MockFunctionOptions) : Result

This function is syntax sugar for MockTracker.method with options.setter set to true.

@since v19.3.0, v18.13.0