-
Notifications
You must be signed in to change notification settings - Fork 91
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unstable tests caused by NgxWebstorage #156
Comments
I kept investigating to try to understand what is the cause of this issue. The rxjs documentation states that Observable (of is a builder of an observable) guarantees that the processing will be synchronous unless we use a specific scheduler (but this does not seem to be the case). I still don't know what is the cause of the issue. It does not seem to be related to rxjs but I keep thinking that it could come from a bad synchronization issue. I'll let you know if I have some new information. |
After having spent a lot of time to investigate this issue (that made us lose a lot of time in our development process), I think I could identify its cause. It appears that the main problem comes from the fact that the For some reasons (including this one), i stopped using the decorators in favor to the services. This showed that in some tests we forgot to provide the module. Most of the time, it is in the tests of components that do not use directly a storage but embed a sub component or use a service that uses a storage. Also, during my investigation I tried to use the in-memory storage to be sure to use a fresh new storage at each test by adding I ended with adding this global jasmine hook to be sure the local and the session storage are empty when a test starts:
This avoid clearing manually the storage in all the tests that use a storage or depend on a component that use a storage. I have been pushing those changes a few days ago and the tests seem to be stable now. Hope this will really be the case! To sum up, it appears that the problem came from a misuse of the library. But I think the library could help us to detect that, for instance by failing to start when using the decorators without having configured the module. What do you think about that? |
Versions (please complete the following information):
Describe the bug
We are using NgxWebstorage in an Angular project. Our project has some component tests (that use Angular TestBed) and some of them are unstable: they randomly fail, I would say half of the time at least one test fail which makes the build fail. It's very painful since each time I have to relauch a build on the CI until it's successful (sometimes I have to retry up to three times).
There are multiple tests that are unstable (not a single one) and there are two different causes, both of them seem to be related to NgxWebstorage:
LocalStorageService
and the component reads that value and behaves differently according to this value. Most of the time the value read is ok, but sometimes, with the exact same code, the value read isundefined
. Another example, an action on the component is supposed to write something in the local storage, at the end of the execution we check with theLocalStorageService
that the value is correctly saved and that's not the case.invalid_strategy
error:To Reproduce
That's difficult to say how to reproduce that since it happens randomly.
I noticed that not all the tests that are using NgxWebstorage are unstable. Only some of them randomly fail and I couldn't identify why only some of them are unstable.
Our components use the
@LocalStorage
and@SessionStorage
decorators to read and write the storages. Don't know whether this could be the cause of the issue.The test code uses the NgxWebstorage module with the default configuration:
All the tests that use a storage clear it at the end of their execution.
Expected behavior
I would expect my code to have the same behavior every time it is executed but unfortunately that's not the case.
Desktop (please complete the following information):
That happen both on my desktop and on the CI server, they have the same configuration:
Note that I am using the Angular CLI to run tests (karma and jasmine) and the tests are executed in parallel on Chrome and Firefox and they randomly fail on both of them.
Additional context
I assume that the system load could be responsible of the randomness, at least for the issue 1. Some bugs whose cause is a synchronization issue can happen for instance only if the execution is very slow. What makes me believe that is:
SyncStorage
class (base class ofLocalStorageService
andSessionStorageService
) delegates the operations to the theStorageStrategy
which is async (for instancethis.strategy.set(StorageKeyManager.normalize(key), value).subscribe(noop);
for thestore
method). I don't know rxjs enough to know whether it is guaranteed that an observable created directly from the value as here will execute synchronously when it is observed but this may be the cause of synchronization issues.I read the documentation and the code of the NgxStorage module and I did not find many tips regarding how to test some components using this module. Maybe I should use another storage configuration when running the tests? I tried to use a stub strategy as shown here but I got the same behavior. This means I am facing this issue I am using both the real browser storage and a fake in-memory storage.
Sorry for this large description but I wanted to provide as much information as possible. Thanks for building this great module!
The text was updated successfully, but these errors were encountered: