Currently, I am working on typescript code that compiles into javascript, gets bundled with rollup, and is utilized by a framework. This framework exposes a library to me in the global scope, taking the form of a function:
fun({
prop1: number,
...
});
I do not want to include this library in my bundled JavaScript as it is already provided by the framework. Having different versions of the library could potentially lead to unexpected bugs. In my opinion, it would be safer and more straightforward to use the library supplied by the framework.
After some research, I believe the declare
keyword serves this purpose. So, I added the following line in the same file where I call fun
:
export declare function fun(input: unknown): void;
Although my code now compiles successfully, I am facing challenges when it comes to unit testing. When attempting to test the code that calls fun
, Jasmine reports an error saying:
ReferenceError: fun is not defined
While I understand the reason for this error, I am unsure how to circumvent it. I tried using sinon to stub fun
, but encountered a similar issue during testing with Jasmine.
The solution I came up with involves adding an if
statement around the call to fun
in order to skip it during testing - a workaround that I'm not fond of.
I am seeking advice on how to properly stub fun
so that I can execute the code calling it and verify that the correct properties are being passed. Alternatively, is there a way to create a mock version of fun
that can be excluded from the transpiled javascript for testing purposes, yet does not exist in production? Due to module functionality, I am doubtful about this possibility, but I am open to exploring any suggestions.