Cannot getByRole for routerLink
See original GitHub issueBackground
I’ve been trying to do a query by role to check a link on my page using: screen.getByRole('link', { name: /contact/i })
. This keeps throwing an error as if the the element is undefined.
Debugging Inspecting my element shows it as
<a
_ngcontent-oet-c21=""
class="navigation--link navigation--link-active"
href="/contact"
ng-reflect-router-link="/contact"
ng-reflect-router-link-active="navigation--link-active"
routerlink="/contact"
routerlinkactive="navigation--link-active"
>
<i
_ngcontent-oet-c21=""
aria-hidden="true"
class="far fa-clock navigation--icon"
/>
contact
</a>
I tried debugging while running tests by using queryAllByRole(component.fixture.nativeElement, 'navigation)
to print out the DOM element and I see this. Looks like when the component is rendered it’s not adding in the href like it does for the live site
<a
class="navigation--link"
routerlink="/contact"
routerlinkactive="navigation--link-active"
>
<i
class="far fa-clock navigation--icon"
/>
Contact
</a>
I had a similar issue trying to get an image by alt text using screen.getByRole('img', { name: /company logo/i })
but not sure what was stopping this as Angular isn’t adding the alt text.
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
React Router - Testing Library
This example demonstrates React Router v6. For previous versions see below.
Read more >How to test link using react-testing-library and jest
I'm trying to figure out how to test a link. I've been looking all over the place for a solution and I can't...
Read more >testing-library - Bountysource
Cannot manually set route parameters on Vue-Testing-Library@next using ... const { getByRole } = render(h(App)); const button = getByRole("button"); ...
Read more >Testing an NgRx project - Tim Deschryver
How write maintainable NgRx tests within an Angular application.
Read more >react testing library click disabled button - You.com | The AI ...
TypeError: Cannot read property 'disabled' of null in react-testing-library ... getByRole("button", { name: /submit/i }); expect(emailErrorElement).not.
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
Hey, sorry this is a month later, but I just made some changes to my configuration and tests and it seems I either broke stuff, or just wasn’t testing if elements were in the Document in the first place… Pretty much any getByRole() isn’t working in my Angular project. I’ve tried using
component.getRoleBy('...')
,screen.getRoleBy('...')
,findRoleBy()
and none of them work, but the debugged html output looks good. If I try it in Testing Playground it works, just not in my project which makes me think it’s a configuration issue?In case someone comes across this, unable to find any element byRole.
This StackOverflow answer was what helped me.
The gist is to remove the “getComputedStyle” mock. (If you are using @angular-builders/jest it’s the globalMocks option).