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

164
Visualizações
Test primitive value on if statement with Jest

I would like to create a test with Jest to make sure there is an if statement in my code. Let's say we have a function that receives a string and I need to verify if this string includes something in it, if there is execute another function otherwise just ignore it.

function create (str) {
  if (str.includes('something')) {
    otherFunction()
  }

  ...
}

So I want not just to make sure there is an if statement, I also want that the string "calling" includes is str. I know I can use String.prototype but it does not make sure the string "calling" includes is str, any other string calling the same function passes the test

What I have till now:

test('Should call otherFunction if str includes something', () => {
  const { sut, otherObj } = makeSut()
  const otherFunctionSpy = jest.spyOn(otherObj, 'otherFunction')
  const includesSpy = jest.spyOn(String.prototype, 'includes')

  sut.create('has_something')

  expect(includesSpy).toHaveBeenCalledWith('something')
  expect(otherFunctionSpy).toHaveBeenCalled()
})
about 4 years ago · Juan Pablo Isaza
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