MockTracker

sealed external interface 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

Properties

Link copied to clipboard
abstract val timers: MockTimers

Functions

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

This function is used to create a mock function.

abstract fun <F : Function<*>> fn(original: F = definedExternally, implementation: Function<*> = definedExternally, options: MockFunctionOptions = definedExternally): Mock<Function<*>>
Link copied to clipboard
abstract 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.

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

abstract fun method(target: Any, methodName: String, options: MockMethodOptions): Mock<Function<*>>
abstract fun method(target: Any, methodName: String, implementation: Function<*>, options: MockFunctionOptions = definedExternally): Mock<Function<*>>
abstract fun method(target: Any, methodName: String, implementation: Function<*>, options: MockMethodOptions): Mock<Function<*>>
Link copied to clipboard
abstract fun module(specifier: String, options: MockModuleOptions = definedExternally): MockModuleContext

This function is used to mock the exports of ECMAScript modules, CommonJS modules, JSON modules, and Node.js builtin modules. Any references to the original module prior to mocking are not impacted. In order to enable module mocking, Node.js must be started with the --experimental-test-module-mocks command-line flag.

Link copied to clipboard
abstract fun property(target: Any, property: String, value: Any? = definedExternally): MockTrackerPropertyResult

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.

Link copied to clipboard
abstract 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
abstract fun restoreAll()

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
abstract 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.

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