'Not able to figure out issue in jest code coverage

I have below file in my nestjs.

import { extname } from 'path';
import { diskStorage } from 'multer';
import { v4 as uuid } from 'uuid';
import { HttpException, HttpStatus } from '@nestjs/common';

export const multerConfig = {
    dest: process.env.UPLOAD_LOCATION,
};

export const multerOptions = {
    limits: {
        fileSize: +process.env.MAX_FILE_SIZE,
    },
    fileFilter: (_req: any, file: any, cb: any) => {
        if (file.mimetype.match(/\/(jpg|jpeg|png|gif|pdf|msg|eml)$/)) {
            cb(null, true);
        } else {
            cb(
                new HttpException(
                    `Unsupported file type ${extname(file.originalname)}`,
                    HttpStatus.BAD_REQUEST
                ),
                false
            );
        }
    },
    storage: diskStorage({
        destination: multerConfig.dest,
        filename: (_req: any, file: any, cb: any) => {
            cb(null, `${uuid()}${extname(file.originalname)}`);
        },
    }),
};

I wrote below test cases for it.

import { Readable } from 'stream';
import { multerConfig, multerOptions } from './multer.config';

describe('Multer Configuration ', () => {
    const mockFile: Express.Multer.File = {
        filename: '',
        fieldname: '',
        originalname: '',
        encoding: '',
        mimetype: '',
        size: 1,
        stream: new Readable(),
        destination: '',
        path: '',
        buffer: Buffer.from('', 'utf8'),
    };
    it('should define destination', () => {
        expect(multerConfig).toBeDefined();
        expect(multerConfig.dest).toBe(process.env.UPLOAD_LOCATION);
    });

    it('should define multer upload options', async () => {
        expect(multerOptions).toBeDefined();
        expect(multerOptions.fileFilter).toBeDefined();
        expect(multerOptions.storage).toBeTruthy();
        expect(multerOptions.limits).toBeTruthy();

        const cb = jest.fn();
        multerOptions.fileFilter({}, mockFile, cb);
        expect(cb).toHaveBeenCalled();
        expect(cb).toHaveBeenCalledTimes(1);
        expect(cb()).toBeFalsy();
    });

    afterAll(() => {
        jest.resetAllMocks();
    });
});

Both the test cases are successful but when I check code coverage then it is showing only 50% .It is showing line 16 and 31 not covered.

enter image description here

Line 16 is

 cb(null, true); it comes inside the `if` block

and line 31 is

 cb(null, `${uuid()}${extname(file.originalname)}`);

Could you please help me how can I cover this part? I am really struggling. Do I need to additional test case or I need to modify the existing test case?

Edit 1:-

 const fileType = 'jpg';

    it('should define filetype', async () => {
        const cb = jest.fn();
        process.env.FILE_TYPE = 'jpg';
        multerOptions.fileFilter({}, mockFile, cb);
        expect(cb).toHaveBeenCalled();
        expect(cb).toHaveBeenCalledTimes(1);
        expect(cb()).toBeFalsy();
    });

Test case getting success. but coverage and lines are still same



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source