javascript - 如何使用 jest 测试包含数组的对象
问题描述
我正在尝试测试包含几个复杂字段的对象:
- 其中一个字段包含对象数组。
- 其中一个字段包含对象,其字段之一包含对象数组。
我尝试使用expect(createdRequest).toEqual(expect.objectContaining(originRequestData));
. 我也尝试使用expect().toMatchObject()
和其他很多方法,但没有一个有效。
这是我要测试的对象的接口:
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;
}
我使用的模拟:
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(),
};
我写的测试:
test('Should create a single request', async () => {
const createdRequest: IRequest = await RequestsManager.create(validRequest);
expect(createdRequest).toEqual(expect.objectContaining(validRequest));
});
解决方案
推荐阅读
- ruby-on-rails - 调用基于超级的 __callee__ 值
- python - 远程环境中的openCV函数'.read()'返回(False,None)
- azure-active-directory - 为 Azure AD 应用程序启用 OAuth 2.0 隐式流功能
- python - 如何保存或下载我在请求中获得的图像——Python
- php - 无法从 XML 加载字符串 (SimpleXML)
- android - 我一直只在我的 recyclerview 中获得第一项,但在 logcat 中我得到了所有
- entity-framework-core - EF Core 迁移未被识别
- limesurvey - limesurvey - 来自另一个 php 应用程序的自动填充令牌
- python - 写入文件:在 X MB 文件容量后切换到新文件
- microsoft-translator - NMT V3 中的动态字典功能