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

307
Views
¿Cómo simular una función usando Jest que tiene parámetros?

Tengo un método add que quiero simular:

A.js

 function add(a, b, c) { return a + b + c } export add;

Estoy llamando a este método de add en componentDidMount así

B.js

 import add from './A'; class AddForm extends React.Component { ... componentDidMount() { add(1, 2, 3); } ... } export AddForm;

Ahora así es como lo estoy probando.

B.prueba.js

 import AddForm from './B'; const helpers = require("./A"); jest.spyOn(helpers, "add").mockImplementation(() => 100); describe('<AddForm/>', () => { test('should render all elements', () => { return render( <AddForm></AddForm>, ); } }

No obtengo el valor simulado de 100 durante la prueba, sino que la función se ejecuta. Intenté todas las respuestas disponibles pero no pude obtener esta respuesta simulada.

¿Algo que me esté perdiendo aquí?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Revisa estos documentos

  • https://jestjs.io/docs/mock-functions#mocking-modules
  • https://jestjs.io/docs/mock-function-api

Muestra:

 const helpers = require("./A"); jest.mock('./A') test('should render all elements', () => { helper.add.mockReturnValue(100); // write your test case });
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!