Delete user._id

the test “it can add a new user to the database” calls delete user._id
Printing the object after “delete” prints the object w/o the deleted field as expected
yet
the expectation expect(user.name).toEqual(testUser.name) shows _id as still being in the object so it fails

Object {
- “_id”: “5c91b40cf5f8e8243308ff0d”,
“email”: "magicz@cats.com",
“name”: “Magical Mr. Mistoffelees”,
“password”: “somehashedpw”,
}

1 Like

I’m having the same issue. For now, I fixed it by adding delete testUser._id in just below the delete user.id. I don’t get where the testUser gets an _id from tho…

1 Like

Thank you for your help.

I will give a hint. to pass the test, you need to “copy” the object that is used in insertOne.

Because there are 2 things at play here.
One is, probably for convenient’s sake , MongoDB will append _id into the object being pass on when you insert it. That is why the testUser in the test we pass on to it has _id after insertion.
And two, Javascript Object is referenced in memory, not copy when being passed as function 's argument. Any modification inside will affect the actual object.

By copying object and use a second one to insert, the original remains unaffected.

Mystery solved. Took me a while to deduce it.

hope it helps.

3 Likes