Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

153
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!