Skip to content
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

StoreModule and MockBuilder issues #589

Closed
satanTime opened this issue May 21, 2021 · 14 comments · Fixed by #626 or #642
Closed

StoreModule and MockBuilder issues #589

satanTime opened this issue May 21, 2021 · 14 comments · Fixed by #626 or #642

Comments

@satanTime
Copy link
Member

I finally found why my tests runs not using initial (empty) initial state, this was because the spy on my metaReducer util was not taken into account, so I got previous value from sessionStorage which failed some tests :(

Here is the declaration in AppModule:

    StoreModule.forRoot({}, { metaReducers: [ReducerUtils.storePersist], runtimeChecks: environment.storeRuntimeChecks }),

Here is how I set spy (using jasmine), prior to MockBuilder usage:

      spyOn(ReducerUtils, 'storePersist').and.callFake((v) => v);

Maybe it can help you to see util's declaration ;)

export abstract class ReducerUtils {
  static storePersist(reducer: ActionReducer<object>): ActionReducer<object> {
    ...

If I add some logs in this util, I can see them in console 👎
If I remove metaReducers declaration, it works.

Moreover, I see another problem using StoreDevtoolsModule. I also have to keep it (.keep(StoreDevtoolsModule)). Most of tests fails because reducer function is not called even after store.dispatch.

you asked me before how I spy on store, here it is:

  describe('integration', () => {
    let store: Store;
    ...
    const initTestBed = () => {
      ...
      return MockBuilder(MyComponent, [MyModule, AppModule])
        .keep(StoreRootModule)
        .keep(StoreFeatureModule)
        .keep(StoreDevtoolsModule)
        .keep(NoopAnimationsModule)
        .keep(FormsModule)
        .keep(ReactiveFormsModule);
    };
    beforeEach(initTestBed);

    beforeEach(() => {
      fixture = MockRender(MyComponent);
      store = TestBed.inject(Store);
      spyOn(store, 'dispatch').and.callThrough();
      fixture.detectChanges();
    });

Originally posted by @Dji75 in #312 (comment)

@satanTime
Copy link
Member Author

Hi @Dji75,

let's discuss the issue here. Could it be an option for you to have a call with screensharing?

@Dji75
Copy link
Contributor

Dji75 commented May 21, 2021

Thanks, I just sent you an email

@satanTime
Copy link
Member Author

Hi @Dji75, today at 16 CET then :)

I tried to answer on your email but got a rejection email from outlook. So the same link you provided at 16 CET.

@satanTime
Copy link
Member Author

@Dji75, sure, let's do it at 16.15 then.

@satanTime
Copy link
Member Author

satanTime commented May 21, 2021

For the records.

2 issues:

  • kept store and mock store dev tools have a conflict (if no actions have been dispatched)
  • there is a way needed to properly handle mock / kept metaReducers in a kept store

@Dji75
Copy link
Contributor

Dji75 commented May 21, 2021

In the meantime, I've added the following lines in tests.ts in order to exclude the modules we reasonably expecting not useful in testing suites:

ngMocks.globalExclude(StoreDevtoolsModule);
ngMocks.globalExclude(BrowserAnimationsModule);

@satanTime
Copy link
Member Author

Hi @Dji75,

could you test if this update works for you? ng-mocks.zip.

The main change is that StoreDevtoolsModule is excluded by default.
The issue is that it has internal reducers and because the module has been mocked, the reducers are mocked too and nothing happens in the kept store. The reducers use useExisting and useClass and it makes too complicated to try to solve the whole chain in order to understand what we should keep and what we should mock. So I would leave it on shoulders of developers to decide what to do.

About BrowserAnimationsModule, looking forward to having a call to dig deeper into its issue.

satanTime added a commit that referenced this issue May 31, 2021
fix(core): excluding StoreDevtoolsModule by default #589
@satanTime satanTime reopened this May 31, 2021
@Dji75
Copy link
Contributor

Dji75 commented Jun 2, 2021

Hi @satanTime,

Thanks for this release :)
It solved issue with StoreDevtoolsModule as I do not need anymore to global exclude it (explicitly), but it does not solve issue with metaReducers.

I ran my test under Firefox (headless) and Chrome (headless) with same result (still an error even if slightly différent if I don't use this version):

// With Firefox
        <Jasmine>
        m0Ny/</</</</<@main.js:183957:47
        invoke@polyfills.js:5612:26
        QpwO/</</ProxyZoneSpec.prototype.onInvoke@vendor.js:169407:43
        invoke@polyfills.js:5611:52
        run@polyfills.js:5374:43
        runInTestZone@vendor.js:169687:38
        wrapTestInZone/<@vendor.js:169702:24
        <Jasmine>
        invoke@polyfills.js:5612:26
        run@polyfills.js:5374:43
        scheduleResolveOrReject/<@polyfills.js:6516:36
        invokeTask@polyfills.js:5646:31
        runTask@polyfills.js:5418:47
        drainMicroTaskQueue@polyfills.js:5822:35

// With Chrome
            at <Jasmine>
            at UserContext.<anonymous> (src/app/side-panel-wrapper/daily-planning/daily-planning.component.spec.ts:692:37)
            at ZoneDelegate.invoke (node_modules/zone.js/dist/zone-evergreen.js:372:1)
            at ProxyZoneSpec.onInvoke (node_modules/zone.js/dist/zone-testing.js:301:1)

The real util function given to metaReducers is still called (even with the spy)

We can have a call together if you want, same GMeet at 16PM (CET) if you are available 😉

@satanTime
Copy link
Member Author

Hi @Dji75,

thanks for the info. Yes, let's do that today at 16 CET.
Please send me a link to the call.

@satanTime
Copy link
Member Author

Add to the documentation information about USER_PROVIDED_META_REDUCERS in order to suppress meta reducers.

@satanTime
Copy link
Member Author

About aminations - the problem is that Animation module provides a mock version of RendererFactory2, it should be kept.

@satanTime
Copy link
Member Author

v12.1.0 has been released and contains a fix for the issue. Feel free to reopen the issue or to submit a new one if you meet any problems.

@Dji75
Copy link
Contributor

Dji75 commented Jun 7, 2021

It works like a charm 👍
Again, thanks @satanTime

@satanTime
Copy link
Member Author

Cool! Thanks for the report!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants