Skip to content
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

Open
achyutjhunjhunwala opened this issue Sep 8, 2020 · 5 comments
Open

[NEW TEST] Custom Decorator Testing #857

achyutjhunjhunwala opened this issue Sep 8, 2020 · 5 comments
Assignees
Labels
enhancement New feature or request

Comments

@achyutjhunjhunwala
Copy link

achyutjhunjhunwala commented Sep 8, 2020

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

@achyutjhunjhunwala achyutjhunjhunwala added the enhancement New feature or request label Sep 8, 2020
@jmcdo29
Copy link
Owner

jmcdo29 commented Sep 8, 2020

It sounds like this is not a decorator created with Nest's createParamDecorator, for controllers, resolvers, and gateways, but a general typescript decorator, is that correct?

@achyutjhunjhunwala
Copy link
Author

achyutjhunjhunwala commented Sep 9, 2020

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

@joebowbeer
Copy link

joebowbeer commented Dec 10, 2021

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.

nestjs/nest#1020 (comment)

@jmcdo29
Copy link
Owner

jmcdo29 commented Dec 10, 2021

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 myParamFactory directly in a test while mocking the ExecutionContext. I'll see about adding a sample for this, or feel free to create one with this knowledge and open a PR

@joebowbeer
Copy link

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants