0

I want to mock AWS service in unit tests , we already have aws service used in controller but how to make mock service of aws and replace the working of actual service to mock service to get values on which my entity depends in unit tests

async create(entity: Template, options?: Options): Promise<Template> {
    await this.businessUnitRepository.findById(entity.businessUnitId);
    entity.body = _.unescape(entity.body);
    const response = await this.pinpointService
      .saveEmailTemplate(
        entity.name,
        entity.subject,
        entity.body,
        entity.description ?? '',
      )
      .catch(error => {
        throw error;
      });
    let key;
    try {
      key = await this.lambdaService.saveEmailTemplate(
        `${entity.name}-v1`,
        TemplateService.getPDFContent(entity.subject, entity.body),
      );
    } catch (LambdaError) {
      await this.pinpointService.deleteEmailTemplate(entity.name);
      throw LambdaError;
    }
    try {
      if (response.CreateTemplateMessageBody.Arn) {
        entity.arn = response.CreateTemplateMessageBody.Arn;
      }
      if (key) {
        entity.key = key;
      }
      entity.version = '1';
      return await this.templateRepository.create(entity, options);
    } catch (error) {
      if (error.code === DbErrorsEnum.Conflict) {
        throw new HttpErrors.Conflict(ErrorKeys.NameAlreadyExist);
      }
      await this.s3Service.deleteEmailTemplate(key);
      await this.pinpointService.deleteEmailTemplate(entity.name);
      throw error;
    }
  }

Create function uses 2 AWS services pinpoint and lambda which returns ARN and KEY , but how to get these values in unit testing , as while testing aws can't be mocked

Manthan
  • 1
  • 2
  • You need a mock/stub library like [jestjs](https://jestjs.io/) or [sinonjs](https://sinonjs.org/) – Lin Du Apr 17 '20 at 03:41
  • Welcome to Stackoverflow. It's generally a good idea to show some code along with your question. That will help others be able to give you a better quality answer that is also easier for you and future people to understand. https://stackoverflow.com/help/how-to-ask –  Apr 18 '20 at 16:04

0 Answers0