Argument of type is not assignable (Jest Testing, Typescript)

Viewed 14

I implemented an application with express.js using typescript.

I have the following method I want to test with Jest.

export const Register = async (req: Request, res: Response) => {
    const body = req.body;

    if (body.password !== body.password_confirm) {
        return res.status(400).send({
            message: "Password does not match"
        });
    }                                

    const {password, ...user} = await connectDB.getRepository(User).save({
        first_name: body.first_name,
        last_name: body.last_name,
        email: body.email,
        password: await bcryptjs.hash(body.password, 10)
    });

    res.send(body);
};

My test looks like that:

it ("should return 400 if password does not match", async () => {
        const req = {
            body: {
                first_name: "John",
                last_name: "Doe",
                email: "john.doe@test.com",
                password: "123456",
                password_confirm: "1234567"
            }
        };
        const res = {
            status: jest.fn().mockReturnThis(),
            send: jest.fn()
        };
        await AuthController.Register(req, res);
        expect(res.status).toHaveBeenCalledWith(400);
    });

That's my error:

Argument of type '{ body: { first_name: string; last_name: string; email: string; password: string; password_confirm: string; }; }' is not assignable to parameter of type 'Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>'. Type '{ body: { first_name: string; last_name: string; email: string; password: string; password_confirm: string; }; }' is missing the following properties from type 'Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>': get, header, accepts, acceptsCharsets, and 80 more.ts(2345)

Thank you for your help!

0 Answers
Related