Karma Jasmine unit testing causing Template parse errors: The pipe "translate" could not be found
Current behavior
I'm trying to unit test the simple ng2-translation application. Karma Jasmine unit test spec is not recognizing HTML tags, for example
beforeEach(()聽=>聽{
聽聽聽聽TestBed.configureTestingModule({
聽聽聽聽聽聽聽聽declarations:聽[聽SidenavComponent聽],聽// declare the test component
聽聽聽聽聽聽聽聽providers: [
聽聽聽聽聽聽聽聽聽聽聽聽TranslateService,
聽聽聽聽聽聽聽聽聽聽聽{聽provide: TranslateLoader,聽useFactory: (http: Http) =>聽new聽TranslateStaticLoader(http,聽'public/assets/i18n',聽'.json'),聽deps:[Http]},
]
});
});
I'm not sure that I'm providing the JSON files in proper format like above. How to resolve this pipe error?
Ocombe- Please provide sample unit tests for the examples you have provided in the git
Expected/desired behavior
Reproduction of the problem
If the current behavior is a bug or you can illustrate your feature request better with an example, please provide the steps to reproduce and if possible a minimal demo of the problem via https://plnkr.co or similar (you can use this template as a starting point: http://plnkr.co/edit/tpl:btpW3l0jr5beJVjohy1Q).
What is the expected behavior?
What is the motivation / use case for changing the behavior?
Please tell us about your environment:
ng2-translate version: x.x.x
Angular version: 2.1.0-rc.X
Browser: [IE 11 ]
Language: [all | TypeScript X.X | ES6/7 | ES5]
Hi,
you just need to import the module into your testbed (like you would for any other module, like HttpModule or RouterModule):
beforeEach(() => {
TestBed.configureTestingModule({
imports: [TranslateModule.forRoot({
provide: TranslateLoader,
useFactory: (http: Http) => new TranslateStaticLoader(http, 'public/assets/i18n', '.json'),
deps: [Http]
})],
declarations: [SidenavComponent] // declare the test component
});
});
Thank you Ocombe for your quick response. I added those imports and pipe error got resolved. But I see that content of
de聽=聽fixture.debugElement.query(By.css('h1'));
el聽=聽de.nativeElement;
console.log(el.textContent);
is empty. i.e ' '. Can you please suggest me something?
If you use the TranslateStaticLoader then you must resolve the requests before the content can change: https://github.com/ocombe/ng2-translate/blob/master/tests/translate.service.spec.ts#L62
But unless you're trying to test that translations work, you should probably mock the TranslateLoader with a custom loader that doesn't use http requests, that will make all you test much easier https://github.com/ocombe/ng2-translate/blob/master/tests/translate.service.spec.ts#L570-L594
I tried with that method but I'm getting TypeError: undefined is not an object (evaluating 'connection.mockRespond') in config/karma-test-shim.js error
This is my code
const聽mockBackendResponse聽=聽(connection:聽MockConnection,聽response:聽string)聽=>聽{
聽聽聽聽connection.mockRespond(new聽Response(new聽ResponseOptions({body:聽response})));
};
describe('SideNav Component Test', () => {
聽聽聽聽let聽translate: TranslateService
聽聽聽聽let聽connection: MockConnection;
beforeEach(()聽=>聽{
聽聽聽聽TestBed.configureTestingModule({
聽聽聽聽聽聽聽聽imports: [TranslateModule.forRoot({
聽聽聽聽聽聽聽聽聽聽聽聽provide: TranslateLoader,
聽聽聽聽聽聽聽聽聽聽聽聽useFactory: (http: Http) =>聽new聽TranslateStaticLoader(http,聽'app/public/assets/i18n',聽'.json'),
聽聽聽聽聽聽聽聽聽聽聽聽deps: [Http]
聽聽聽聽聽聽聽聽}),
聽聽聽聽聽聽聽聽聽聽聽聽HttpModule
聽聽聽聽聽聽聽聽],
聽聽聽聽聽聽聽聽declarations:聽[聽SidenavComponent聽],聽// declare the test component
聽聽聽聽聽聽聽聽providers: [
聽聽聽聽聽聽聽聽聽聽聽聽TranslateService,
聽聽聽聽聽聽聽聽],
聽聽聽聽});
});
it('should be able to get translations', () => {
聽聽聽聽聽聽聽聽mockBackendResponse(connection,聽'{"TEST": "Allgemein"}');
聽聽聽聽聽聽聽聽translate.get('TEST2').subscribe((res:聽string) => {
聽聽聽聽聽聽聽聽聽聽聽聽expect(res).toEqual('Allgemein');
聽聽聽聽聽聽聽聽});
聽聽聽聽});
});
You forgot to create the connection in your beforeEach: https://github.com/ocombe/ng2-translate/blob/master/tests/translate.service.spec.ts#L37
// sets the connection when someone tries to access the backend with an xhr request
backend.connections.subscribe((c: MockConnection) => connection = c);
Closing this, let me know if you need it to be reopened.
Not working!. I am using this plugin, and when I add 'Something' | translate in template of plugin not working
@ocombe Can you please tell how to fix this issue Pipe translate could not be found in [email protected]?
Please, answer the question set in the tittle for reference
For problemns with Jasmine test.
Its works for me guys 馃槈
Add this on MyComponent..component.spec.ts
import { TranslateModule } from "@ngx-translate/core";
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ EmailMessageComponent ],
imports: [
TranslateModule.forRoot(),
]
})
.compileComponents();
}));
Most helpful comment
@ocombe Can you please tell how to fix this issue
Pipe translate could not be foundin [email protected]?