type-graphql: Jest does not cover decorators with parameters

Describe the bug When using Jest coverage, decorators with additional parameters appear as uncovered :

  • @Field(type => ID)
  • @Field(type => [Team])
  • @Query(returns => User)
  • @Mutation(returns => User)
  • Plain decorators such as @Field() are covered.

To Reproduce User class

@ObjectType()
export class User {
  @Field(type => ID)
  @IsMongoId()
  _id!: Types.ObjectId;

  @Field()
  @IsEmail()
  email!: string;

  @Field()
  @IsString()
  name!: string;

  @Field(type => [Team])
  @IsArray()
  teams?: Team[];
}

User resolver

@Resolver(of => User)
export class UserResolver {
  constructor(private readonly usersService: UserService) {}

  @Query(returns => User)
  async user(@Args('id') id: string): Promise<User> {
    const user = await this.usersService.findById(id);
    if (!user) {
      throw new NotFoundException(id);
    }
    return user;
  }

  @Query(returns => [User])
  async users(): Promise<User[]> {
    return await this.usersService.findAll();
  }

  @Mutation(returns => User)
  @UsePipes(new ValidationPipe())
  async addUser(
    @Args('addUserInput') addUserInput: AddUserInput
  ): Promise<User> {
    return await this.usersService.add(addUserInput);
  }
}

Jest output

src/user                    |    92.54 |      100 |    72.22 |       91.8 
  user.model.ts             |    89.47 |      100 |        0 |      88.24 
  user.resolver.ts          |    86.36 |      100 |     62.5 |         85 

Expected behavior Decorators should be covered

Logs If applicable, add some console logs to help explain your problem. You can paste the errors with stack trace that were printed when the error occured.

Environment (please complete the following information):

  • OS: Linux Alpine 3.10.3
  • Node v13.3.0
  • Package v0.17.5
  • TypeScript v3.6.3

Additional context

  • Jest v24.9.0
  • Typegoose v6.1.8
  • NestJS 6

Did I miss something ? I read issue #377 but didn’t find a solution. Thank you !

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 1
  • Comments: 16 (7 by maintainers)

Most upvoted comments

As I said - use @Resolver() without callback and @Field(() => ID) without args.

Nothing more I can do, so closing this issue 🔒

export const returingUser = () => User;

then in ur decorator @Query(returningUser)

if there’s any progression for this in jest, please someone post a link here so we can resolve our coverage issues

So please post an issue on the Jest repo that it doesn’t properly report coverage for decorators.

export const returingUser = () => User;

then in ur decorator @Query(returningUser)

I already tried your suggestion to omit args with @Field(() => ID) and @Query(() => User), but it still appears as uncovered. Thank you for your time anyway.