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

179
Views
setState no funciona en la prueba Jest usando React Testing Library

En un componente determinado, me estoy burlando de una llamada a la API que devuelve algunas marcas para un documento HTML. Logré que el simulacro funcionara y se devolviera la respuesta simulada, pero mi componente no puede establecer el estado con esta respuesta. Así es como se ve.

 export const RetrieveContent = (props: Props) => { const [content, setContent] = useState < Partial < ContentViewModel >> {}; useEffect(() => { const requestContent = async (props: Props) => { const { id } = props; const data = await getContent(id); // console.log(data) <-- This logs the object correctly! setContent(data); }; requestContent(props); }, []); return <>{parse(content.contentObject.content.body)}</>; // content is an empty object here };

Después de llamar a setState , el estado sigue siendo un objeto vacío. La prueba es bastante simple.

 cconst server = setupServer( rest.get('/content/*', (req, res, ctx) => { return res(ctx.json(ContentViewModelMock)) }) ) describe('<Content /> tests', () => { beforeAll(() => server.listen()) beforeEach(() => server.resetHandlers()) afterAll(() => server.close()) test('should display content when the id is found', async () => { const { asFragment } = render(<Content id={`${faker.datatype.uuid}`} />) console.log(asFragment().textContent) // This returns nothing }) })
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!