Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 12111

test for nested exception nestjs

$
0
0

I have a nested exception in my method which returns an error based on request (http or proto) and I'm writing tests for it with vitest. The problem I encounter with it is my tests pass successfully, but I get an error from vitest in runtime of my test and the thrown error is my inner exception

test:

test("BadRequestException should throw since sticker doesn't exist", async () => {      const result = () =>        fakeUserSetService.addRecently("475003664466759904","658693e1b5e04d540ba22088"        );      expect(result).rejects.toThrowError(CustomException)      );    });  });

method :

async addRecently(userId: string, stickerId: string) {    const stickerCheck = await this.StickerRepo.find({      _id: stickerId,      status: StickerStatus.OK,    });    if (!isFilledObject(stickerCheck)) {      throw new CustomException(        new NotFoundException(          this.i18n.t("fa.errors.SINGLE_STICKER_NOT_FOUND")        ),        {          majorCode: 10,          minorCode: 1,        }      );    }

Error:

CustomException: sticker not found❯ UserSetService.addRecently src/v1/user/user-set/user-set.service.ts:157:13    155|    156|     if (!isFilledObject(stickerCheck)) {    157|       throw new CustomException(       |             ^    158|         new NotFoundException(    159|           this.i18n.t("fa.errors.SINGLE_STICKER_NOT_FOUND")

I tried new CustomException(new NotFoundException()) this at my test expect but that didn't work


Viewing all articles
Browse latest Browse all 12111

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>