'how to test controller with function decorators in nestjs
I have just implemented an interceptor that is used as a decorator in the controller, but when I try to run the test for the controller, the interceptor doesn't seem to be called, so I don't know if I am doing it wrong or if it is not possible to have the decorator functions called in unit tests.
here is my test case :
it('should throw an error', async () =>
const input = makeInput(1);
const user = makeUserWithApplications();
const postResponse = makeResponse(
'input', Status.USER_ERROR, HttpStatus.BAD_REQUEST
)
jest.spyOn(httpService, 'post').mockImplementation(() => {
throw new BadRequestException(postResponse);
});
jest.spyOn(repository, 'findById').mockImplementation(
() => Promise.resolve(input)
);
jest.spyOn(application, 'checkUserPermission').mockImplementation();
// jest.mock('../../../shared/exceptions/invalidSolverDataException.ts', () => ())
jest.spyOn(service, 'getData').mockImplementation(() => {
throw new BadRequestException(postResponse);
});
const exception = await getExceptionAsync(() =>
inputController.getData(1, user ))
expect(exception).toBeInstanceOf(InvalidSolverDataException);
});
and this the controller:
@UseInterceptors(SolverInterceptor)
@Get(':inputId/visuWithApi')
async getData(@Param('inputId') inputId: number, @GetUser() user: User): Promise<model> {
const input = await this.inputService.findById(
inputId, user.company.id, u => u.instance, u => u.instance.application
);
const application: Application = input.instance.application;
await this.applicationService.checkUserPermission(user, application);
return this.inputService.getData(input);
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
