question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot read property 'translate' of undefined in karma spec test

See original GitHub issue

I’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:closed
  • Created 4 years ago
  • Comments:15 (5 by maintainers)

github_iconTop GitHub Comments

4reactions
stijnvncommented, Mar 5, 2020

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.

constructor(private translocoService: TranslocoService) {}

...
this.translocoService.translate('some-key); // The key is not translated
...

This only occurs in the unit test. In the application it is working fine.

2reactions
shaharkazazcommented, Dec 16, 2020

@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:

  1. TypeError: Cannot read property 'translate' of undefined thrown when using the pure translate function.
  2. Values don’t get translated when using the pure translate or the translocoService.translate.

Explnations

  1. This issue was caused since the pure translate function is a proxy the translocoService.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 get TypeError: Cannot read property 'translate' of undefined.

  2. 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

  1. I’ll update the testing module to take care of the injection.
  2. While this behavior makes sense in the real app, it could be tiring to make sure the translations are loaded in specs, I’ll add an option to preload all the given langs via the module. ({ 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 change TranslocoTestingModule.withLangs is deprecated (still supported and also supports the new option but will be removed in v3) and you should now use TranslocoTestingModule.forRoot.
  3. I’ll update all the docs accordingly.
Read more comments on GitHub >

github_iconTop 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 &#38;#039;length&#38
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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found