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

155
Visualizações
Unit Testing a Function with embedded Async Function using Jest

I am trying to unit test this function

getSoapClient(url, wsdlOptions) {
        return soap.createClientAsync(url, wsdlOptions).catch((err) => {
            if (err.message.includes('Invalid WSDL URL')) {
                throw new SOAPFaultClientErrorException('Invalid WSDL URL', 555);
            } else if (err.message.includes('getaddrinfo ENOTFOUND incorrect')) {
                throw new AddressNotFoundException('Address Not Found', 400);
            }
        })
    }

I am using jest and trying to mock the catch block for when the url is incorrect.

it('getSoapClient throws a SOAPFaultClientErrorException if the WSDL is invalid', (done)=> {
        soap.createClientAsync = jest.fn().mockImplementation( () => { throw new SOAPFaultClientErrorException('Invalid WSDL URL', 555)})
        soap.createClientAsync(incorrectUrl, wsdlOptions).catch(err=> {
          expect(err).toBe(new SOAPFaultClientErrorException('Invalid WSDL URL', 555))
        })
        done()
      })

This is not working and I'm getting an error when trying to mock the implementation.

I'm not sure why it is throwing an error in the test(it just highlights the mockImplementation line with a red arrow under new).

I tried jest.spyOn and that also gives me the same error.

enter image description here

How do I mock the throwing of an error from soap.createClientAsync and test that it is throwing an error.

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