How do you unit test a custom param decorator?

See original GitHub issue

I’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:closed
  • Created 5 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

9reactions
itslennycommented, Aug 30, 2018

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?


function getParamDecoratorFactory(decorator: Function) {
  class Test {
    public test(@decorator() value) { }
  }

  const args = Reflect.getMetadata(ROUTE_ARGS_METADATA, Test, 'test');
  return args[Object.keys(args)[0]].factory;
}

/// Usage

const factory = getParamDecoratorFactory(CurrentUser);
const mockUser = {};
const result = factory(null, {user: mockUser));

expect(result).toBe(mockUser);
2reactions
danieldaeschlecommented, Jan 22, 2019

import { ROUTE_ARGS_METADATA } from '@nestjs/common/constants';

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found