Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

179
Vistas
Method unit testing failing in nestjs

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.

enter image description here

what wrong I am doing?

about 4 years ago · Santiago Gelvez
1 Respuestas
Responde la pregunta

0

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.

about 4 years ago · Santiago Gelvez Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda