1

How to write unit tests a method with a result that is highly based on another method ?

async function getStatus(
  session,
  correlationId,
  data
) {

  const client = new SomeAPIWrapperClient({
    correlationId: correlationId,
    timeout: 10000
  })

  const result = await client.createSession(data)

  return result.status == 'SUCCESS';

}

Should I test that the method is creating a new SomeAPIWrapperClient and passing the received correlationId and 10000 !

Should I check if the client.createSession is called once with the passed data

marked as duplicate by gnat, BobDalgleish, Bart van Ingen Schenau, Greg Burghardt, GlenH7 Mar 24 at 16:53

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2

It would be a waste of time to test the entire functionality of this method, assuming that the referenced functions are already tested. Only unit-test the value added by a function. The value is that it successfully delegates some task to another API. Things like the exact timeout value are probably not that important: the function would work just as well if the timeout were changed to 10001.

Note that a single test exercising this function may be totally sufficient because it contains no branching. A more pedantic approach would be to test with a successful result, unsuccessful result, and service that times out.

But here a simple, isolated unit test isn't really possible because of the hard dependency on SomeAPIWrapperClient. Depending on what precisely you want to test, you might want to make that dependency injectable.

It may be even sensible to not unit-test this function at all, if it will be covered by integration tests. This is likely the preferable approach, and is what I would probably do.

It is also excusable to not test very simple code, if you are confident that it will work as expected. However, dynamic languages like JavaScript make it too easy to accidentally pass a wrong parameter, so that may be undesirable.

0

How to write unit tests a method with a result that is highly based on another method ?

The most straight forward approach is to convert the other method into an argument:

async function getStatus(
  session,
  correlationId,
  data
) {

  const client = new SomeAPIWrapperClient({
    correlationId: correlationId,
    timeout: 10000
  })

  return getStatusWithDependencyInjection(session,correlationId,data,client)
}

async function getStatusWithDependencyInjection(
  session,
  correlationId,
  data,
  client
) {
  const result = await client.createSession(data)

  return getStatusFromResult(result)
}

function getStatusFromResult(
  result
) {
  return result.status == 'SUCCESS';
}

If you experiment, I think you will find that get getStatusWithDependencyInjection and getStatusFromResult are both much easier to test than getStatus.

Arrange your code so that the parts that are hard to test are really really simple, and the parts that are complicated are also really really easy to test.

Not the answer you're looking for? Browse other questions tagged or ask your own question.