'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.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source