Hey, I am noob in testing actually this is my first project where I write some unit test I have a question, How do I write test for this endpoint using jest and supertest?
Router.post("/create", async (req, res, next) => {
try {
if (Object.keys(req.body).length === 0 && req.body.constructor === Object) {
throw new BadRequest("Missing: request body content")
}
const { username, email } = req.body
const token = jwt.sign(
{
// expire in 1 hour
exp: Math.floor(Date.now() / 1000) + (60 * 60),
data: req.body
}, process.env.JWT_SECRET)
const mailUser = {
email: email,
name: username,
title: "Welcome to our app",
body: `
<h1>Please click the given link to activate your accound.</h1>
<p>${process.env.HOST}/api/users/confirm/account/${token}</p>
`
}
sendingMail(mailUser)
res.json({
message: "Please kindly confirm your account"
})
}
catch (e) {
next(e)
}
})
lt,dr - Here users try to register themselves to my application and here just I take their register credential’s like email,password,username and send a link after storing them in jwt token on their provided email and wait when they click on the link and register themselves or save their data into database.
I already try this -
test('should send signup email to the user.',async () => {
await request(app)
.post('/api/users/create')
.send({
username: "test",
fullName: "test user",
email: "someemail@gmail.com",
password: "test-user"
})
.expect(200)
})
But it only test is email successfully send email to the user or not. How do I test this endpoint?