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

397
Vistas
React Jest Error: Too many re-renders. React limits the number of renders to prevent an infinite

I don´t understand why I'm having error "Too many re-renders. React limits the number of renders to prevent an infinite loop." when I run my test, this is where I make the request:

// usePokemon.js

export const usePokemon = (pokemonName) => {
  const [pokemon, setPokemon] = useState(null);
  const [error, setError] = useState(null);
  const [isLoading, SetIsLoading] = useState(true);

  useEffect(() => {
    async function fetchPokemon() {
      SetIsLoading(true);
      try {
        let namePokemon = pokemonName.toLocaleLowerCase();
        const response = await fetch(
          `https://pokeapi.co/api/v2/pokemon/${namePokemon}`
        );
        const json = await response.json();
        setPokemon(json);
      } catch (err) {
        setError(err);
      }
      SetIsLoading(false);
    }
    fetchPokemon();
  }, [pokemonName]);

  return { pokemon, error, isLoading };
};

this is my Hook Test:

//usePokemon.test.js

import { act, renderHook } from "@testing-library/react-hooks";
import { usePokemon } from "./usePokemon";

const getControlledPromise = () => {
  let deferred;
  const promise = new Promise((resolve, reject) => {
    deferred = { resolve, reject };
  });
  return { deferred, promise };
};

describe("Success test", () => {

    test("handles loading state correctly", async () => {
      const { deferred, promise } = getControlledPromise();
  
      global.fetch = jest.fn(() => promise);
  
      const { result, waitForNextUpdate } = renderHook(usePokemon);
  
      expect(result.current.isLoading).toBe(true);
      deferred.resolve();
  
      await waitForNextUpdate();
      expect(result.current.isLoading).toBe(false);
    });
});

Too many re-renders. React limits the number of renders to prevent an infinite loop.

  31 |       global.fetch = jest.fn(() => promise);
  32 |   
> 33 |       const { result, waitForNextUpdate } = renderHook(usePokemon);
     |                                             ^
about 4 years ago · Juan Pablo Isaza
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