Joe Attardi
Joe Attardi

Reputation: 4521

Angular testing module not being set up right: Can't resolve all parameters (ngrx)

I have an Angular CLI 6 project that I'm trying to get tests working in. Here is a snippet from my test:

beforeEach(() => {
  spyOn(console, 'info');

  TestBed.configureTestingModule({
    providers: [
      ConsoleLoggerEffects,
      provideMockActions(() => actions),
    ],
  });

  effects = TestBed.get(ConsoleLoggerEffects);
});

The ConsoleLoggerEffects has a single dependency injected - the Actions observable:

@Injectable() export class ConsoleLoggerEffects { constructor(private actions$: Actions) { } }

This is exactly following the ngrx example, but it's not working.

When I go to run the tests, Angular complains Error: Can't resolve all parameters for ConsoleLoggerEffects: (?). I added a console.log to the factory function passed to provideMockActions, and it was never executed, so the factory function isn't even being called.

Even if I don't use provideMockActions and specify a provider myself, it also doesn't work:

TestBed.configureTestingModule({
    providers: [
      ConsoleLoggerEffects,
      { provide: Actions, useValue: new ReplaySubject(1)}
    ]
  });

Anyone know what might be going on?

Upvotes: 3

Views: 457

Answers (1)

Colo Ghidini
Colo Ghidini

Reputation: 723

I had the same issue, and I finally could fix it by editing my tsconfig.spec.json, then removing node_modules and reinstalling them.

This is how my TS Spec configuration file looks like now:

But you can follow the one in the example-app on https://github.com/ngrx/platform

{
  "extends": "../tsconfig.json",
  "compilerOptions": {
    "sourceMap": true,
    "declaration": false,
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "lib": ["es2017", "dom"],
    "outDir": "../out-tsc/spec",
    "module": "commonjs",
    "target": "es6",
    "types": ["jest", "node"],
    "baseUrl": ".",
    "rootDir": "./"
  },
  "files": [
    "polyfills.ts"
  ],
  "include": [
    "**/*.spec.ts",
    "**/*.d.ts"
  ]
}

Upvotes: 1

Related Questions