【发布时间】:2018-12-10 12:23:00
【问题描述】:
我有一个测试,我想测试我收到的对象值类型是否与架构匹配。问题是对于某些键,我可能会收到一些东西或 null
目前为止我试过了
const attendeeSchema = {
birthDate: expect.extend(toBeTypeOrNull("Date")),
contact: expect.extend(toBeTypeOrNull(String)),
createdAt: expect.any(Date),
firstName: expect.any(String),
id: expect.any(Number),
idDevice: expect.extend(toBeTypeOrNull(Number)),
information: expect.extend(toBeTypeOrNull(String)),
lastName: expect.any(String),
macAddress: expect.extend(toBeTypeOrNull(String)),
updatedAt: expect.any(Date),
// state: toBeTypeOrNull()
};
const toBeTypeOrNull = (received, argument) => {
const pass = expect(received).toEqual(expect.any(argument));
if (pass || received === null) {
return {
message: () => `Ok`,
pass: true
};
} else {
return {
message: () => `expected ${received} to be ${argument} type or null`,
pass: false
};
}
};
在我的测试中
expect(res.result.data).toBe(attendeeSchema);
我也尝试过 tobeEqual 和其他东西......
我的测试没有通过
TypeError: any() expects to be passed a constructor function. Please pass one or use anything() to match any object.
我不知道在这里做什么.. 如果有人有想法 谢谢
【问题讨论】:
-
您能否确定您的代码最初是哪一行触发了错误?你有很多电话给
any这里.. -
我真的不知道导致错误的行没有给我一个精确的错误,但如果我只保留我的参加者模式的一行,我仍然有同样的错误。错误说我的架构文件的第 3 行是 const pass = expect(received).toEqual(expect.any(argument));
标签: javascript node.js jestjs