Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

419
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda