In my Express project, I am conducting the following test:
it('testing club', async () => {
let club = await clubDAO.create(baseClubParams);
console.log(club)
const resp = await http.put(`/api/user/follow/${club._id}`);
isOk(resp);
resp.body.data.clubs.should.include(club);
console.log(resp.body.data.clubs)
// Re-fetch user info to verify that the change persisted
const userResp = await http.get(`/api/user/${currentUser._id}`);
userResp.body.data.clubs.should.include(clubId);
});
Based on the console log, I can see that the club object is:
{
admins: [],
_id: 5e8b3bcb1dc53d1191a40611,
name: 'Club Club',
facebook_link: 'facebook',
description: 'This is a club',
category: 'Computer Science',
__v: 0
}
and the clubs array is:
[
{
admins: [],
_id: '5e8b3bcb1dc53d1191a40611',
name: 'Club Club',
facebook_link: 'facebook',
description: 'This is a club',
category: 'Computer Science',
__v: 0
}
]
The test fails at resp.body.data.clubs.should.include(club) because the _id of the club is an ObjectId and the _id of clubs[0] is a string:
AssertionError: expected [ Array(1) ] to include { admins: [],
_id:
{ _bsontype: 'ObjectID',
id: <Buffer 5e 8b 3b cb 1d c5 3d 11 91 a4 06 11>,
toHexString: [Function],
get_inc: [Function],
getInc: [Function],
generate: [Function],
toString: [Function],
toJSON: [Function],
equals: [Function: equals],
getTimestamp: [Function],
generationTime: 1586183115 },
name: 'Club Club',
facebook_link: 'facebook',
description: 'This is a club',
category: 'Computer Science',
__v: 0 }
To address this issue, I attempted the following:
let club = await clubDAO.create(baseClubParams);
club._id = club._id.toString()
console.log(club)
Unfortunately, the club id remains as an ObjectId even after this attempted fix. Can someone provide insight into why this is happening?