Sometimes, we want to use Jasmine to spy on a function without an object.
In this article, we’ll look at how to use Jasmine to spy on a function without an object.
How to use Jasmine to spy on a function without an object?
To use Jasmine to spy on a function without an object, we can use the spyOn function.
For instance, we write
import * as FooFunctions from "../foo_functions";
x = FooFunctions.foo(y);
to call foo in FooFunctions in the test file.
Then in the test, we write
spyOn(FooFunctions, "foo").and.callFake(() => {
//...
});
// ...
expect(FooFunctions.foo).toHaveBeenCalled();
to call spyOn with the FooFunctions module and the 'foo' function name string.
And we call callFake with a function that we mock foo with.
And then we call expect to check if foo is called with toHaveBeenCalled.
Conclusion
To use Jasmine to spy on a function without an object, we can use the spyOn function.