I have a global variable defined in my main template, which I use to store information bits from the back end, such as the environment context path. I can't move that variable inside a service.
How can I expose that variable to Karma when I run the unit tests?
You either declare that global variable within your test file:
or add a Javascript file where it's defined to your
karma.conf.js
file:The first solution didn't work for me in Angular 2.1.x. It simply would not recognize the variable in my imported service. What I had to do was put my environment variable in my
karma-test-shim.js
file and removevar
so it would be globally available.Mine looks like this:
If you are coming from Angular 2+ the only way I found that works is to create the variable or object globally using
window
:Google Recapthca Loaded from a script:
In this example a call to the google Recaptcha will create a global variable called
grecaptcha
.In our component we decide to render a new Google Recaptcha. Of course since we do not declare
grecaptcha
we need to usedeclare var
to say hey I promise it is declared somewhere:Now that our function works we decide to run a test but of course we would like to mock our
grecaptcha
as we don't have control over it. So we name our global variable we would like to create and add the functions we would like:Update:
Creating the global variable in the
beforeEach
is fine but what about when it has some sort of callback function such as above that calls a function from your component? Easy enough we just move the login to our test and in our mock we set it to our components function:Note:
spyOn<any>
: The<any>
is used so we can refence without error the function because it is private, otherwise we will get a typescript error;