测试 Jest 和 NestJs

Test Jest and NestJs

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

我的服务:

class MyService{

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

我想测试一下,但我不知道怎么做。

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