Skip to main content

How to test NGXS in Angular applications

If you need to avoid mocking of NGXS in your modules, you need to use .keep.

beforeEach(() =>
MockBuilder(TargetComponent, TargetModule)
// NgxsModule.forRoot() is called in TargetModule or its imports
.keep(NgxsModule.forRoot().ngModule) // keeps all NgxsModule.forRoot
// add it only if your module imports NgxsModule.forFeature
// NgxsModule.forFeature() is called in TargetModule or its imports
.keep(NgxsModule.forFeature().ngModule) // keeps all NgxsModule.forFeature
);

if your module imports NgxsModule.forFeature only, you need to add NgxsModule.forRoot() manually:

beforeEach(() =>
MockBuilder(
// keep and export
[
TargetComponent,
NgxsModule.forRoot(), // provides required services
],
// mock
TargetModule,
).keep(NgxsModule.forFeature().ngModule) // keeps all NgxsModule.forFeature
);