#Unit testing

5 messages · Page 1 of 1 (latest)

gaunt falcon
#

I have to unit test some service without installing any third party packages like "Automock" or "golevelup" packages.

// myService.service.ts
export class MyService {
  constructor(private readonly getDataApi: GetDataApi) {}

  async getDataApi(params: MyParams): Promise<Datanull> {
     this.getDataApi.execute({ ...params });
  }
}

// get-data-api.ts
export class GetDataApi {
  constructor(private readonly configService: ConfigService, private readonly httpService: IHttpService){}

  async execute() {
    const config: HttpServiceRequestConfig = {
      // ...some config
    }
    // make a GET request and return data
    await httpService.request<Data>(config);
  }
}

Any idea how to unit test myService with mocking all this dependencies?

proper dove
#

Not sure if there is anything to test here.

The MyService should be easily covered by typescript linting since if you do not call an API of sort, you likely do not get the Data interface.

For the API, the config format is already enforced by the type with linting again, and once you enforce the return type, you'll notice that you never returned the response.

I suppose you could write a test for the configuration that would logically validate the conditions.

#

Though now that I think about it, you have the same issue with the Service 😄 you just didn't return things there so it should already yell at you.

fickle lantern
#

You can instead setup Contract Tests if you want to make sure that this is all configured right, usually not worth the time. If you zoom out one more level, whatever calls the service, if you inject a fake imlpementation (or stub) of the GetDataApi then you're set.

proper dove
#

Oh yeah, I quite like that E2E one for these. Especially if you can inject a local mock of the external API you are calling.