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
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 await
ed and the resolved value of the promise has a RegisterExperimentAsync
method
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.