最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Expect deep property to have any of multiple values - Stack Overflow

programmeradmin5浏览0评论

In Chai assertion library, we can assert a deep property to exist and have a value:

expect(obj).to.have.deep.property("field1.field2", 1);

But, what if we need to assert this property to have one of multiple values? In this case, the test should pass if obj has a field1.field2 property that has 0 or 1 or 2 value.


FYI, I need this to check that a ESLint plugin ships with a remended rules configuration that has a "warning level" configured for every rule. Warning level can be of 0, 1 or 2 values.

In Chai assertion library, we can assert a deep property to exist and have a value:

expect(obj).to.have.deep.property("field1.field2", 1);

But, what if we need to assert this property to have one of multiple values? In this case, the test should pass if obj has a field1.field2 property that has 0 or 1 or 2 value.


FYI, I need this to check that a ESLint plugin ships with a remended rules configuration that has a "warning level" configured for every rule. Warning level can be of 0, 1 or 2 values.

Share edited Jun 29, 2016 at 17:57 alecxe asked Jun 29, 2016 at 17:50 alecxealecxe 475k127 gold badges1.1k silver badges1.2k bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

You can use .oneOf():

expect(obj).to.have.deep.property('field1.field2').that.is.oneOf([ 0, 1, 2 ])

Or .within():

expect(obj).to.have.deep.property('field1.field2').that.is.within(0, 2)
发布评论

评论列表(0)

  1. 暂无评论