store: 🐞[BUG]: No reset of NgxsModule.forRoot after test run

I’m submitting a…


[ ] Regression (a behavior that used to work and stopped working in a new release)
[x] Bug report  
[ ] Performance issue
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => https://github.com/ngxs/store/blob/master/CONTRIBUTING.md
[ ] Other... Please describe:

Current behavior

I want to test a component that has a @Select() on a state.

@Component({
  selector: 'my-user',
  templateUrl: './user.component.html',
  styleUrls: ['./user.component.scss'],
})
export class UserComponent {
  /** select current user */
  @Select(UserState)
  user$: Observable<UserDto>;

At my test, I prefill the store with:

 beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [UserComponent],
      imports: [
       ...
      ],
    }).compileComponents();

    const store = TestBed.get(Store);
    store.reset({
        user: {
            id: 3,
            name: 'MyUser',
        },
    });

Obviously, this will fail, because I forget the TestBed-Import of the NgxsModule with the according state:

      imports: [
        NgxsModule.forRoot([UserState], {
          developmentMode: true,
        }),
      ],

But, if there is any other test that runs before the UserComponent-test and imports this UserState then my UserComponent-test will succeed.

At the moment, we have over 500 tests running in random order and from time to time there are failures because someone forget to provide the neccessary state at NgxsModule.forRoot(). But with an other random order, this test will succeed again.

Expected behavior

NgxsModule.forRoot() should be isolated for each TestBed.

Environment


Libs:
- @angular/core version: 7.2.15
- @ngxs/store version: 3.3.4


Browser:
- [x] Chrome (desktop) version 75.0.3770 (Windows 10.0.0) 
- [ ] Chrome (Android) version XX
- [ ] Chrome (iOS) version XX
- [ ] Firefox version XX
- [ ] Safari (desktop) version XX
- [ ] Safari (iOS) version XX
- [ ] IE version XX
- [ ] Edge version XX
 
For Tooling issues:
- Node version: v12.6.0  
- Platform: Windows 10

Others:

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 1
  • Comments: 21 (12 by maintainers)

Most upvoted comments

Indeed, it works! Thank you.

Works for me (even if I remove the HelloComponent spec) […] if it is not, give me another example to reproduce

I think there is a misunderstanding. Your NgxsModule setup in the test is not the same as in @amjmhs’s original Stackblitz. You added UserState explicitly, but amjmhs β€˜forgot’ to add UserState there on purpose to demonstrate the issue. The weird behavior is that the test passes if another, completely unrelated spec inits ngxs with the UserState.

BTW: Resetting the store during test setup is complete valid according to ngxs docs.