Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to deal with angular module's config function when unit testing?

When setting up a unit test suite for an angular application using Karma/Jasmine, is it recommended to include the js with the app module's config function in the test's files?

I've read that it is suggested to exclude this from testing, however that seems awkward because there's often critical setup that happens in the config function that would prevent the application from working.

What's the best practice around this? Create a mock config function that does the same thing in a 'mocked' manner?

I'm running across this issue myself but want to understand the broader strategy: How do unit test with angular-translate

like image 830
Marplesoft Avatar asked Oct 10 '13 04:10

Marplesoft


1 Answers

In my application, I ended up using the following solution:

Define an "appBase" module with all the config and run functions that I want to run when unit-testing and create another "app" module which declares "appBase" module as a dependency and includes all the config and run functions that I don't what to run when unit-testing. Then all my unit tests use the "appBase" module, while the final application uses the "app" module. In code:

angular.module('appBase', ['dependencies'])
       .config(function() {
            // This one will run when unit-testing. Can also set-up mock data
            // that will later be overridden by the "app" module
        });

angular.module('app', ['appBase'])
       .config(function() {
            // This function will only run in real app, not in unit-tests.
       });
like image 154
urish Avatar answered Sep 19 '22 05:09

urish