core: [Testing] [Angular-cli] Can't resolve all parameters when TranslateService is required in injection

I’m submitting a … (check one with “x”)

[ ] bug report => check the FAQ and search github for a similar issue or PR before submitting
[x] support request => check the FAQ and search github for a similar issue before submitting
[ ] feature request

Current behavior I can’t run basic karma+Jasmine tests using angular-cli configuration. I get the following error:

Error: Can’t resolve all parameters for I18nToolsService: (?).

And I18nToolsService’s constructor is:

constructor(private translator: TranslateService) {
}

My test file is a simple test file :

describe('I18nToolsService', () => {
    beforeEach(() => {
        TestBed.configureTestingModule({
            imports: [
                TranslateModule.forRoot()
            ],
            providers: [
                I18nToolsService
            ]
        });
    });

    it('should be created', inject([I18nToolsService], (service: I18nToolsService) => {
        expect(service).toBeTruthy();
    }));
});

Expected/desired behavior The tests should run fine, with no errors at startup.

Please tell us about your environment:

  • ngx-translate version: 8.0.0

  • Angular version: 4.3.6

  • Karma version: 1.4.1

  • Jasmine version: 2.5.2

  • Angular cli version: 1.4.0

About this issue

  • Original URL
  • State: open
  • Created 7 years ago
  • Reactions: 2
  • Comments: 15

Most upvoted comments

Are you importing the es7 reflect polyfill? If not, you could try if adding the following line to the polyfills.ts fixes your issues:

import 'core-js/es7/reflect'; // needed for unit testing

and you might also want to check you tsconfig.json file for "emitDecoratorMetadata": true,

I also had the same problem. I managed to solve by putting in tsconfig.spec.json from the root of the project the option emitDecoratorMetada as true.

My tsconfig.spec.json

{
  "extends": "./tsconfig.json",
  "compilerOptions": {
    "outDir": "./out-tsc/spec",
    "types": ["jest", "node"],
    "emitDecoratorMetadata": true
  },
  "files": ["src/polyfills.ts"],
  "include": ["src/**/*.spec.ts", "src/**/*.d.ts"]
}

Any updates on that? I really need to be able to test my app and at the moment this is the only thing I’m stuck with.

I faced the same problem and fixed it this way.

  1. Used this TranslateTestingModule : https://github.com/ngx-translate/core/issues/636#issuecomment-451137902
  2. Modified the contructor of MyComponent (the tested component):
constructor(@Inject(TranslateService) public translate: TranslateService) {
  1. Inside the test file:
beforeEach(async(() => {
  TestBed.configureTestingModule({
    imports: [
      TranslateTestingModule
    ],
    declarations: [
      MyComponent
    ],
    providers:[
      TranslateService
    ]
  }).compileComponents();
}));

Same here, some input would be nice since testing is an important part of any serious app; please provide some documentation on testing, thanks !