Search code examples
typescriptunit-testingexpressmockingts-mockito

Mocking Express Request with ts-mockito and Typescirpt


Is it possible to mock the class Request from Express using ts-mockito in typescript?

I tried the following

import { Request, Response } from "express";  

const request = mock(Request);
const req: Request = instance(request);

but get an error on req stating: Type 'Request' is missing the following properties from type 'Request<ParamsDictionary>': get, header, accepts, acceptsCharsets, and 73 more.


Solution

  • Request is an interface, so you should use this syntax:

    import { Request, Response } from "express";
    import { mock, instance } from "ts-mockito";
    
    const mockReq = mock<Request>();
    const req = instance(mockReq);
    

    Refer to the ts-mockito documentation for more.