Constructors

Spies, Mocks, and Fixtures

Constructors

Spies, Mocks, and Fixtures

  • Intercept calls to a method to track the arguments, call site, and return/throw status, and replace the implementation.

    By default, the method is set to a no-op. To retain the method behavior, pass the current value of the method as the third argument. For example:

    const results = t.capture(obj, 'foo', obj.foo)
    

    Automatically restores at t.teardown() if the @tapjs/after plugin is not disabled. Otherwise, it is important to call the restore() method on the returned function when you are done capturing.

    Type Parameters

    • T extends {}
    • M extends string | number | symbol

    Parameters

    • obj: T
    • method: M
    • impl: ((this: T, ...a: any[]) => any) = ...
        • (this, ...a): any
        • Parameters

          • this: T
          • Rest...a: any[]

          Returns any

    Returns CaptureResultsMethod<T, M, T[M]>

  • Intercept and track object property sets and gets.

    If a PropertyDescriptor is set, then it will be used as the replacement value. Otherwise, the original descriptor will be used.

    If the strictMode param is set, then attempts to write to read-only properties will throw an error.

    Type Parameters

    • T extends object

    Parameters

    • obj: T
    • prop: keyof T
    • Optionaldesc: PropertyDescriptor
    • strictMode: boolean = true

    Returns InterceptResultsMethod