This property contains CallsLog instance, with several helper methods, related to the calls tracking information. It is available as the property of the spy object and also assigned to the spies wrapper function.
Example:
const spy = t.spyOn(obj, 'someMethod').callThrough()
obj.someMethod(0, 1)
obj.someMethod(1, 2)
// available both on spy and wrapper
t.expect(spy.calls.any()).toBe(true)
t.expect(obj.someMethod.calls.count()).toBe(2)
t.expect(spy.calls.first()).toEqual({ object : obj, args : [ 0, 1 ], returnValue : undefined })
This is just a reference to itself, to add some syntax sugar.
This property is also assigned to the wrapper function of spy.
t.spyOn(obj, 'someMethod').callThrough()
// same thing as above
t.spyOn(obj, 'someMethod').and.callThrough()
// returns spy instance
obj.someMethod.and
This method makes the spy to call the provided function and return the value from it, instead of the original function.
The function to call instead of the original function
This method makes the spy to also execute the original function it has been installed over. The value returned from original function is returned from the spy.
This method applies its 1st argument (if any) to the current instance using Object.assign()
.
Supposed to be overridden in the subclasses to customize the instance creation process.
This method resets all calls tracking data. Spy will report as it has never been called yet.
This method makes the spy to return the value provided and not execute the original function.
The value that will be returned from the spy.
This method makes the spy to just return undefined
and not execute the original function.
This method makes the spy to throw the specified error
value (instead of calling the original function).
The error value to throw. If it is not an Error
instance, it will be passed to Error
constructor first.
This spy instance
This is a type-safe static constructor method, accepting a single argument. If that argument is already an instance of this class - it is returned right away, otherwise the new constructor is used for instantiation.
This is a type-safe static constructor method, accepting a single argument, with the object, corresponding to the class properties. It will generate a compilation error, if unknown property is provided.
For example:
class MyClass extends Base {
prop : string
}
const instance : MyClass = MyClass.new({ prop : 'prop', wrong : 11 })
will produce:
TS2345: Argument of type '{ prop: string; wrong: number; }' is not assignable to parameter of type 'Partial<MyClass>'.
Object literal may only specify known properties, and 'wrong' does not exist in type 'Partial<MyClass>'
The only thing this constructor does is create an instance and call the initialize method on it, forwarding the first argument. The customization of instance is supposed to be performed in that method.
Generated using TypeDoc
This class implements a "spy" - function wrapper which tracks its calls. Spy can be installed instead of a method in some object or can be used standalone.
Usually you don't use this class directly, but instead via the test helper methods: spyOn, createSpy, createSpyObj.
Spy can "operate" in several modes, called "strategies". See callThrough, callFake, stub, returnValue, throwError below. Default one is callThrough.
Note, that spies "belongs" to a test and once the test is completed all, spies that were installed during its execution will be removed.