I am testing the logging in and deleting the app account feature of the app. Now the POST and GET methods of login are working fine, but when I run the delete test of the app and check if the database is null, it returns the account details with which I logged in and not null as it should. The command deletes the document successfully from the database but still, when I check for null it returns an object of the account details
Now I've tried
1)switching between the supertest methods DEL and DELETE
2)changing the database from MongoDB atlas to locally hosted one
3)changing the latest versions of jest, supertest, MongoDB, and mongoose to previous versions
but nothing worked.
Following is the express code, my test code, and the error message
express code:
user.js
const express = require('express')
const User = require('../models/user')
const auth=require('../middleware/auth')
const router= new express.Router()
router.delete('/users/me',auth, async(req, res)=>{
try{
req.user.remove()
res.send(req.user)
}catch(e){
res.status(500).send()
}
})
module.exports= router
app.js
const userRouter= require('./routers/user')
app.use(express.json())
app.use(userRouter)
module.exports= app
test code:
user.test.js
const request = require('supertest')
const mongoose = require('mongoose')
const jwt = require('jsonwebtoken')
const app = require('../src/app')
const User = require('../src/models/user')
const userOneId = new mongoose.Types.ObjectId()
const userOne = {
_id: userOneId,
name: 'Mike',
email: 'mike@example.com',
password: '56what!!',
tokens: [{
token: jwt.sign({ _id: userOneId }, process.env.JWT_SECRET)
}]
}
beforeEach(async()=>{
await User.deleteMany()
await new User(userOne).save()
})
test('Should login existing user', async () => {
const response = await request(app).post('/users/login').send({
email: userOne.email,
password: userOne.password
}).expect(200)
const user = await User.findById(userOneId)
expect(response.body.token).toBe(user.tokens[1].token)
})
test('Should not login nonexistent user', async () => {
await request(app).post('/users/login').send({
email: userOne.email,
password: 'thisisnotmypass'
}).expect(400)
})
test('Should not delete account for unauthenticate user', async () => {
await request(app)
.del('/users/me')
.send()
.expect(401)
})
test('Should delete account for user', async () => {
await request(app)
.del('/users/me')
.set('Authorization', `Bearer ${userOne.tokens[0].token}`)
.send()
.expect(200)
//the lines of code below are not working
const user = await User.findById(userOneId)
expect(user).toBeNull()
})
the error:
Tests: 1 failed, 8 passed, 9 total
● Should delete account for user
expect(received).toBeNull()
Received: {"__v": 0, "_id": "630477018ef98753a4efebbe", "age": 0, "createdAt": 2022-08-23T06:43:14.383Z, "email": "mike@example.com", "id": "630477018ef98753a4efebbe", "name": "Mike", "updatedAt": 2022-08-23T06:43:14.383Z}
89 | //the lines of code below are not working
90 | const user = await User.findById(userOneId)
> 91 | expect(user).toBeNull()
| ^
92 | })
93 |
94 |
at Object.toBeNull (tests/user.test.js:91:18)
package.json file configs:
"scripts": {
"start": "node src/index.js",
"dev": "env-cmd ./config/dev.env nodemon src/index.js",
"test": "env-cmd ./config/test.env jest --watch"
},
"jest": {
"testEnvironment": "node"
},
test.env
PORT=3000
MONGODB_URL=mongodb://127.0.0.1:27017/task-manager-api-test
I hope I have made my problem clear if you guys need any other info feel free to leave a comment. Thank you.
The problem was in user.js, I was using the mongoose deprecated method remove() for deleting my document. I updated it to deleteOne() method and it started giving me null. I am keeping my doubt and my answer here so if anyone faces the same error they shall be able to solve it quicker than I did. Happy coding!!