How do you unit test a custom param decorator?
See original GitHub issueI’m submitting a…
[ ] Regression
[ ] Bug report
[ ] Feature request
[ X ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.
Current behavior
How can I test the result of a parameter decorator created using createParamDecorator
from @nestjs/common
?
Example
Given the following simple decorator how can I test that it correctly returns “user” from the req object?
export const CurrentUser = createParamDecorator((_, req) => req.user);
I know one option would be to separate the factory function that I’m passing in and just test that, but I’d really rather not export it if that’s possible.
Issue Analytics
- State:
- Created 5 years ago
- Comments:6 (1 by maintainers)
Top Results From Across the Web
Create a Test Case for Custom Parameter Decorator in NestJs 7
In the github of nestjs exists the #1020 issue that describe how to make a test. This issue describe to build a getParamDecoratorFactory...
Read more >Create and Test Decorators in JavaScript | by Netanel Basal
A Method decorator is a function that takes three parameters. The target: Either the constructor function of the class for a static member,...
Read more >Unit testing Typescript decorators - Stack Overflow
Since decorators are just functions I would suggest to just test them like any other function. And only if you really need to,...
Read more >How would I mock a custom decorator on integration test
I want to test an endpoint with supertest. I use a custom decorator to add `user` to the request body and I use...
Read more >Testing | NestJS - A progressive Node.js framework
The Test class has a createTestingModule() method that takes a module metadata object as its argument (the same object you pass to the...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I figured out a solution from digging through the
createParamDecorator
that source. Seems to work pretty well. Is there a better way of doing this?import { ROUTE_ARGS_METADATA } from '@nestjs/common/constants';