我使用nestjs(6.5.0)和jest(24.8)并具有引发错误的方法:
public async doSomething(): Promise<{ data: string, error?: string }> {
throw new BadRequestException({ data: '', error: 'foo' });
}
我如何编写一个单元测试来检查我们是否获得了带有预期数据的预期异常?显而易见的解决方案是:
it('test', async () => {
expect(await userController.doSomething())
.rejects.toThrowError(new BadRequestException({ data: '', error: 'foo'});
});
但是不起作用,因为new BadRequestException()
用不同的调用堆栈创建了一个对象。我该如何测试?
答案 0 :(得分:0)
与jest documentation中的示例相比,您在这里可能有两个问题。
await
应该在expect
参数之外rejects
意味着引发了错误,因此您需要测试是否相等类似的东西:
it('test', async () => {
await expect(userController.doSomething())
.rejects.toEqual(new BadRequestException({ data: '', error: 'foo'});
});
答案 1 :(得分:0)
回答我自己的问题:
使用自定义匹配器(见下文),测试可以写为:
it('test', async () => {
await expect(userController.doSomething()).rejects.toContainException(
new BadRequestException({ data: '', error: 'foo' }),
);
});
自定义匹配项:
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,
};
},
});