0

I have code that imports library easysoap to get a request from WSDL. And I'm trying to refactor my unit test.

I've written the unit test with jest and mock the return value but it's called the true endpoint

my function requestConverter.helper.ts

import * as EasySoap from 'easysoap';
export async function requestConverter(params, method, body) {
  const opts = {
    secure: false
  };
  const easySoap = await EasySoap(params, opts);
  try {
    const result = easySoap.call({
      method,
      params: body
    });
    return result[`data`];
  } catch (err) {
    return err;
  }
}

my test file

import { requestConverter } from '../helpers/requestConverter.helper';
import * as EasySoap from 'easysoap';

describe('covert http request to wsdl request', () => {
  let params: object;
  let method: string;
  let body: object;
  let expectedResponse: object;
  let opts: object;
  beforeEach(() => {
    params = {
      host: '223.27.147.38',
      path: '/~kabgianyar/ppobwebservice/develop/webservice-pdam/CustomerBillingPelInfo3Tagihan.php',
      wsdl: '/~kabgianyar/ppobwebservice/develop/webservice-pdam/CustomerBillingPelInfo3Tagihan.php?wsdl'
    };
    method = 'billinfo';
    body = {
      namapengguna: 'value',
      katasandi: 'abs'
    };
    expectedResponse = {
      billinfoResponse: {
        'xmlns:ns1': 'http://schemas.xmlsoap.org/soap/envelope/',
        "return": 'mocked'
      }
    };
    opts = {
      secure: false
    };
  });
  afterEach(() => jest.restoreAllMocks());
  it('should return response from wsdl', async () => {
    jest.spyOn(EasySoap, 'call').mockImplementation(jest.fn().mockReturnValue(expectedResponse));
    const result = await requestConverter(params, method, body);
    expect(EasySoap.call).toBeCalled();
    console.log(JSON.stringify(result), '====');
  });
});

I want the response to be

expectedResponse = {
  billinfoResponse: {
    'xmlns:ns1': 'http://schemas.xmlsoap.org/soap/envelope/',
    "return": 'mocked'
  }
};
  • Have a look at these two threads: https://stackoverflow.com/a/57393748/4694994 and https://stackoverflow.com/a/57381798/4694994 – Kim Kern Aug 26 '19 at 21:59
  • it's not what i'm looking for – Abed Nego Lubis Aug 27 '19 at 08:05
  • In your code you're not using nest.js at all. Of course, that's an option but if you want to actually use nestjs then you should use the dependency injection system to mock your dependencies as described in those two threads. Otherwise you should remove nest.js from the tags. – Kim Kern Aug 27 '19 at 09:24
  • What solution do you need? jestjs or sinon? – Lin Du Nov 19 '19 at 08:36

0 Answers0