1

I have a soap RestAPI to call for excecuting a function from a service for which i have used soap lib with async await. The code is working fine. When comes to unit testing the test case fails at the callback method returning from the client. The code and UT error follows.

Function to call Soap Client - Code - Helper.ts

class soapHelper {
    public async registerInSoap(
            uploadRequest: ImportExperimentRequestDto,
        ): Promise<{ RegisterExperimentResult: IffManPublishResponseDto }> {
            const url = "http://test.com/Services/Req.svc?wsdl";
            const client = await soap.createClientAsync(flavourUrl);
            return client.RegisterExperimentAsync({ upload: uploadRequest});
        }
}

Test Case - Code

describe("**** Register via soap service ****", () => {
        it("** should excecute register method **", async () => {
            const request = cloneDeep(MOCK.API_PAYLOAD);
            const clientResponse = {
                RegisterExperimentAsync: jest.fn(),
            };
            jest.spyOn<any, any>(soap, "createClientAsync").mockReturnValueOnce(() => Promise.resolve(clientResponse));
            const result= await soapHelper.registerInSoap(request);
            expect(result).toEqual({ Result: AFB_MOCK_RESPONSE.API_RESPONSE });
        });
    });

Error

TypeError: client.RegisterExperimentAsync is not a function

UT Error

1 Answers1

0

You tell Jest that soapHelper.createClientAsync should return a function that has a RegisterExperimentAsync method instead of telling it that it should immediately return a promise of the object with the RegisterExperimentAsync method. This snippet should fix it:

    describe("**** Register via soap service ****", () => {
        it("** should excecute register method **", async () => {
            const request = cloneDeep(MOCK.API_PAYLOAD);
            const clientResponse = {
                RegisterExperimentAsync: jest.fn(),
            };
            jest.spyOn<any, any>(soap, "createClientAsync").mockResolvedValue(clientResponse);
            const result= await soapHelper.registerInSoap(request);
            expect(result).toEqual({ Result: AFB_MOCK_RESPONSE.API_RESPONSE });
        });
    });

Now soapHelper.createClientAsync is properly returning a promise to be awaited and the resolved value of the promise has a RegisterExperimentAsync method

Jay McDoniel
  • 57,339
  • 7
  • 135
  • 147