Failed One Test in User Management

FAIL test/user-management.test.js (6.995s)
User Management
√ it can add a new user to the database (942ms)
√ it returns an error when trying to register duplicate user (221ms)
× it allows a user to login (486ms)
√ it allows a user to logout (436ms)

● User Management › it allows a user to login

TypeError: Cannot convert undefined or null to object

  52 |     console.log(actual, testUser, sessionResult);
  53 |     debugger
> 54 |     delete sessionResult._id
     |     ^
  55 |     expect(sessionResult).toEqual(sessionUser)
  56 |   })

  test("it allows a user to login", async () => {
    const actual = await UsersDAO.loginUser(testUser.email, sessionUser.jwt)
    expect(actual.success).toBeTruthy()
    const sessionResult = await UsersDAO.getUserSession(testUser.email)
    console.log(actual, testUser, sessionResult);
    debugger
    delete sessionResult._id
    expect(sessionResult).toEqual(sessionUser)
  })

console.log test/user-management.test.js:52
    { success: true } 
    { name: 'Magical Mr. Mistoffelees',
      email: 'magicz@cats.com',
      password: 'somehashedpw' } 
     null

I console logged it, and the sessionResult is in fact ‘null’.
But the loginUser returned { success: true }
So, if the user is logged in then it should be in the sessions collection, why is it not finding it?
I have tested my getUserSession function return sessions.findOne({ user_id: email })
and it worked no problem.

Any help appreciated, thanks!

Oh, it passed in the frontend.

Hey @danchann,

I was having this same problem and while you may have had it pass on the frontend, you are missing a piece. I don’t want to just give you the answer but the issue comes from your loginUser function in usersDAO.js. Make sure you have implemented >EVERYTHING< in the TODO Ticket: User Management comment, listed above the updateOne command.
Getting all of the tests to pass is the only way you can get that warm fuzzy feeling from seeing all the green check marks :wink: .

Hope that helps!

  • Chris
2 Likes

Oh, I got it. :smile:
Thanks of the tip, Chris

It’s weird that vscode intellisense gave me the flag in uppercase, which doesn’t work.

For others who get the same error, look at your field names correctly, follow not all the TODO section properly, don’t assume the field names, visit the sessions collection in the mflix db and check the field names once again. I did the same mistake, and then rectified it with the help of the tip from above comment (Christopher_79126). Hope you figure it out.