Function Mockable

  • Parameters

    • Optional options: MockableOptions

      optional configuration (snapshot directory, static wrapping)

    Returns (<T>(Base) => T)

      • <T>(Base): T
      • Type Parameters

        • T extends (new (...args) => object)

        Parameters

        • Base: T

        Returns T

    Description

    Class decorator that enables snapshot-based mocking.

    In record mode, every method call is forwarded to the original implementation and its arguments + result are persisted into a snapshot file.

    In replay mode, method calls return the previously recorded response from the snapshot without invoking the original logic.

    Features:

    • Wraps prototype methods and getters recursively up to Object.prototype.
    • Callback arguments are intercepted and their invocations are recorded/replayed.
    • Results with methods are wrapped in MockHandler so subsequent calls on them are also recorded/replayed.
    • Supports static method wrapping via MockableOptions.statics.

    Example

    @Mockable()
    class MockedService extends RealService {}