dec*_*ijo 4 unit-testing exception jestjs nestjs
我使用 nestjs (6.5.0) 和 jest (24.8) 并有一个抛出错误的方法:
public async doSomething(): Promise<{ data: string, error?: string }> {
throw new BadRequestException({ data: '', error: 'foo' });
}
Run Code Online (Sandbox Code Playgroud)
如何编写单元测试来检查我们是否获得了预期数据的预期异常?显而易见的解决方案是:
it('test', async () => {
expect(await userController.doSomething())
.rejects.toThrowError(new BadRequestException({ data: '', error: 'foo'});
});
Run Code Online (Sandbox Code Playgroud)
但这不起作用,因为new BadRequestException()创建了一个具有不同调用堆栈的对象。我该如何测试?
与jest 文档中的示例相比,您在这里可能有 2 个问题。
await应该在expect论点之外rejects 意味着抛出了一个错误,所以你测试是否相等就像是:
it('test', async () => {
await expect(userController.doSomething())
.rejects.toEqual(new BadRequestException({ data: '', error: 'foo'});
});
Run Code Online (Sandbox Code Playgroud)
回答我自己的问题:
使用自定义匹配器(见下文),测试可以编写为:
it('test', async () => {
await expect(userController.doSomething()).rejects.toContainException(
new BadRequestException({ data: '', error: 'foo' }),
);
});
Run Code Online (Sandbox Code Playgroud)
自定义匹配器:
import { HttpException } from '@nestjs/common';
// ensure this is parsed as a module.
export {};
// https://stackoverflow.com/questions/43667085/extending-third-party-module-that-is-globally-exposed
declare global {
namespace jest {
interface Matchers<R> {
toContainException: (expected: R | any) => {};
}
}
}
// this will extend the expect with a custom matcher
expect.extend({
toContainException<T extends HttpException>(received: T, expected: T) {
const success =
this.equals(received.message, expected.message) &&
this.equals(received.getStatus(), expected.getStatus());
const not = success ? ' not' : '';
return {
message: () =>
`expected Exception ${received.name}${not} to be ${expected.name}` +
'\n\n' +
`Expected: ${this.utils.printExpected(expected.message)}, ` +
`status: ${this.utils.printExpected(expected.getStatus())} \n` +
`Received: ${this.utils.printReceived(received.message)}, ` +
`status: ${this.utils.printReceived(received.getStatus())}`,
pass: success,
};
},
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2478 次 |
| 最近记录: |