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

168
Visualizações
JS - Mocking an uninitialized class method inside a function

Lets say I have this code below and I want to test someFunc() but I want to mock the doSomething() function? How can I do it in a test when the SomeClass is initialized only inside the someFunc() itself and not in the test?

class SomeClass {
  constructor(element){
    this.element = element
  }
  
  doSomething(element){
    ...
  }
}

function someFunc() {
  const newClass = new SomeClass()
  newClass.doSomething() // I want this to get to the mocked function
}

I am using jest, please help!

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Try some testing frameworks, most of them provide spies which allow you to mock specific functions for given classes.

For example, you can try Jasmine - https://jasmine.github.io/api/edge/Spy.html Where you would do something like

spyOn(newClass, 'doSomething').and.returnValue(42);
about 4 years ago · Juan Pablo Isaza Relatório

0

There are multiple ways to mock es6 class in Jest: https://jestjs.io/docs/es6-class-mocks

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