admin管理员组文章数量:1425221
Sometimes I need to assert only specific properties of the actual object and I don't care about the other properties.
For example:
const actual = [
{ a: 1, additionalProp: 'not interestig' },
{ a: 2, additionalProp: 'not interestig' }
];
expect(actual).toEqual([
{ a: 1 },
{ a: 2 }
])
This currently fails with:
Expected $[0] not to have properties
additionalProp: 'random value'
How can I write the expectation?
I currently do something like this:
expect(actual.map(i => ({a: 1}))).toEqual([
{ a: 1 },
{ a: 2 }
])
but I don't like it much and it does not work for more plex objects
Sometimes I need to assert only specific properties of the actual object and I don't care about the other properties.
For example:
const actual = [
{ a: 1, additionalProp: 'not interestig' },
{ a: 2, additionalProp: 'not interestig' }
];
expect(actual).toEqual([
{ a: 1 },
{ a: 2 }
])
This currently fails with:
Expected $[0] not to have properties
additionalProp: 'random value'
How can I write the expectation?
I currently do something like this:
expect(actual.map(i => ({a: 1}))).toEqual([
{ a: 1 },
{ a: 2 }
])
but I don't like it much and it does not work for more plex objects
Share Improve this question edited Oct 30, 2019 at 13:06 adiga 35.3k9 gold badges65 silver badges87 bronze badges asked Oct 30, 2019 at 12:58 LieroLiero 27.5k41 gold badges179 silver badges337 bronze badges 01 Answer
Reset to default 5You could explicitly address specific objects and their relevant attributes?
expect(actual.length).toBe(2);
expect(actual[0]['a']).toBe(1);
expect(actual[1]['a']).toBe(2);
Another approach is using jasmine.obectContaining
. This may be more appropriate in case the expected objects contain several properties.
const expected = [
{ a: 1 },
{ a: 2 }
];
expect(actual.length).toBe(expected.length);
for (let i = 0; i < expected.length; i++) {
expect(actual[i]).toEqual(jasmine.objectContaining(expected[i]));
}
本文标签:
版权声明:本文标题:javascript - Jasmine expect toEqual but ignore additional properties on the actual object to prevent 'Expected ... not t 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745360970a2655291.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论