Bug(scope): Testing, Cannot read properties of null (reading 'nativeElement') #735
Replies: 2 comments
-
Same here. It looks like an error is being swallowed. Running tests inside the [transloco-playground] (https://github.com/ngneat/transloco/tree/master/apps/transloco-playground) shows:
|
Beta Was this translation helpful? Give feedback.
-
@Piiiiiiiiiiiim This doesn't seem related as he is using Transloco v4 which uses flat@5.0.2. import { createComponentFactory, Spectator } from '@ngneat/spectator';
import { OnPushComponent } from './on-push.component';
import { getTranslocoModule } from '../transloco-testing.module';
import { TranslocoService } from '@ngneat/transloco';
describe('OnPushComponent', () => {
let spectator: Spectator<OnPushComponent>;
const createComponent = createComponentFactory({
component: OnPushComponent,
imports: [
getTranslocoModule({
translocoConfig: { reRenderOnLangChange: true },
}),
],
});
beforeEach(() => (spectator = createComponent()));
it('should translate', () => {
expect(spectator.query('.structural [data-cy=regular]')).toHaveText(
'Regular: home spanish'
);
expect(spectator.query('.structural [data-cy=current-lang]')).toHaveText(
'Current Lang: es'
);
expect(spectator.query('.pipe [data-cy=p-regular]')).toHaveText(
'Regular: home spanish'
);
expect(spectator.query('.pipe [data-cy=p-regular]')).toHaveText(
'Regular: home spanish'
);
const service = spectator.inject(TranslocoService);
service.setActiveLang('en');
spectator.detectChanges();
expect(spectator.query('.structural [data-cy=regular]')).toHaveText(
'Regular: home english'
);
expect(spectator.query('.structural [data-cy=current-lang]')).toHaveText(
'Current Lang: en'
);
expect(spectator.query('.pipe [data-cy=p-regular]')).toHaveText(
'Regular: home english'
);
});
}); Please share a repo with the setup so I can take a further look. |
Beta Was this translation helpful? Give feedback.
-
Is there an existing issue for this?
Which Transloco package(s) are the source of the bug?
Don't know / other
Is this a regression?
No
Current behavior
Trying to run any test involving querying the DOM of any component using the structural directive returns an error of property being null, like the element doesn't even exist.
Expected behavior
Test should pass with the text of the H1 header being "Testing"
Please provide a link to a minimal reproduction of the bug, if you won't provide a link the issue won't be handled.
See current behaviour
Transloco Config
No response
Please provide the environment you discovered this bug in
Browser
Additional context
I tried making another test with the element outside of the structural directive and that works without issues, the problem only occurs when using transloco's structural directive for translation.
transloco-testing.module.ts
side-bar.component.spec.ts
side-bar.component.html
I would like to make a pull request for this bug
No
Beta Was this translation helpful? Give feedback.
All reactions