Hi,
So I am trying to understand the limits of jest and I know that if I have a function that calls a function which returns a promise I'm able to test it.
If that promise has multiple .then' s chained and one of them calls a different function that has it own .then, i have no idea how I would test the .then return value.
To give an example I have the following express middleware function:
export const canAccess =
(module: string, action: string) =>
(req: Request, res: Response, next: NextFunction) => {
try {
var cookie = JSON.parse(req.cookies["something"]);
return AuthController.validateToken(cookie._id, process.env.TOKEN_SECRET)
.then((decoded: userInviteJwtToken) => {
if (decoded.companyId !== cookie.companyId) {
throw new Error("Cookie and token contents are different!");
}
return decoded;
})
.then((decoded: userInviteJwtToken) =>
CompanyController.getCompany(decoded.companyId).then((company) => { return {decodedToken: decoded, company: company};})
)
.then((payload) =>
RoleController.getRole(payload.decodedToken.roleId).then((role) => { return {...payload, role};})
)
.then((payload) => {
if (payload) {
// do something
return
}
throw new Error(
"You don't have the right permissions to access this resource!"
);
})
.catch((err) => {
if (err.status) {
res.status(err.status).json({ message: err.message });
} else {
res.status(400).json({ message: err.message });
}
});
} catch (err) {
res.status(400).json({
message: err,
});
}
};
Now I want to know how I can test this function, more specifically how I can test that the .then after the .getCompany method is returning both token and company data.
Any ideas?
Thx!