我正在尝试编写用于分析树结构的测试,并希望将它们逐层拆分。
class Root {
value: string;
children: Child[];
}
class Child {
value: string;
}
test("Root is parsed correctly", () => {
const expected: Root = {
value: "foo",
children: [
// Don't care about how children look, just if there is 2 of them
{},
{},
]
}
const received: Root = // Parse the data
// Test fails because children don't equal
expect(received).toEqual(expected)
})
在上面的示例中,我具有要测试的Root
类,但是仅在数量正确的情况下,才关心children
数组中的内容。使用此代码,测试当然不会通过,因为子级expected
中的空对象与received
中的子级不匹配。
有没有一种方法不能轻松比较嵌套对象?