в тестовом наборе. Я хочу смоделировать модель методом цепочки findOne
, затем select
LoginService
public loggingIn = async (loginDTO: LoginDTO) => {
const user = await UserModel.findOne({ email : loginDTO.email }).select(['_id', 'username', 'email', 'password']);
if (user) {
const isPasswordMatching = await bcrypt.compare(loginDTO.password, user.password);
if (isPasswordMatching) {
const token = this.generateToken(user);
const tokenDTO : TokenDTO = {
access_token: token,
expiresIn: loginConstant.EXPIRES_IN,
};
return tokenDTO;
}
throw new InvalidCrendentialsException();
}
throw new InvalidCrendentialsException();
}
тест
it('should return access_token when login is success', async () => {
UserModel.findOne = jest.fn().mockResolvedValueOnce(UserFactory.successResponse);
bcrypt.compare = jest.fn().mockResolvedValueOnce(true);
const loginController = new LoginController();
const app = new App([loginController]);
const result = await request(app.getServer())
.post(`${loginController.path}`)
.send(loginFactory.validRequest);
// expect(result.status).toBe(200);
expect(result.body).toBe(200);
});
ErrorMessage
user_model_1.default.findOne (...). Select не является функцией