Mocking Express Request with ts-mockito and Typescirpt

1.1k Views Asked by At

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.

1

There are 1 best solutions below

0
On BEST ANSWER

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.