How to write Jest test case for middleware in Node.js

  Kiến thức lập trình
export const validatePathParametr = (req: Request, res: Response, next: NextFunction): void => {
    const { id, name,  } = req.params;
    
    
    try {
        validateId(id);
        validateName(name)
       
    } catch (error: any) {
        error.msg= 'wrong parameter';
        error.code= 500;
        getLogger().error({
            req,
            error,
        });
        return next(error);
    }
    return next();
};


function validateId(id){

    if (!id || id === null) {
                throw new Error(`id cannot be empty`);
            }
}

function validateName(name){

    if (!name || name === null) {
                throw new Error(`name cannot be empty`);
            }
}



// Test code

import { createRequest, createResponse } from 'node-mocks-http';

import { validatePathParametr } from '../../validatePathParameter';
jest.mock('../../validatePathParameter', () => ({
    validateId: jest.fn(),
    validateName: jest.fn(),
   
}));

const { validateCluster: mockvalidateId , validateName: mockvalidateName } = require('../../validatePathParameter');

describe.only('validateParams Middleware', () => {
    let mockRequest: any;
    let mockResponse: any;
    let next: jest.Mock;

    beforeEach(() => {
        mockRequest = createRequest({
            params: { id: 11,  name: 'test' }
        });
        mockResponse = createResponse();
        next = jest.fn();
        jest.clearAllMocks(); 
    });
    console.log('typeof validateParams',typeof validateParams);

    it('should call validateName, validateId and invoke next on success', () => {
       
        (mockvalidateId).mockImplementation(() => {});
        (mockvalidateName).mockImplementation(() => {});

        // Call the middleware
        validatePathParametr(mockRequest, mockResponse, next);
       

       
        expect(mockvalidateId).toHaveBeenCalledWith('2');
        expect(mockvalidateName).toHaveBeenCalledWith('test');
        expect(next).toHaveBeenCalled(); // Ensure next() was called
    });

    it('should call next with an error when validateName fails', () => {
        const nameErrorMessage = 'id cannot be empty';
        (mockvalidateId).mockImplementation(() => {
            throw new Error(nameErrorMessage);
        });

       
        validatePathParametr(mockRequest, mockResponse, next);

       
        expect(next).toHaveBeenCalledWith(expect.any(Error));
        expect(next).toHaveBeenCalledWith(new Error(nameErrorMessage));
    });


});

I am getting this error TypeError: (0 , validatePathParametr_1.validatePathParametr) is not a function

Although export and import looks fine. Still getting above error
How to write test case for this middleware

Not sure where is the issue is it main file or test file?

I have tried multiple things but it’s not working

3

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website Kho Theme wordpress Kho Theme WP Theme WP

LEAVE A COMMENT