Mavi Domates
Mavi Domates

Reputation: 4521

Minified $provider injection with jasmine and angular

We have a project (angular) and some unittests for it (jasmine+sinon), which when minified creates some issues. For the actual code, we've solved these problems by injecting using the staticly typed string array, e.g. ['locationService', 'etcService']. Unfortunately for the unittests, the minification has some more problems to solve. As an example:

module(function($provide){
    $provide.service('etc..',...);
}

Code above immediately becomes unusuable since the provider variable gets renamed to something like 'a'. I've tried to tweak it a bit wrapping the function with something like below:

function injectTest($provide){
    // do the same stuff
}
injectTest.$inject = ['$provide'];

which was a recommended solution in some other online posts. The problem is with modules this really doesn't work. I've tried both:

module(angular.injector().invoke(injectTest)); // which results in 'Unknown provider: $provideProvider <- $provide

and

module(injectTest); // which results in 'Unknown provider: nProvider <- n'

Is there any way to inject the $provider into a module without breaking on minification?

Upvotes: 0

Views: 150

Answers (1)

Walfrat
Walfrat

Reputation: 5353

Inline injection :

var myFN = ['$provide', function($provide){
   // do stuff
}]

Now if you want to bind a function to a 3rd party library where you need service let's say in my sample your function need the service CRUDService and receive a params objects from the 3rd party :

var myFN = ['CRUDService', function(CRUDService){
   // do some init stuff
   // you can either make it a singleton by sotrng the function and return the reference or either return new function on each call
   return function(params){
       // do stuff
   };
}] ;

// now to bind it to your 3rd party
objectFor3rdParty = {fn:$injector.invoke(myFN)};

I use only inline injection instead of $inject, matter of taste i guess.

Upvotes: 0

Related Questions