Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

182
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda