I'm currently working with a typescript interface called cRequest
, which is being used as a type in a class method. This interface extends the express
Request type. I need to figure out how to properly mock this for testing in either jest
or typemoq
.
import { Request } from 'express';
import { User } from '.';
export interface cRequest extends Request {
context: {
ID?: string;
user?: string;
nonUser?: string;
};
user?: User;
}
export default cRequest;
In my code, it's being utilized within a class method like this
import { Response } from 'express';
public getData = async (req: cRequest, res: Response) => {}
When attempting to test it as shown below, it fails
const expRespMock: TypeMoq.IMock<Response> = TypeMoq.Mock.ofType<Response>();
const cReqMock: TypeMoq.IMock<cRequest> = TypeMoq.Mock.ofType<cRequest>();
await classObj.getData(cReqMock, expRespMock);
The error message received is:
Argument of type 'IMock<cRequest>' is not assignable to parameter of type 'cRequest'.
Property 'context' is missing in type 'IMock<cRequest>'.
I need to find the correct approach to inject this interface mock into the method during testing.