Cannot read property 'translate' of undefined in karma spec test
See original GitHub issueI’m submitting a…
[ ] Regression (a behavior that used to work and stopped working in a new release)
[x] Bug report
[ ] Performance issue
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request
[ ] Other... Please describe:
Current behavior
Spec test fails and says translate(....)
is undefined.
Expected behavior
Spec test runs sucessful and translate does not throw an error.
Minimal reproduction of the problem with instructions
Create a component and call translate('some key')
somewhere in it.
import { translate } from '@ngneat/transloco';
Create a spec test (default generated) Add the following import: import { getTranslocoModule } from ‘…/transloco-testing-module’;
describe('MapContainerComponent', () => {
let component: MapContainerComponent;
let fixture: ComponentFixture<MapContainerComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ MapContainerComponent ],
imports: [HttpClientTestingModule, RouterTestingModule, getTranslocoModule()],
providers: [Network],
schemas: [CUSTOM_ELEMENTS_SCHEMA],
})
.compileComponents();
}));
Transloco Testing Module:
import { TranslocoTestingModule, TranslocoConfig } from '@ngneat/transloco';
import en from '../assets/i18n/en-US.json'
import de from '../assets/i18n/de-DE.json'
export function getTranslocoModule(config: Partial<TranslocoConfig> = {}) {
return TranslocoTestingModule.withLangs(
{ en, de },
{
availableLangs: ['en-US', 'de-DE'],
defaultLang: 'en-US',
reRenderOnLangChange: true,
...config
}
);
}
Environment
Angular version: 8.1.2
```
"@ngneat/transloco": "^2.13.5",
"@ngneat/transloco-locale": "^1.0.1",
```
Browser:
- [x] Chrome (desktop) version XX
- [ ] Chrome (Android) version XX
- [ ] Chrome (iOS) version XX
- [x] Firefox version XX
- [ ] Safari (desktop) version XX
- [ ] Safari (iOS) version XX
- [ ] IE version XX
- [ ] Edge version XX
For Tooling issues:
- Node version: 12.14.1
- Platform: Windows, Linux
Others:
Issue Analytics
- State:
- Created 4 years ago
- Comments:15 (5 by maintainers)
Top Results From Across the Web
Angular Unit Test - Cannot read properties of undefined ...
The error ERROR: TypeError: Cannot read properties of undefined (reading 'subscribe') indicates that the service is undefined.
Read more >typeerror: cannot read properties of undefined ... - You.com
Here's an example of a JavaScript TypeError: Cannot read property of undefined thrown when a property is attempted to be read on an...
Read more >Cannot read property 'subscribe' of undefined - ng-mocks
This issue means that something has been replaced with a mock object and returns a dummy result ( undefined ) instead of observable...
Read more >ERROR in Cannot read property &#039;length&
Can you give more information about this: ERROR in Cannot read property 'length' of undefined ? Try to update "screenfull": "^3.3.0" to latest...
Read more >Component testing scenarios - Angular
Angular doesn't know that you set the input element's value property. It won't read ... The purpose of the spec is to test...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I can confirm this bug also exists in combination with Angular 9.0.2.
When I change my component to explicitly reference the TranslocoService, I can avoid the undefined error, but the key is not translated.
This only occurs in the unit test. In the application it is working fine.
@developer239 I personally didn’t have time to get to it in the last couple of months so it just got missed, thanks for bringing this up again 🙂
@tommueller Thank you for the reproduction, here is a summary of the issues discussed above: There are 2 issues here that got mixed together:
TypeError: Cannot read property 'translate' of undefined
thrown when using the pure translate function.translate
or thetranslocoService.translate
.Explnations
This issue was caused since the pure
translate
function is a proxy thetranslocoService.translate
so while in your actual app the service was already initialized when you got to that component if you didn’t provide it as part of your testing you getTypeError: Cannot read property 'translate' of undefined
.This is stated in the docs, and also applies to the specs, it’s your responsibility to make sure that the translations are already loaded when using the synchronous translate function.
Solutions
{ preloadLangs: true }
). You only need to use this option if you aren’t using transloco in the template since if you are it takes care of the loading for you. Note that as part of this changeTranslocoTestingModule.withLangs
is deprecated (still supported and also supports the new option but will be removed in v3) and you should now useTranslocoTestingModule.forRoot
.