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

1.4K
Views
Se llama a la función simulada de Jest pero falla expect.toHaveBeenCalled()

Sé que ya hay preguntas sobre esto, pero no puedo encontrar una respuesta definitiva. Estoy usando SvelteKit y traté de burlarme de $app/navigation likes this en el archivo de configuración.

 jest.mock('$app/navigation', () => { return { __esModule: true, goto: jest.fn().mockImplementation((target) => console.log(target)) }; });

Pruebo un componente que llama goto . De hecho, se llama porque hay una llamada a console.log en la salida de prueba. Cuando traté de probarlo con expect(goto).toHaveBeenCalled() , falla.

 // SvelteKit import * as navigations from '$app/navigation'; it('show error when account does not exists', async () => { // render is in before Each await fireEvent.change(screen.getByLabelText('Email'), { target: { value: 'example@email.com' } }); await fireEvent.change(screen.getByLabelText('Password'), { target: { value: 'B@adPass0rd' } }); await fireEvent.click(screen.getByRole('button')); // There is no problem. It should redirect. expect(navigations.goto).toHaveBeenCalled(); });

Producción

 console.log /success at log (jest-setup.js:6:58) FAIL src/lib/routes-tests/login.test.js Login ✕ show error when account does not exists (23 ms) ● Login › show error when account does not exists expect(jest.fn()).toHaveBeenCalled() Expected number of calls: >= 1 Received number of calls: 0 24 | await fireEvent.click(screen.getByRole('button')); 25 | // expect(screen.queryByText('Account does not exist')).not.toBeNull(); > 26 | expect(navigations.goto).toHaveBeenCalled(); | ^ 27 | }); 28 | }); 29 | at toHaveBeenCalled (src/lib/routes-tests/login.test.js:26:28) at tryCatch (src/lib/routes-tests/login.test.js:23:2404) at Generator._invoke (src/lib/routes-tests/login.test.js:23:1964) at Generator.next (src/lib/routes-tests/login.test.js:23:3255) at asyncGeneratorStep (src/lib/routes-tests/login.test.js:25:103) at _next (src/lib/routes-tests/login.test.js:27:194)
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Resulta que llamé a goto en la función async . Debo usar waitFor para esperar el cambio.

 await waitFor(() => expect(navigations.goto).toHaveBeenCalled())
about 4 years ago · Juan Pablo Isaza Report
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!