I am trying to test a function that checks if the user enter the email and if so it returns true, otherwise it passes an error argument to the next function and then it returns false. The test when the user passes an email it runs succesfully, but the test when the user does not provide an email fails. The error logs is that next is not a function. How is it possible to pass next as argument?
const crypto = require("crypto");
const bcrypt = require("bcryptjs");
const jwt = require("jsonwebtoken");
const User = require("../model/userModel");
const throwsAnError = require("../utils/throwsAnError");
exports.signup = async (req, res, next) => {
const {email, username, password, confirmPassword} = req.body;
if(!checkIfEmailExists(email, next)) {
return;
}
try{
const user = await User.create({
email: email,
userName: username,
password: password,
confirmPassword: confirmPassword
});
res.status(200).json({
message: "success",
data: user
})
}
catch(e){
next(new throwsAnError("Ο χρήστης δεν μπορεί να δημιουργηθεί", 400, e));
console.log("I'm in");
}
};
function checkIfEmailExists(email, next) {
if(!email) {
next(new throwsAnError("Συμπληρώστε το e-mail", 400));
return false;
}
return true;
}
exports.checkIfEmailExists = checkIfEmailExists;
const expect = require("chai").expect;
const authController = require("../controller/authController");
describe("Testing if email exist", function() {
it("should return true if email exists", function() {
expect(authController.checkIfEmailExists("email@email.com")).to.be.true;
})
it("should return false if email does not exist", function() {
expect(authController.checkIfEmailExists(undefined, next)).to.be.false;
})
});
Aucun commentaire:
Enregistrer un commentaire