Protractor keeps state of added mocked modules.
I have the following problem:
- first i write some test suites where i'm using mocked modules.
- next i write a test suite where i'm not adding any mocked modules and i rely on the real implementation. I just want to test whether a specific template is loaded on a click or not. What i see is that when all test suites are enabled my test without adding mocked module is not passing. When i disable all other test suites where i added mocked modules, everything is passing and working just fine. When all suites are included i noticed that when it comes to the test without mocked objects, the labels on the page is from a previous suite (where i have mocked the data in the module).
Is it something that i'm missing or doing wrong? If so, how can i fix it.
Thanks in advance.
Try calling browser.removeMockModule(<name>) after the tests where you want things mocked.
Well i must admit that what you have proposed worked just fine but my concern is that protractor is like a huge global namespace where you should take care what kind of state you leave because this might affect your different suites. It's just an opinion ;)
Thank you very much for the fast answer.
It's a valid point, and definitely something to think about. My use case for adding mock modules was more like something you'd want to setup and never edit - like turning off animations, or mocking out an email server. Maybe surfacing information about which modules are being mocked would be a good solution?
Well having that kind of information would be useful but still that will lead to the same problem. The job for removing mocked objects should not be to the developer but to protractor. I mean you cannot rely on the developer. Also it's time consuming - i must decide which modules affect my current suite and remove them. This brings the follow question - 'Well if i'm going to remove them, on the next run they will be added again so why should i keep them at first place?"
I might agree that once added you avoid duplication on adding them again somewhere else. But on other hand i write additional code to remove them which also might lead to duplication in some suites.
Well on second thought i agree with you to keep information about mocked modules and after each test or suite to destroy them but internally in protractor.
Well mocking modules is good feature it allows you to inject test data and assert against it. That way you avoid duplicating your data with the one in the production which avoids of course renaming one data property to affect your tests.
P.S. After all everything is about automation right? the less you manage your pre/post configurations, the better.
I find the idea of keeping a state reference of the current instance of protractor quite interesting. It could be implemented as a stash (like git stash) where its possible to save the current protractor state and later re-apply it or drop it.
a state could contain the active mock modules, current url etc.
this is what i came up with so far:
Protractor.prototype.stash = function (action, success) {
var states = [];
var success = success || (success = function () {});
var lastState = function () {
return states.length - 1
};
var params;
if (typeof action === 'object') {
action = action.action;
params = action.params;
}
switch (action) {
case 'save':
success(save());
break;
case 'pop':
success(pop());
break;
case 'drop':
success(drop(params));
break;
case 'apply':
success(apply(params));
break;
};
var save = function () {
states.push({
currentUrl: this.driver.getCurrentUrl(),
baseUrl: this.baseUrl,
params: this.params,
moduleNames: this.moduleNames_,
moduleScripts: this.moduleScripts_,
});
this.moduleNames_ = [];
this.moduleScripts_ = [];
};
var pop = function () {
var state = states[lastState()];
this.moduleNames_ = state.moduleNames;
this.moduleScripts_ = state.moduleScripts;
this.get(state.currentUrl);
this.drop
};
var drop = function (state) {
state = state || (state = lastState());
states.splice(state, 1);
};
var apply = function (state) {
state = state || (state = lastState());
this.moduleNames_ = state.moduleNames;
this.moduleScripts_ = state.moduleScripts;
this.get(state.currentUrl);
};
};
save the state: browser.stash('save');
pop the last stashed state: browser.stash('pop',function(){//success callback});
apply a specific stash: browser.stash({action: 'apply', params: 0});
let me know if this sounds interesting then i will implement it in a Pull request.
Well what i see is that your implementation is stack like which probably is not giving much flexibility. What if i want to apply another state which is at the bottom of the stack ? The whole idea to do something is to have a reason for that. Why would you keep a state of your mockedModules? I come from java background and to be honest if you need some suites to use the same mocked modules you can just combine them in a single suite. I strongly recommend the state for mocked modules to be removed.
I agree that right now it's possible to do it if you write afterEach implementation where you remove every single mocked module. But if i have to do that in 100 suites each with let's say 2-4 modules maybe less , maybe more. these are 400 lines of unnecessary code.
Now if i reconsider the issue , it's explicitly related to garbage collection. You just remove objects you don't need or use.
Well that's my point of you.
p.s. really glad there is some sort of discussion going on.
What if i want to apply another state which is at the bottom of the stack ?
this can easily be solved by converting the stash to a javascript object so you can save the state with a specific key and later access the state with the same key.
Why would you keep a state of your mockedModules?
Not really keeping a state of a mockModule but keeping a state where you have a specific set of mock modules active.
if you need some suites to use the same mocked modules you can just combine them in a single suite.
You do have a point here, if there are some suites that use the same set of mock modules it's wise to combine them in order to keep your test suites DRY. however if you combine test suites purely based on the mock modules that they use you lose some flexibility as to keeping suites separated based on functionality.
I strongly recommend the state for mocked modules to be removed.
As far is i'm aware of mock modules don't have a specific state as of this moment and I agree that a mock module shouldn't have a state property.
however adding and removing mock modules gives a level of flexibility that should be kept intact.
I agree that right now it's possible to do it if you write afterEach implementation where you remove every single mocked module.
If you want to remove all active mock modules you can simply use: browser.clearMockModules()
Now if i reconsider the issue , it's explicitly related to garbage collection. You just remove objects you don't need or use.
The way modules are implemented in Angular seems to be that whenever the injector is used it's impossible to change the injected dependencies in this module on the fly.
that's why if you use the browser.clearMockModules() or browser.removeMockModule('moduleName') methods the change isn't processed until you reload or navigate to another page with browser.get('url');
Angular/protractor can't decide if you need a mocked module in a specific test unless you tell it to (with either a Add or Remove instruction).
I'm also glad that there is some sort of discussion going on and I hope you don't mistake my points as criticism. I believe discussions as these lead to the core issues that need to be tackled and are very valuable.
My current thoughts are that mocking modules is a configuration step, not a test-level step. So the state of the mocked modules should generally be saved for the whole test, not reset after each one. If you want to reset, you can do so manually, but I do not think this is the more common case.
Happy to continue discussion here, but I'm closing the issue to clean things up.