Jasmine javascript : How to check response contain something?

How to check response contain postText with wuihduwih and userId: ’60f469cf784379051298e96d’

here is the response : ({ id: '612bf0792ca01806398da2d6', data: Object({ createdOn: '2021-08-29T20:39:21.445Z', lastUpdatedBy: null, userId: '60f469cf784379051298e96d', displayName: 'Nadia', postText: 'wuihduwih', postImages: [ ], pluginInstance: Object({ pluginInstanceId: '1627334094776-047554258642281355', pluginInstanceTitle: 'communityFeedPlugin' }), isPublic: false, _buildfire: Object({ index: Object({ array1: [ Object({ string1: 'userId_60f469cf784379051298e96d' }), Object({ string1: 'displayName_nadia' }), Object({ string1: 'pluginTitle_communityfeedplugin' }), Object({ string1: 'isPublic_0' }) ] }) }) }), tag: 'posts' })

tried the following but failed :

        Posts.addPost({postText :'wuihduwih'},(err,resp) => {
          expect(resp).toContain(jasmine.objectContaining({
              postText :'wuihduwih'
            }));
          done();
        }); ```

Answer

Based on Jasmine introductory page, toContain “works for finding an item in an Array” and “also works for finding a substring”. They make no mention of using toContain to find objects within objects.

If the response’s structure is always the same/the fields userId and postText are always in the same spot, you can just do:

expect(resp.data.postText).toEqual('wuihduwih');

and

expect(resp.data.hasOwnProperty('userId')).toBe(true);

or, if the userId never changes:

expect(resp.data.hasOwnProperty('userId')).toEqual('60f469cf784379051298e96d');

Based on the same introductory page, you can also use objectContaining, as you’ve tried, but combined with toEqual instead of toContain:

expect(resp.data).toEqual(jasmine.objectContaining({
  postText: 'wuihduwih',
  userId: '60f469cf784379051298e96d'
}));

Note that I pass in resp.data, not just resp. The referenced page doesn’t say whether it works for nested objects.