我有一個服務(在 Nestjs 中),我想測驗 Jest 的函式例外。
我的服務:
class MyService{
public throwError(ms: string) {
throw new UnprocessableEntityException(ms);
}
}
我想測驗它,但我不知道該怎么做。
uj5u.com熱心網友回復:
我覺得 jest 的檔案很好地指出了這一點,但作為一個簡單的例子,對于上面的代碼,你只需要一些簡單的東西
describe('MyuService', () => {
describe('throwError', () => {
it('should throw an UnprocessableEntityException', () => {
const myService = new MyService();
expect(
() => myService.throwError('some string')
).toThrow(new UnprocessableEntityException('some string'));
})
})
})
對于更多示例,這里有一個 git repo,其中包含從 rxjs 到 typeorm 到 graphql、sync 和 async 的許多不同示例
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/370988.html
