vendredi 6 novembre 2020

Test endpoint with Jest and NestJS with fileuploading

I try to write a test to my endpoint, where I uploading some file. Here is a simple example:

  @Post(BOT_ROUTES.audio)
  @HttpCode(HttpStatus.OK)
  @UseInterceptors(FileInterceptor('file'))
  handleAudioMessage(@UploadedFile() file, @Body() body: SendAudioDTO) {
    return this.appService.handleVoiceMessage(file, body.context);
  }

The question is, how can I mock file uploading? Or maybe I did something wrong?

  describe('handleAudioMessage', () => {
    it('should return response from bot', async () => {
      const context = [
        {
          name: '',
          lifespanCount: 12,
          parameters: {
            fields: null,
          },
        },
      ];
      const result = {
        query: 'mock query',
        response: 'mock response',
        context,
      };
      const request = {
        context,
      };
      const file = fs.createReadStream(
        path.normalize(__dirname + '/output.wav'),
      );
      jest
        .spyOn(botService, 'handleBotMessage')
        .mockImplementation(async () => result);
      expect(await botController.handleAudioMessage(file, request)).toBe(
        result,
      );
    });
  });

Aucun commentaire:

Enregistrer un commentaire