-
-
Notifications
You must be signed in to change notification settings - Fork 378
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[NEW TEST] Custom Decorator Testing #857
Comments
It sounds like this is not a decorator created with Nest's |
Yes, it is a custom Decorator that consumes Redis for caching data, it creates a key based on the method name and passed arguments to the function. Its like memoization decorator for Service Methods |
I was looking for a sample of a unit test for a param decorator, and suggest you include one here for completeness. AFAICT, the best way to test a param decorator is to test the factory (CustomParamFactory), that is passed to createParamDecorator, in isolation. To test the ParameterDecorator itself would require a lot of scaffolding. |
Honestly, I would worry about testing a custom decorator via integration and e2e tests that use actual HTTP requests. If you do want to have an easy way to test the factory inside the decorator you'd need to do something like export const myParamFactory = (data: unknown, context: ExecutionContext) => {
return context.switchToHttp().getRequest()['custom-property'];
}
export const MyParam = createParamDecorator(myParamFactory); And now you can just call |
@jmcdo29 yes, I agree, but people like myself will still arrive here looking for a decorator unit test. Hopefully they will find this issue and your comment. |
Unit Tests for Custom Decorators
I created a custom decorator which can be added to Service methods for Caching Data.
But unfortunately cannot find any simple documentation which can provide a way we can test Custom Decorators.
It would be great if you can add something around it.
I found this from the official Github code as a starting point, but since this is such a nice place for all the nest js testing stuff, adding something around decorators would be a plus -https://github.com/nestjs/nest/blob/master/packages/common/test/decorators/cache.decorator.spec.ts
The text was updated successfully, but these errors were encountered: