I have below only method in my controller.
@Controller()
export class AppController {
@Get('whoami')
@Roles(Role.All)
@ApiResponse({ status: 200, type: User })
currentUser(@Req() request: Request) {
return request.user;
}
}
I am writing below test case for the same.
describe('AppController', () => {
let controller: AppController;
beforeAll(async () => {
const app: TestingModule = await Test.createTestingModule({
controllers: [AppController],
}).compile();
controller = app.get<AppController>(AppController);
});
it('should be defined', () => {
expect(controller).toBeDefined();
});
it('currentUser should be called', () => {
expect(controller.currentUser).toBeDefined();
expect(controller.currentUser).toEqual("shruti");
});
});
not only this is failing but also return request.user;
not getting covered in coverage. below is the error.
what wrong I am doing?
CodePudding user response:
controller.currentUser
is a function. It takes in the Request
object and returns request.user
. Your test should look something like
it('currentUser should be called', () => {
expect(controller.currentUser).toBeDefined();
expect(controller.currentUser({ user: "shruti" } as any)).toEqual("shruti");
});
The as any
is necessary here because Request
has way more properties than just user
but all your test and code care about is that user
is there to be returned.