Anton Poznyakovskiy
Anton Poznyakovskiy

Reputation: 2169

Error: No Provider for Store! in @ngrx 4.x

When migrating my project from @ngrx 2.x to 4.1.0, I encountered the error message

NullInjectorError: No provider for Store!

The store was imported as shown in the docs:

import { StoreModule as NgRxStoreModule } from '@ngrx/store';

@NgModule({
  imports: [
    NgRxStoreModule.forRoot(reducerMap, {
      initialState: initial
    }),
    StoreRouterConnectingModule,
    EffectsModule.forRoot(effects)
  ],
  providers: [AppActions]
})
export class StoreModule {}

Upvotes: 9

Views: 17720

Answers (3)

Gal Margalit
Gal Margalit

Reputation: 5844

For ngrx 8 use:

import { provideMockStore } from '@ngrx/store/testing';

Upvotes: 0

uthomas
uthomas

Reputation: 754

I got this while I was trying to run tests in angular 7.

The solution for me was to:

  1. define a store mock in the body of describe:
let storeMock;
  1. initialize it in beforeEach section:
  beforeEach(async () => {
    storeMock = {
      dispatch: jasmine.createSpy("dispatch"),
      pipe: jasmine.createSpy("pipe").and.returnValue(from([{
...
        requestTimeout: 5000,
...
      }]))
    };
  1. define provider for Store in TestBed.configureTestingModule:
    TestBed.configureTestingModule({
      imports: [
        HttpClientTestingModule,
      ],
      providers: [
        ...
        {
          provide: Store,
          useValue: storeMock
        }
        ...
      ]
    });
    ```

Upvotes: 0

Anton Poznyakovskiy
Anton Poznyakovskiy

Reputation: 2169

Turned out that some of my services imported the store via

import { Store } from '@ngrx/store/src/store'

Changing the imports to

import { Store } from '@ngrx/store'

fixed the problem.

Upvotes: 11

Related Questions