I use nestjs (6.5.0) and jest (24.8) and have a method that throws an error:
public async doSomething(): Promise<{ data: string, error?: string }> {
throw new BadRequestException({ data: '', error: 'foo' });
}
How can I write a unit test that checks that we get the expected exception with the expected data? The obvious solution is:
it('test', async () => {
expect(await userController.doSomething())
.rejects.toThrowError(new BadRequestException({ data: '', error: 'foo'});
});
but that doesn't work because new BadRequestException()
creates an object with a different call stack. How can I test this?