Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

309
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda