spy-proxy
TypeScript icon, indicating that this package has built-in type declarations

1.0.0-alpha.1.0.8 • Public • Published

spy-proxy#

Object proxy helper to enable tracing of all function calls:

On a call to any function of the proxied object instance, the following is traced:

  • The name of the function.
  • How many times each function has been called.
  • The arguments passed to the function.
  • The return value of the function.

This is very helpful for asserting function calls in unit tests.

Example:

import {SpyProxy} from "../dist/spy-proxy";
 
let mock = SpyProxy.create({
    getGreeting(name) {
        return `Hello ${name}`;
    }
}, "myMock");
 
let result = mock.getGreeting("John");
 
// Assert that the method was called (any number of times, any arguments, any return value).
mock.assertWasCalled(a => a.getGreeting);
 
// Assert that the method was called 1 time (regardless of arguments, regardless of return value).
mock.assertWasCalled(a => a.getGreeting, 1);
 
// Assert that the method was called 1 time with one argument with value "John" (regardless of return value). 
mock.assertWasCalled(a => a.getGreeting, 1, ["John"]);
 
// Assert that the method was called 1 time with one argument with value "John" and that the returned value was "Hello John". 
mock.assertWasCalled(a => a.getGreeting, 1, ["John"], "Hello John");

Dependencies (0)

    Dev Dependencies (0)

      Package Sidebar

      Install

      npm i spy-proxy

      Weekly Downloads

      0

      Version

      1.0.0-alpha.1.0.8

      License

      ISC

      Last publish

      Collaborators

      • josundt