我是jest
的新手,所以不确定是否是我的错。这是我的测试用例
it('should throw error if wrong email or phone number is provided', async () => {
await expect(userService.verifyCredentials('invalidemail@invaliddomain.com', 'sayantan94'))
.rejects
.toEqual(new UnauthorizedException('Invalid email or phone number'));
})
但是,即使抛出相同的异常,此操作也会失败。这是输出
FAIL src/modules/user/user.service.spec.ts (5.156s)
● UserService › verifyCredentials › should throw error if wrong email or phone number is provided
expect(received).toStrictEqual(expected)
Expected value to equal:
[Error: [object Object]]
Received:
[Error: [object Object]]
Difference:
Compared values have no visual difference.
88 | await expect(userService.verifyCredentials('invalidemail@invaliddomain.com', 'sayantan94'))
89 | .rejects
> 90 | .toEqual(new UnauthorizedException('Invalid email or phone number'));
| ^
91 | })
92 | })
93 |
我该如何检查?
答案 0 :(得分:4)
尝试使用toEqual
代替toThrow
it('should throw error if wrong email or phone number is provided', async () => {
await expect(userService.verifyCredentials('invalidemail@invaliddomain.com', 'sayantan94'))
.rejects
.toThrow('Invalid email or phone number');
});