Cannot test delete with supertest (for testing the express app) in Jest


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:


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)=>{

module.exports= router


const userRouter= require('./routers/user')
module.exports= app 

test code:


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: '[email protected]',
    password: '56what!!',
    tokens: [{
        token: jwt.sign({ _id: userOneId }, process.env.JWT_SECRET)

    await User.deleteMany()
    await new User(userOne).save()

test('Should login existing user', async () => {
    const response = await request(app).post('/users/login').send({
        password: userOne.password
    const user = await User.findById(userOneId)

test('Should not login nonexistent user', async () => {
    await request(app).post('/users/login').send({
        password: 'thisisnotmypass'

test('Should not delete account for unauthenticate user', async () => {
    await request(app)

test('Should delete account for user', async () => {
    await request(app)
        .set('Authorization', `Bearer ${userOne.tokens[0].token}`)
    //the lines of code below are not working
    const user = await User.findById(userOneId)

the error:

Tests:       1 failed, 8 passed, 9 total
  ‚óŹ Should delete account for user


    Received: {"__v": 0, "_id": "630477018ef98753a4efebbe", "age": 0, "createdAt": 2022-08-23T06:43:14.383Z, "email": "[email protected]", "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"



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!!

Answered By – Upendra Bhanushali

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published