Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

418
Views
How to test a react router link with testing library?

I want to test a react router link with testing library, I tried:

App.js:

import React from 'react'
import { Routes, NavLink, Route } from 'react-router-dom'

function App() {
  return (
    <div>
      <nav>
        <NavLink to={"/"}>Home</NavLink>
        <NavLink to={"/about"}>About</NavLink>
      </nav>
      <Routes>
        <Route path="/" element="home page" />
        <Route path="/about" element="about page" />
      </Routes>
    </div>
  )
}

export default App

App.test.js:

import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { createMemoryHistory } from 'history';
import { Route, Router } from "react-router-dom"
import App from './App';

test('first home link', () => {
  const history = createMemoryHistory({ initialEntries: ["/"] });
  render(
    <Router location={history.location} navigator={history}>
      <App />
    </Router>
  );
  const linkElement = screen.getByRole("link", { name: /about/i });

  userEvent.click(linkElement);
  
  screen.debug();
});

Output of screen.debug:

    <body>
      <div>
        <div>
          <nav>
            <a
              aria-current="page"
              class="active"     
              href="/"
            >
              Home
            </a>
            <a
              class=""
              href="/about"
            >
              About
            </a>
          </nav>
          home page
        </div>
      </div>
    </body>

the page didn't change to about page like you see on the output the screen.debug

Is there is a way to fix that?

dependencies:

  1. "@testing-library/jest-dom": "^5.16.4"
  2. "@testing-library/react": "^13.3.0"
  3. "@testing-library/user-event": "^13.5.0"
  4. "history": "^5.3.0"
  5. "react": "^18.1.0"
  6. "react-router-dom": "^6.3.0"
  7. "react-dom": "^18.1.0"
about 4 years ago · Santiago Gelvez
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!