【问题标题】:Test Jest and NestJs测试 Jest 和 NestJs
【发布时间】:2022-01-06 15:52:56
【问题描述】:

我有一个服务(在 Nestjs 中),我想用 Jest 测试函数异常。

我的服务:

class MyService{

public throwError(ms: string) {
    throw new UnprocessableEntityException(ms);
  }
}

我想测试它,但我不知道该怎么做。

【问题讨论】:

    标签: unit-testing jestjs nestjs


    【解决方案1】:

    I feel like jest's docs point this out pretty well,但作为一个简单的例子,对于上面的代码,你只需要一些简单的东西,比如

    describe('MyuService', () => {
      describe('throwError', () => {
        it('should throw an UnprocessableEntityException', () => {
          const myService = new MyService();
          expect(
            () => myService.throwError('some string')
          ).toThrow(new UnprocessableEntityException('some string'));
        })
      })
    })
    

    For even more examples, there's a git repo here with a lot of different samples from rxjs to typeorm to graphql, sync and async

    【讨论】:

    • 尽管问题中的细节非常有限,但这是一个很好的答案。
    猜你喜欢
    • 1970-01-01
    • 2019-08-29
    • 2020-11-16
    • 1970-01-01
    • 2021-02-03
    • 2019-12-28
    • 2018-09-30
    • 2023-03-12
    • 2020-03-30
    相关资源
    最近更新 更多