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

198
Visualizações
Why is code coverage inadequate for my controller?

I have below method in my controller. How can I test it? Annotation coverage is failing. Actually I am not able to define code for coverage in jest file.

What parameter I should add in my test file and pass to cover it?

 @Post('upload')
    @UseInterceptors(
        FileInterceptor('image', {
            storage: diskStorage({
                destination: './uploads',
                filename(_, file, callback) {
                    const randomName = Array(32)
                        .fill(null)
                        .map(() => Math.round(Math.random() * 16).toString(16))
                        .join('');
                    return callback(null, `${randomName}${extname(file.originalname)}`);
                },
            }),
        })
    )
    uploadFile(@UploadedFile() file) {
        return {
            url: `http://localhost:2300/api/inventory/uploads/${file.filename}`,
            originalFileName: `${file.originalname}`,
        };
    }

It is working perfectly. I wrote this jest test case for same.

   it('Single file upload', async () => {
            const response = await controller.uploadFile(mockFile);
            const fileService = new InventoryController(service);
            expect(response).toBeTruthy();
            expect(response).toStrictEqual(
            expect.objectContaining({
                        originalFileName: expect.any(String),
                        url: expect.any(String),
                    }),
            );
        });

This is successful but coverage is failing for this line.

 @UseInterceptors(
            FileInterceptor('image', {
                storage: diskStorage({
                    destination: './uploads',
                    filename(_, file, callback) {
                        const randomName = Array(32)
                            .fill(null)
                            .map(() => Math.round(Math.random() * 16).toString(16))
                            .join('');
                        return callback(null, `${randomName}${extname(file.originalname)}`);
                    },
                }),
            })
        )
       
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

What I would do here if code coverage is an absolute must on all lines with unit tests, is move the filename function to a separate file that exports the method so it can be tested directly.

During unit tests, enhancers aren't called, only created, so you won't get any coverage other than the initial "yeah, this decorator works" kind of coverage. For this inner callback, you'll need a separate function.

The other option, if code coverage isn't as important a metric, would be to test this in an e2e test. That way you know for certain this works as intended

about 4 years ago · Juan Pablo Isaza 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