Test case fail for method returning from soap.createClientAsync function

Viewed 34

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

enter image description here

1 Answers

The mock resolved value of soap.createClientAsync() should be a soap client.

E.g.

index.ts:

import * as soap from 'soap';

interface ImportExperimentRequestDto {}
interface IffManPublishResponseDto {}

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

index.test.ts:

import { Client } from 'soap';
import * as soap from 'soap';
import { SoapHelper } from '.';

describe('**** Register via soap service ****', () => {
  it('** should excecute register method **', async () => {
    const request = {};
    const mClient = ({
      RegisterExperimentAsync: jest.fn().mockResolvedValueOnce({ Result: 'mock result' }),
    } as unknown) as Client;
    jest.spyOn(soap, 'createClientAsync').mockResolvedValue(mClient);
    const soapHelper = new SoapHelper();
    const result = await soapHelper.registerInSoap(request);
    expect(result).toEqual({Result: 'mock result'});
  });
});

Test result:

 PASS  stackoverflow/72604110/index.test.ts (9.87 s)
  **** Register via soap service ****
    ✓ ** should excecute register method ** (3 ms)

----------|---------|----------|---------|---------|-------------------
File      | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
----------|---------|----------|---------|---------|-------------------
All files |     100 |      100 |     100 |     100 |                   
 index.ts |     100 |      100 |     100 |     100 |                   
----------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        10.471 s
Related