unit-testingjasminekarma-runnerangularjs-ngmock

Conditionally load ngMock into app only if running karma jasmine tests


I am having issues loading my app when including ngMock, so I would like to only load it when I am running my tests as this works fine. Is there a flag or some kind of isTesting() function that gets set when karma runs tests? Something I can then reference when creating the array of dependencies for my app.


Solution

  • I ended up just checking for the existence of a global variable which would only be set if the test classes had loaded, I suppose it's kind of obvious but I was kind of hoping for a karma.isRunning property or something...

    Anyway my solution looks kind of like:

    var dependencies = [
        'ui.router',
        'ngAnimate',
        'ngSanitize',
    ];
    if (typeof APP_TEST !== "undefined")
        dependencies.push("ngMock");
    
    angular.module('MyApp', dependencies)...
    

    Keep in mind that the test classes need to be loaded before the main app files