spy-proxy
v1.0.0-alpha.1.0.8
Published
Proxy to trace and method calls to an object.
Downloads
3
Readme
#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");