How to test object that contains array using jest

Viewed 123

I'm trying to test object that contains several complicated fields:

  1. one of the fields contains array of objects.
  2. one of the fields contains object that one of its fields contains array of objects.

I tried to use expect(createdRequest).toEqual(expect.objectContaining(originRequestData));. I also tried to use expect().toMatchObject() and another a lot of methods but none of them worked.

This is interface of the object I want to test:

export interface IRequest {
  id?: string;
  openDate?: Date;
  closeDate: Date;
  approvals:
  {
    replyUser: Types.ObjectId;
    answer: number | null;
  }[]; // Array of objects
  target: {
    recordId?: Types.ObjectId;
    collectionName: string;
  };
  action: {
    actionType: ActionType;
    additionalData: AdditonalDataType[]; // Array of objects. note: AdditonalDataType  
                                         // is an interface of object.
  };
  comment?: string;
  requester: Types.ObjectId;
}

The mock I use:

export const validRequest: IRequest = {
  closeDate: new Date('12/10/2030'),
  approvals: [{
    replyUser: new Types.ObjectId(),
    answer: 2,
  }],
  target: {
    recordId: Types.ObjectId(),
    collectionName: 'occupations',
  },
  action: {
    actionType: ActionType.create,
    additionalData: [{
      field: 'job',
      value: 'chef',
    }],
  },
  requester: new Types.ObjectId(),
};

The test I wrote:

test('Should create a single request', async () => {
  const createdRequest: IRequest = await RequestsManager.create(validRequest);
  expect(createdRequest).toEqual(expect.objectContaining(validRequest));
});

The error I received: enter image description here

0 Answers
Related