I've known this for so long now, that I've sort of forgotten that it can nail new people in the ass.
The last one registered with the name wins - which means you have to be mindful about how you name things, or prefix/namespace them.
The other issue that can happen, is once something is defined as a dependency somehow in your app - every other module can use it, even if it was not an explicit dependency.
When you run your app, module b can access cFactory, even though you never defined it as a module dependency.
However, when writing my unit tests - I'll only do a beforeEach(module('b')) - in which case, the unit test would blow up and fail about not knowing about cFactoryProvider. Helps keep me honest about keeping my module dependencies in check and being mindful about where I'm getting things from, even if when running as an app - that sort of goes out the window.
Just out of curiosity, how did you find out about this? Because I can't find it explicitly stated in docs. I had to be kicked in the ass yesterday by the app itself to start digging and to discover that thing.
Regarding tests, the case you describe is good, but there is another one: in tests you use correct dependency because you load "only" modules you need (not all). Tests pass but in the app itself you may end up with completely different dependency being injected because something somewere loaded a module having service with the same name. And this is dangerous I think.
Was burned by it once in one of my first larger angular apps. I think I had something like
angular.module('entityTypeOne',[]).factory('reports', function {});
angular.module('entityTypeTwo',[]).factory('reports', function {});
and then got tripped up when I wasn't getting the reports that I expected. Thankfully it was the first case/time I ran into the issue - so was a simple change to be 'ok, entityTypeOneReports, entityTypeTwoReports'
You are right though about my testing approach - it does run the risk of - if someone registered two things with the same name, the unit tests would pass but the in-app behavior may be different.
I could do a 'just load the main module' for all of my tests - which I used to do before I know better, but then you run into the issue of all of the initialization code on every module (in the config/run block) firing off - and ended up in an endless rabbit chase of 'mocking a million things that had nothing to do with my test to get the test to pass'.
It's the lesser of two evils - and I've generally worked on 'single-team' projects, so there has been minimal risk of someone introducing a duplicate name for something and it can be easily avoided. But, by avoiding generic names for things can go along way to avoiding this from being an issue.
If you are using the annotation style syntax (which I'm not a fan of, I'd rather let ngAnnotate deal with that for me), I've seen people do things like
I had this happen when we had developed a module that had a directive in it for rendering list items specific to that module. Then, in another module, we needed another directive to render list items in a format required by that module. The directives just happened to both get named something way too generic, like "ListItem" or something. This was months apart.
We had a hard time figuring out why the list in one module wasn't rendering properly when the directive looked fine. That was when I discovered the naming overlap. I told our contractor to prefix module-specific controllers/services/directives with the module-code from then on.
13
u/e82 May 22 '15
I've known this for so long now, that I've sort of forgotten that it can nail new people in the ass.
The last one registered with the name wins - which means you have to be mindful about how you name things, or prefix/namespace them.
The other issue that can happen, is once something is defined as a dependency somehow in your app - every other module can use it, even if it was not an explicit dependency.
When you run your app, module b can access cFactory, even though you never defined it as a module dependency.
However, when writing my unit tests - I'll only do a beforeEach(module('b')) - in which case, the unit test would blow up and fail about not knowing about cFactoryProvider. Helps keep me honest about keeping my module dependencies in check and being mindful about where I'm getting things from, even if when running as an app - that sort of goes out the window.