This is a sample of resolver
in NestJs, and I'm about to write tests for this file. But there is no documentation for testing resolvers in nestjs docs.
I already have a test for my service
, but resolvers also may have little logic inside them, so it's better to have tests for them as well.
How I can test resolver
files?
import { ObjectId } from 'mongodb';
import { AuthGuard } from '../utils/Auth.guards';
import { UseGuards } from '@nestjs/common';
import { IUser } from '../users/users.service';
import { User } from '../utils/user.decorator';
import { Query, Resolver, Mutation, Args } from '@nestjs/graphql';
import { AccessService } from './access.service';
import { NeedAccess } from '../utils/needAccess.decorator';
import { HasAccess } from '../utils/access.decorator';
@Resolver('Accesss')
@UseGuards(AuthGuard)
export class AccessResolvers {
constructor(private readonly accessService: AccessService) {}
@Query()
@NeedAccess()
access(
@Args('userId') userId: ObjectId,
@User() user: IUser,
@HasAccess(['access.view']) hasAccess,
) {
if (userId && hasAccess) { // this might be a situation to concern about in tests
return this.accessService.getUserAccess(userId);
} else {
return this.accessService.getUserAccess(user._id);
}
}
}