MockTracker

external class MockTracker(source)

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

Constructors

Link copied to clipboard
constructor()

Properties

Link copied to clipboard

Functions

Link copied to clipboard
fun <F : Function<*>> fn(original: F = definedExternally, options: MockFunctionOptions = definedExternally): Mock<F>

This function is used to create a mock function.

fun <F : Function<*>> fn(original: F = definedExternally, implementation: Function<*> = definedExternally, options: MockFunctionOptions = definedExternally): Mock<Function<*>>
Link copied to clipboard
fun getter(target: Any, methodName: String, options: MockFunctionOptions = definedExternally): Mock<() -> Any?>

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

fun getter(target: Any, methodName: String, implementation: Function<*> = definedExternally, options: MockFunctionOptions = definedExternally): Mock<Function<*>>
Link copied to clipboard
fun method(target: Any, methodName: String, options: MockFunctionOptions = definedExternally): Mock<Function<*>>

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.

fun method(target: Any, methodName: String, options: MockMethodOptions): Mock<Function<*>>
fun method(target: Any, methodName: String, implementation: Function<*>, options: MockFunctionOptions = definedExternally): Mock<Function<*>>
fun method(target: Any, methodName: String, implementation: Function<*>, options: MockMethodOptions): Mock<Function<*>>
Link copied to clipboard
fun module(specifier: String, options: MockModuleOptions = definedExternally): 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.

Link copied to clipboard
fun reset()

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.

Link copied to clipboard

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.

Link copied to clipboard
fun setter(target: Any, methodName: String, options: MockFunctionOptions = definedExternally): Mock<(value: Any?) -> Unit>

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

fun setter(target: Any, methodName: String, implementation: Function<*> = definedExternally, options: MockFunctionOptions = definedExternally): Mock<Function<*>>