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

310
Vistas
How to mock a function using Jest that has parameters in it?

I have a method add which I want to mock:

A.js

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

export add;

I am calling this add method in componentDidMount like this

B.js

import add from './A';

class AddForm extends React.Component {
    ...
    componentDidMount() {
       add(1, 2, 3);
    }
    ...
}

export AddForm;

Now this is how I am testing it

B.test.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>,
     );
  }
}

I am not getting the mocked value 100 while testing instead the function is getting executed. I tried all answers that are available but unable to get this mocked response.

Anything that I am missing here?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Check these documents

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

Sample:

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 Denunciar
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