I have an API and I want to assert that at least the expected data is returned. I don't care if more data is returned.
Therefore I want to compare two objects (expected
and actual
) where all attributes of expected
must be equal to actual
, but actual
may contain more attributes:
var expected = {
foo: 1,
bar: {
x1: 42,
a1: [
1,
2,
{
x: 7
}
]
}
}
var actual = {
foo: 1,
whatever: 55, // to be ignored
additional: { // to be ignored
ignore: 1
},
bar: {
x1: 42,
a1: [
1,
2,
{
x: 7,
y: 8 // to be ignored
}
]
}
}
partiallyEqual(expected, actual) // returns true
Some more examples:
partiallyEqual({x: 1}, {a:2, x:1}) // return true
partiallyEqual({x: 1}, {a:2, x:2}) // return false (x is different)
Arrays may (optionally) be subject to partial equivalent, if actual
contains additional elements.
partiallyEqual([1, 3], [1, 2, 3]) // return true
partiallyEqual([3, 1], [1, 2, 3]) // return false (different order)