Disclaimer
Putting the disclaimer in the beginning as it is a bad practice to check whether our code is running under the test and do actions based on it. Ideally it should be done via dependency injection and mock objects.
How to check code is running by Karma
The idea came from the below SO question.
Before going into the code let us take some time to understand Karma. It is a test runner. It runs the JavaScript application by starting browser instance(s). Those instances can be either with UI or headless. Headless means no UI or consider as in memory.
Lets understand the code
function isKarmaRunning(){ let isKarmaRunning = false; if (typeof window["__karma__"] !== "undefined") { isKarmaRunning = true; } console.log(`[bootstrap] isKarmaRunning - ${isKarmaRunning} ${typeof window["__karma__"]}`); // still undefined return isKarmaRunning; }
It is self explanatory and show the danger itself. It assumes when the code is run by Karma test runner, the window object will get a properly called __karma__. We can detect the presence of Karma by checking that property.
The danger is that in future when Karma decided to stop setting the __karma__ property or change its name, our code will fail.
Thanks to laukok for his question with solution.
One scenario to detect whether Karma is running
This is not a scenario where we must check for Karma. Rather its hack and you are free to suggest how to do it right.
The scenario is where AngularJS and PWA (Progressive Web Apps) are combined. PWA's ServiceWorker feature is used to intercept the web requests to populate cache or serve from cache instead of hitting server. The cache is filled to ensure that the application can be used even without internet connection.
The ServiceWorker has fetch API which is used to intercept the web requests. fetch API hook starts only after the service worker is properly initiated. It may take up to 100ms as per different sources. If AngularJS application is bootstraped before ServiceWorker comes into active mode, it will not catch the web requests hence not cached. The web requests include the requests from ng to get the html views
If the user is still online and use the application, there are chances that those web requests are again intercepted and stored into cache. But what if the user first launch the application and went to offline. The cache is not populated hence error.
In order to work the initialization delay one of the solutions is to explicitly bootstrap the Angular application once the ServierWorker has started. Then the problem is that the tests written will complain that the Angular application is not bootstraped. Then either we need to change all the tests to wait for the ServiceWorker to initialize or have the application changes to do below
If the application is running under Karma, boo Angular immediately. Else wait for the ServiceWorker to initialize.
Too complicated. Isn't it. As an software engineer we should have gone back to original problem and solved the issue by separating the components and directives to separate ng module or one of the other approaches.
Hopefully once there is enough time to analyze there will be a follow up post with better solution. In mean time feel free to pour your comments.
No comments:
Post a Comment