User Management: logout test failing but Status Passes

I Have passed the Status test in the application but am still failing one of the Jest tests.
There are two tests for logout.
The first is a call to logout user which appears to have succeeded.
This should have removed this user’s sessions record.
The second test is a call to getUserSession using the same email as before which should not have found anything.
Yet it is failing the toBeNull() test which would lead me to conclude there is a problem with getUserSession. But I cannot find one. And wouldn’t that cause other tests to fail?
Sure could use some help here.

Here’s the result from the test:
FAIL test/user-management.test.js
User Management
v it can add a new user to the database (67ms)
v it returns an error when trying to register duplicate user (31ms)
v it allows a user to login (120ms)
× it allows a user to logout (76ms)

  ? User Management › it allows a user to logout

    expect(received).toBeNull()

    Received: {"_id": "5c70825cc26f765fa7a105dd", "jwt": "hello", "user_id": "magicz@cats.com"}

      58 |     expect(actual.success).toBeTruthy()
      59 |     const sessionResult = await UsersDAO.getUserSession(testUser.email)
    > 60 |     expect(sessionResult).toBeNull()
         |                           ^
      61 |   })
      62 | })
      63 |

      at toBeNull (test/user-management.test.js:60:27)

The failing test is actually related to the implementation of the logoutUser() method, and this would imply you either missed updating that method or have incorrectly implemented the query argument on that method.

The error:

expect(received).toBeNull()

Is basically saying that after it was attempted to delete the session, the subsequent getUserSession() actually found a result. This means your logoutUser() is not correctly implemented.

Full context of the test:

  test("it allows a user to logout", async () => {
    const actual = await UsersDAO.logoutUser(testUser.email)
    expect(actual.success).toBeTruthy()
    const sessionResult = await UsersDAO.getUserSession(testUser.email)
    expect(sessionResult).toBeNull()
  })

Catch being, unless there was a network error the expect(actual.success).toBeTruthy() will always return true even if your arguments to the deleteOne() does not match anything. i.e “Nothing deleted is not an error”.

Also FYI, the /status test does not go and look to see if the “delete” actually did anything. It just looks for an “ok” response, which as stated will always be true outside of other real errors.

1 Like

Thank you for your post. It was most informative.
The problem was indeed in the logout call. I was trying to delete the session by email instead of user_id.