Bug(scope): Testing, Cannot read properties of null (reading 'nativeElement') #735
Replies: 6 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 [email protected]. 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.
-
Just as a note, we have exactly the same issue: And I've postponed the whole topic and had a look at it yesterday. Interestingly, in our design system the tests now execute, but in a department shared component lib, it continues to fail. I was not able to spot the difference yet. Setup, Versions etc seem identical. I'll see if I can find a way to reproduce it either in a Stackblitz or maybe in a separate repo and publish that in github |
Beta Was this translation helpful? Give feedback.
-
I tried reproducing it, but I wasn't able to with a clean nx repo (encountered other bugs) but not this one. I'm trying to move our components into a new nx repo and then see if I figure out where the difference is. |
Beta Was this translation helpful? Give feedback.
-
I found out what is causing the issue, but I don't know how to best create a repo. We have an internal published design system using transloco (its specified as peerDependency). As soon as I'm adding this as a dependency into my repository, the otherwise working setup: jest.config.ts:
fails inside the index.d.ts:
If I change the transformIgnorePatterns to this:
The error goes away, but I'm assuming now too many things are actually excluded and then, the transloco directive fails to work and I get the undefined error |
Beta Was this translation helpful? Give feedback.
-
We fixed it by applying this change to our jest.config.ts: #745 (comment) |
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